Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/adblockplus.js

Issue 29355962: Issue 4023 - Use localforage (IndexedDB) instead of localStorage (Closed)
Patch Set: Make sure we always return asynchronously from loadFile. Add try-catch block. Created Oct. 6, 2016, 11:26 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « background.html ('k') | qunit/index.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/adblockplus.js
===================================================================
--- a/lib/adblockplus.js
+++ b/lib/adblockplus.js
@@ -357,60 +357,43 @@
return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
}
- function loadFile(file)
- {
- return new Promise(function(resolve, reject)
- {
- var key = fileToKey(file);
- ext.storage.get([key], function(items)
- {
- var entry = items[key];
- if (!entry)
- {
- try
- {
- entry = JSON.parse(window.localStorage.getItem(key));
- }
- catch (err)
- {}
- }
- if (entry)
- {
- resolve(entry);
- }
+ function loadFile(file, successCallback, errorCallback)
+ {
+ var key = fileToKey(file);
+ var entry = localStorage.getItem(key);
+ if (entry)
+ {
+ try
+ {
+ setTimeout(successCallback(JSON.parse(entry)), 0);
Sebastian Noack 2016/10/06 23:38:35 Not too important but you can call setTimout() wit
Oleksandr 2016/10/07 16:29:17 Done. But I think it wouldn't hurt catching an err
kzar 2016/10/07 17:00:00 Wouldn't this also catch an exception thrown by th
Oleksandr 2016/10/07 17:21:06 Yes, but I think that's expected, no?
+ }
+ catch(err)
+ {
+ setTimeout(errorCallback(new Error("File is corrupted")), 0);
+ }
+ }
+ else
+ {
+ localforage.getItem(key, function(err, value)
+ {
+ if (err || !value)
+ errorCallback(new Error("File doesn't exist"));
else
- {
- reject(new Error("File doesn't exist"));
- }
+ successCallback(value);
});
- }.bind(this));
+ }
}
+
function saveFile(file, data, callback)
{
- var entry = {};
var key = fileToKey(file);
+ var entry = {
+ lastModified: Date.now(),
+ content: data
+ };
- if (typeof browser == "undefined")
- {
- entry[key] = {
- lastModified: Date.now(),
- content: data
- };
- ext.storage.set(entry, callback);
- }
- else
- {
- var processedData = LZString.compressToUTF16(JSON.stringify(data));
- ext.storage.remove(key);
- entry[key] = {
- lastModified: Date.now(),
- content: processedData,
- compressed: true
- };
- window.localStorage.setItem(key, JSON.stringify(entry[key]));
- setTimeout(callback, 0);
- }
- callback();
+ localStorage.removeItem(key);
+ localforage.setItem(key, entry, callback);
}
exports.IO = {
resolveFilePath: function(path)
@@ -423,10 +406,6 @@
{
if ("content" in entry)
{
- if (entry["compressed"])
- {
- entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content));
- }
for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loopIndex15)
{
var line = entry.content[_loopIndex15];
@@ -435,7 +414,7 @@
}
callback(null);
}
- loadFile(file).then(onLoaded, callback);
+ loadFile(file, onLoaded, callback);
},
writeToFile: function(file, data, callback)
{
@@ -447,7 +426,7 @@
{
saveFile(toFile, entry.content, callback);
}
- loadFile(file).then(onLoaded, callback);
+ loadFile(file, onLoaded, callback);
},
renameFile: function(fromFile, newName, callback)
{
@@ -458,7 +437,7 @@
ext.storage.set(keyPrefix + newName, entry, callback);
});
}
- loadFile(file).then(onLoaded, callback);
+ loadFile(file, onLoaded, callback);
},
removeFile: function(file, callback)
{
@@ -474,7 +453,7 @@
lastModified: entry.lastModified
});
}
- loadFile(file).then(onLoaded, callback);
+ loadFile(file, onLoaded, callback);
}
};
return exports;
« no previous file with comments | « background.html ('k') | qunit/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld