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: Fix the IO implementation based on feedback. Created May 10, 2017, 4:08 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 */
19
20 "use strict"; 18 "use strict";
21 19
22 const keyPrefix = "file:"; 20 const keyPrefix = "file:";
23 21
24 function fileToKey(file) 22 function fileToKey(fileName)
25 { 23 {
26 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); 24 return keyPrefix + fileName;
27 } 25 }
28 26
29 function loadFile(file, successCallback, errorCallback) 27 function loadFile(fileName)
30 { 28 {
31 let key = fileToKey(file); 29 return new Promise((resolve, reject) =>
30 {
31 let key = fileToKey(fileName);
32 32
33 ext.storage.get([key], items => 33 ext.storage.get(key, items =>
34 { 34 {
35 let entry = items[key]; 35 let entry = items[key];
36 36
37 if (entry) 37 if (entry)
38 successCallback(entry); 38 resolve(entry);
39 else 39 else
40 errorCallback(new Error("File doesn't exist")); 40 reject({type: "NoSuchFile"});
41 });
41 }); 42 });
42 } 43 }
43 44
44 function saveFile(file, data, callback) 45 function saveFile(fileName, data)
45 { 46 {
46 ext.storage.set( 47 return new Promise((resolve, reject) =>
47 fileToKey(file), 48 {
48 { 49 ext.storage.set(
49 content: Array.from(data), 50 fileToKey(fileName),
50 lastModified: Date.now() 51 {
51 }, 52 content: Array.from(data),
52 callback 53 lastModified: Date.now()
53 ); 54 },
55 resolve
56 );
57 });
58 }
59
60 function removeFile(fileName)
61 {
62 return new Promise((resolve, reject) =>
63 {
64 ext.storage.remove(fileToKey(fileName), resolve);
65 });
54 } 66 }
55 67
56 exports.IO = 68 exports.IO =
57 { 69 {
58 resolveFilePath(path) { return new FakeFile(path); }, 70 /**
59 71 * Reads text lines from a file.
60 readFromFile(file, listener, callback) 72 * @param {string} fileName
73 * Name of the file to be read
Sebastian Noack 2017/05/22 13:36:30 Nit: This is inconsistent with how we wrap documen
Wladimir Palant 2017/05/22 13:46:31 Yes, I am aware of that - and wrapping it this way
Sebastian Noack 2017/05/22 13:49:52 Adding Dave for a third opinion.
kzar 2017/05/22 14:00:41 Well IIRC I've done it both ways depending on the
hub 2017/05/23 12:31:50 I copied (and proofed) these documentation comment
74 * @param {TextSink} listener
75 * Function that will be called for each line in the file
76 * @return {Promise}
77 * Promise to be resolved or rejected once the operation is completed
78 */
79 readFromFile(fileName, listener)
61 { 80 {
62 function onLoaded(entry) 81 return loadFile(fileName).then(entry =>
63 { 82 {
64 for (let line of entry.content) 83 for (let line of entry.content)
65 listener.process(line); 84 listener(line);
Sebastian Noack 2017/05/22 13:36:30 Nit: Please use 2 (not 4) space indentation. Howev
Wladimir Palant 2017/05/22 13:46:31 This comment doesn't appear to match the line it h
Sebastian Noack 2017/05/22 13:49:52 Sorry, I misread the code. For some reason I didn'
66 85 });
67 listener.process(null);
68 callback(null);
69 }
70
71 loadFile(file, onLoaded, callback);
72 }, 86 },
73 87
74 writeToFile(file, data, callback) 88 /**
89 * Writes text lines to a file.
90 * @param {string} fileName
91 * Name of the file to be written
92 * @param {Iterable.<string>} data
93 * An array-like or iterable object containing the lines (without line
94 * endings)
95 * @return {Promise}
96 * Promise to be resolved or rejected once the operation is completed
97 */
98 writeToFile(fileName, data)
75 { 99 {
76 saveFile(file, data, callback); 100 return saveFile(fileName, data);
77 }, 101 },
78 102
79 copyFile(fromFile, toFile, callback) 103 /**
104 * Copies a file.
105 * @param {string} fromFile
106 * Name of the file to be copied
107 * @param {string} toFile
108 * Name of the file to be written, will be overwritten if exists
109 * @return {Promise}
110 * Promise to be resolved or rejected once the operation is completed
111 */
112 copyFile(fromFile, toFile)
80 { 113 {
81 function onLoaded(entry) 114 return loadFile(fromFile).then(entry =>
82 { 115 {
83 saveFile(toFile, entry.content, callback); 116 return saveFile(toFile, entry.content).then(resolve());
84 } 117 });
85
86 loadFile(fromFile, onLoaded, callback);
87 }, 118 },
88 119
89 renameFile(fromFile, newName, callback) 120 /**
121 * Renames a file.
122 * @param {string} fromFile
123 * Name of the file to be renamed
124 * @param {string} newName
125 * New file name, will be overwritten if exists
126 * @return {Promise}
127 * Promise to be resolved or rejected once the operation is completed
128 */
129 renameFile(fromFile, newName)
90 { 130 {
91 function onLoaded(entry) 131 return loadFile(fromFile).then(entry =>
92 { 132 {
93 ext.storage.remove(fileToKey(fromFile), () => 133 ext.storage.set(fileToKey(newName), entry, resolve);
94 { 134 }).then(() =>
95 ext.storage.set(keyPrefix + newName, entry, callback); 135 {
Sebastian Noack 2017/05/22 13:36:30 The parentheses + return is redundant here.
hub 2017/05/23 12:31:50 Done.
96 }); 136 return removeFile(fromFile);
97 } 137 });
98
99 loadFile(fromFile, onLoaded, callback);
100 }, 138 },
101 139
102 removeFile(file, callback) 140 /**
141 * Removes a file.
142 * @param {string} fileName
143 * Name of the file to be removed
144 * @return {Promise}
145 * Promise to be resolved or rejected once the operation is completed
146 */
147 removeFile(fileName)
103 { 148 {
104 ext.storage.remove(fileToKey(file), callback); 149 return removeFile(fileName);
105 }, 150 },
106 151
107 statFile(file, callback) 152 /**
153 * Retrieves file metadata.
154 * @param {string} fileName
155 * Name of the file to be looked up
156 * @return {Promise.<StatData>}
157 * Promise to be resolved with file metadata once the operation is
158 * completed
159 */
160 statFile(fileName)
108 { 161 {
109 function onLoaded(entry) 162 return loadFile(fileName).then(entry =>
110 { 163 {
111 callback(null, { 164 return {
112 exists: true, 165 exists: true,
113 lastModified: entry.lastModified 166 lastModified: entry.lastModified
114 }); 167 };
115 } 168 }).catch(error =>
116 169 {
117 loadFile(file, onLoaded, callback); 170 if (error.type == "NoSuchFile")
171 return {exists: false};
172 throw error;
173 });
118 } 174 }
119 }; 175 };
OLDNEW
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld