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

Unified Diff: lib/io.js

Issue 29433625: Issue 5220 - Update the IO API (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created May 8, 2017, 2:14 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 | « dependencies ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
--- a/lib/io.js
+++ b/lib/io.js
@@ -21,99 +21,105 @@
const keyPrefix = "file:";
function fileToKey(file)
{
return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
Wladimir Palant 2017/05/09 09:12:27 Does this even work? The parameters passed to the
hub 2017/05/10 16:08:51 Looking at the code it seems that "file" is a file
}
-function loadFile(file, successCallback, errorCallback)
+function loadFile(file)
{
- let key = fileToKey(file);
-
- ext.storage.get([key], items =>
+ return new Promise((resolve, reject) =>
{
- let entry = items[key];
+ let key = fileToKey(file);
+
+ ext.storage.get([key], items =>
+ {
Wladimir Palant 2017/05/09 09:12:27 This should really be checking chrome.runtime.last
+ let entry = items[key];
- if (entry)
- successCallback(entry);
- else
- errorCallback(new Error("File doesn't exist"));
+ if (entry)
+ resolve(entry);
+ else
+ reject(new Error("File doesn't exist"));
+ });
});
}
-function saveFile(file, data, callback)
+function saveFile(file, data)
{
- ext.storage.set(
- fileToKey(file),
- {
- content: Array.from(data),
- lastModified: Date.now()
- },
- callback
- );
+ return new Promise((resolve) =>
Wladimir Palant 2017/05/09 09:12:27 I don't think that it is a good idea to omit the r
hub 2017/05/10 16:08:51 I'll put it back wherever. I it probably a better
Sebastian Noack 2017/05/22 13:36:30 Personally, I think I would have omitted it too, b
+ {
+ ext.storage.set(
+ fileToKey(file),
+ {
+ content: Array.from(data),
+ lastModified: Date.now()
+ },
+ resolve
+ );
+ });
}
exports.IO =
{
resolveFilePath(path) { return new FakeFile(path); },
Wladimir Palant 2017/05/09 09:12:27 This method should be removed, along with the Fake
hub 2017/05/10 16:08:51 Done.
- readFromFile(file, listener, callback)
+ readFromFile(file, parser)
Wladimir Palant 2017/05/09 09:12:27 I suggest that you copy JSDoc comments from https:
hub 2017/05/10 16:08:51 I'll change the parameters to match. And this reso
{
- function onLoaded(entry)
+ return loadFile(file).then(entry =>
{
for (let line of entry.content)
- listener.process(line);
+ parser(line);
- listener.process(null);
- callback(null);
- }
-
- loadFile(file, onLoaded, callback);
+ parser(null);
Wladimir Palant 2017/05/09 09:12:27 Please remove this call, it is no longer necessary
hub 2017/05/10 16:08:51 Done.
+ });
},
- writeToFile(file, data, callback)
+ writeToFile(file, data)
{
- saveFile(file, data, callback);
+ return saveFile(file, data);
},
- copyFile(fromFile, toFile, callback)
+ copyFile(fromFile, toFile)
{
- function onLoaded(entry)
+ return new Promise((resolve, reject) =>
{
- saveFile(toFile, entry.content, callback);
- }
-
- loadFile(fromFile, onLoaded, callback);
+ loadFile(fromFile).then(entry =>
+ {
+ saveFile(toFile, entry.content).then(resolve());
+ });
+ });
Wladimir Palant 2017/05/09 09:12:27 You don't need to create the promise explicitly. T
hub 2017/05/10 16:08:51 Acknowledged.
},
- renameFile(fromFile, newName, callback)
+ renameFile(fromFile, newName)
{
- function onLoaded(entry)
+ return new Promise(resolve =>
{
- ext.storage.remove(fileToKey(fromFile), () =>
+ loadFile(fromFile).then(entry =>
{
- ext.storage.set(keyPrefix + newName, entry, callback);
+ ext.storage.remove(fileToKey(fromFile), () =>
+ {
+ ext.storage.set(keyPrefix + newName, entry, resolve);
+ });
});
- }
-
- loadFile(fromFile, onLoaded, callback);
+ });
Wladimir Palant 2017/05/09 09:12:27 It's better to create the promises where they are
hub 2017/05/10 16:08:51 Make sense.
},
- removeFile(file, callback)
+ removeFile(file)
{
- ext.storage.remove(fileToKey(file), callback);
+ return new Promise(resolve =>
+ {
+ ext.storage.remove(fileToKey(file), resolve);
+ });
},
- statFile(file, callback)
+ statFile(file)
{
- function onLoaded(entry)
+ return loadFile(file).then((entry) =>
Wladimir Palant 2017/05/09 09:12:27 Nit: you are somewhat inconsistent, are you puttin
hub 2017/05/10 16:08:51 Acknowledged.
{
- callback(null, {
+ return {
exists: true,
lastModified: entry.lastModified
- });
- }
-
- loadFile(file, onLoaded, callback);
+ };
+ });
Wladimir Palant 2017/05/09 09:12:27 This promise will currently be rejected if the ent
hub 2017/05/10 16:08:51 Done.
}
};
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld