| Index: lib/io.js |
| =================================================================== |
| --- a/lib/io.js |
| +++ b/lib/io.js |
| @@ -15,7 +15,7 @@ |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| -const keyPrefix = "file:"; |
| +var keyPrefix = "file:"; |
| function fileToKey(file) |
| { |
| @@ -24,67 +24,78 @@ |
| function loadFile(file, successCallback, errorCallback) |
| { |
| - let key = fileToKey(file); |
| - |
| - ext.storage.get([key], function(items) |
| + var key = fileToKey(file); |
|
kzar
2016/12/16 13:22:25
Any reason you've changed `let` and `const` to `va
Oleksandr
2016/12/19 11:03:14
Done.
|
| + // Make sure we do not have subscriptions in localStorage from older versions first |
|
kzar
2016/12/16 13:22:25
Nit: This comment is too long, mind wrapping the l
Oleksandr
2016/12/19 11:03:14
Done.
|
| + var entry = localStorage.getItem(key); |
| + if (typeof entry == "string") |
| { |
| - let entry = items[key]; |
| - |
| - if (entry) |
| - successCallback(entry); |
| + try |
| + { |
| + entry = JSON.parse(entry); |
| + } |
| + catch(err) |
| + { |
| + setTimeout(errorCallback(new Error("File is corrupted"))); |
| + return; |
| + } |
| + setTimeout(successCallback(entry)); |
| + return; |
| + } |
| + // Now try to read from IndexedDB |
| + localforage.getItem(key, function(err, value) |
| + { |
| + if (err || !value) |
| + errorCallback(new Error("File doesn't exist")); |
| else |
| - errorCallback(new Error("File doesn't exist")); |
| + successCallback(value); |
| }); |
| } |
| function saveFile(file, data, callback) |
| { |
| - ext.storage.set( |
| - fileToKey(file), |
| - { |
| - content: Array.from(data), |
| - lastModified: Date.now() |
| - }, |
| - callback |
| - ); |
| + var key = fileToKey(file); |
| + var entry = { |
| + lastModified: Date.now(), |
| + content: Array.from(data) |
|
Oleksandr
2016/12/14 01:55:55
Note: While the rest of this code is exactly the s
|
| + }; |
| + |
| + localStorage.removeItem(key); |
| + localforage.setItem(key, entry, callback); |
| } |
| - |
| -exports.IO = |
| -{ |
| +exports.IO = { |
| resolveFilePath: function(path) |
| { |
| return new FakeFile(path); |
| }, |
| - |
| readFromFile: function(file, listener, callback) |
| { |
| function onLoaded(entry) |
| { |
| - for (let line of entry.content) |
| - listener.process(line); |
| - |
| + if ("content" in entry) |
| + { |
| + for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loopIndex15) |
|
kzar
2016/12/16 13:22:25
What's the point of this change to the loop? Seems
Oleksandr
2016/12/19 11:03:14
Done.
|
| + { |
| + var line = entry.content[_loopIndex15]; |
| + listener.process(line); |
| + } |
| + } |
| listener.process(null); |
| callback(null); |
| } |
| - |
| loadFile(file, onLoaded, callback); |
| }, |
| - |
| writeToFile: function(file, data, callback) |
| { |
| saveFile(file, data, callback); |
| }, |
| - |
|
kzar
2016/12/16 13:22:25
Mind going through the diff and fixing pointless c
Oleksandr
2016/12/19 11:03:14
Done.
|
| copyFile: function(fromFile, toFile, callback) |
| { |
| function onLoaded(entry) |
| { |
| saveFile(toFile, entry.content, callback); |
| } |
| - |
| - loadFile(fromFile, onLoaded, callback); |
| + loadFile(file, onLoaded, callback); |
| }, |
| - |
| renameFile: function(fromFile, newName, callback) |
| { |
| function onLoaded() |
| @@ -94,25 +105,22 @@ |
| ext.storage.set(keyPrefix + newName, entry, callback); |
| }); |
| } |
| - |
| - loadFile(fromFile, onLoaded, callback); |
| + loadFile(file, onLoaded, callback); |
| }, |
| - |
| removeFile: function(file, callback) |
| { |
| ext.storage.remove(fileToKey(file), callback); |
| }, |
| - |
| statFile: function(file, callback) |
| { |
| function onLoaded(entry) |
| { |
| - callback(null, { |
| + callback(null, |
| + { |
| exists: true, |
| lastModified: entry.lastModified |
| }); |
| } |
| - |
| loadFile(file, onLoaded, callback); |
| } |
| }; |