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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /* global FakeFile */ 18 /* global FakeFile */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const keyPrefix = "file:"; 22 const keyPrefix = "file:";
23 23
24 function fileToKey(file) 24 function fileToKey(file)
25 { 25 {
26 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); 26 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
27 } 27 }
28 28
29 function loadFile(file, successCallback, errorCallback) 29 function loadFile(file)
30 { 30 {
31 let key = fileToKey(file); 31 return new Promise((resolve, reject) =>
32 {
33 let key = fileToKey(file);
32 34
33 ext.storage.get([key], items => 35 ext.storage.get([key], items =>
34 { 36 {
Wladimir Palant 2017/05/09 09:12:27 This should really be checking chrome.runtime.last
35 let entry = items[key]; 37 let entry = items[key];
36 38
37 if (entry) 39 if (entry)
38 successCallback(entry); 40 resolve(entry);
39 else 41 else
40 errorCallback(new Error("File doesn't exist")); 42 reject(new Error("File doesn't exist"));
43 });
41 }); 44 });
42 } 45 }
43 46
44 function saveFile(file, data, callback) 47 function saveFile(file, data)
45 { 48 {
46 ext.storage.set( 49 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
47 fileToKey(file), 50 {
48 { 51 ext.storage.set(
49 content: Array.from(data), 52 fileToKey(file),
50 lastModified: Date.now() 53 {
51 }, 54 content: Array.from(data),
52 callback 55 lastModified: Date.now()
53 ); 56 },
57 resolve
58 );
59 });
54 } 60 }
55 61
56 exports.IO = 62 exports.IO =
57 { 63 {
58 resolveFilePath(path) { return new FakeFile(path); }, 64 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.
59 65
60 readFromFile(file, listener, callback) 66 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
61 { 67 {
62 function onLoaded(entry) 68 return loadFile(file).then(entry =>
63 { 69 {
64 for (let line of entry.content) 70 for (let line of entry.content)
65 listener.process(line); 71 parser(line);
66 72
67 listener.process(null); 73 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.
68 callback(null); 74 });
69 }
70
71 loadFile(file, onLoaded, callback);
72 }, 75 },
73 76
74 writeToFile(file, data, callback) 77 writeToFile(file, data)
75 { 78 {
76 saveFile(file, data, callback); 79 return saveFile(file, data);
77 }, 80 },
78 81
79 copyFile(fromFile, toFile, callback) 82 copyFile(fromFile, toFile)
80 { 83 {
81 function onLoaded(entry) 84 return new Promise((resolve, reject) =>
82 { 85 {
83 saveFile(toFile, entry.content, callback); 86 loadFile(fromFile).then(entry =>
84 } 87 {
85 88 saveFile(toFile, entry.content).then(resolve());
86 loadFile(fromFile, onLoaded, callback); 89 });
90 });
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.
87 }, 91 },
88 92
89 renameFile(fromFile, newName, callback) 93 renameFile(fromFile, newName)
90 { 94 {
91 function onLoaded(entry) 95 return new Promise(resolve =>
92 { 96 {
93 ext.storage.remove(fileToKey(fromFile), () => 97 loadFile(fromFile).then(entry =>
94 { 98 {
95 ext.storage.set(keyPrefix + newName, entry, callback); 99 ext.storage.remove(fileToKey(fromFile), () =>
100 {
101 ext.storage.set(keyPrefix + newName, entry, resolve);
102 });
96 }); 103 });
97 } 104 });
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.
98
99 loadFile(fromFile, onLoaded, callback);
100 }, 105 },
101 106
102 removeFile(file, callback) 107 removeFile(file)
103 { 108 {
104 ext.storage.remove(fileToKey(file), callback); 109 return new Promise(resolve =>
110 {
111 ext.storage.remove(fileToKey(file), resolve);
112 });
105 }, 113 },
106 114
107 statFile(file, callback) 115 statFile(file)
108 { 116 {
109 function onLoaded(entry) 117 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.
110 { 118 {
111 callback(null, { 119 return {
112 exists: true, 120 exists: true,
113 lastModified: entry.lastModified 121 lastModified: entry.lastModified
114 }); 122 };
115 } 123 });
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.
116
117 loadFile(file, onLoaded, callback);
118 } 124 }
119 }; 125 };
OLDNEW
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld