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

Side by Side Diff: lib/io.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Patch Set: Created Jan. 13, 2017, 12:11 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
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 const keyPrefix = "file:"; 18 const keyPrefix = "file:";
19 19
20 function fileToKey(file) 20 function fileToKey(file)
21 { 21 {
22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
23 } 23 }
24 24
25 function loadFile(file, successCallback, errorCallback) 25 function loadFile(file, successCallback, errorCallback)
26 { 26 {
27 let key = fileToKey(file); 27 let key = fileToKey(file);
28 28
29 ext.storage.get([key], function(items) 29 ext.storage.get([key], items =>
30 { 30 {
31 let entry = items[key]; 31 let entry = items[key];
32 32
33 if (entry) 33 if (entry)
34 successCallback(entry); 34 successCallback(entry);
35 else 35 else
36 errorCallback(new Error("File doesn't exist")); 36 errorCallback(new Error("File doesn't exist"));
37 }); 37 });
38 } 38 }
39 39
40 function saveFile(file, data, callback) 40 function saveFile(file, data, callback)
41 { 41 {
42 ext.storage.set( 42 ext.storage.set(
43 fileToKey(file), 43 fileToKey(file),
44 { 44 {
45 content: Array.from(data), 45 content: Array.from(data),
46 lastModified: Date.now() 46 lastModified: Date.now()
47 }, 47 },
48 callback 48 callback
49 ); 49 );
50 } 50 }
51 51
52 exports.IO = 52 exports.IO =
53 { 53 {
54 resolveFilePath: function(path) 54 resolveFilePath: path => new FakeFile(path),
55 {
56 return new FakeFile(path);
57 },
58 55
59 readFromFile: function(file, listener, callback) 56 readFromFile: (file, listener, callback) =>
60 { 57 {
61 function onLoaded(entry) 58 function onLoaded(entry)
62 { 59 {
63 for (let line of entry.content) 60 for (let line of entry.content)
64 listener.process(line); 61 listener.process(line);
65 62
66 listener.process(null); 63 listener.process(null);
67 callback(null); 64 callback(null);
68 } 65 }
69 66
70 loadFile(file, onLoaded, callback); 67 loadFile(file, onLoaded, callback);
71 }, 68 },
72 69
73 writeToFile: function(file, data, callback) 70 writeToFile: (file, data, callback) =>
74 { 71 {
75 saveFile(file, data, callback); 72 saveFile(file, data, callback);
76 }, 73 },
77 74
78 copyFile: function(fromFile, toFile, callback) 75 copyFile: (fromFile, toFile, callback) =>
79 { 76 {
80 function onLoaded(entry) 77 function onLoaded(entry)
81 { 78 {
82 saveFile(toFile, entry.content, callback); 79 saveFile(toFile, entry.content, callback);
83 } 80 }
84 81
85 loadFile(fromFile, onLoaded, callback); 82 loadFile(fromFile, onLoaded, callback);
86 }, 83 },
87 84
88 renameFile: function(fromFile, newName, callback) 85 renameFile: (fromFile, newName, callback) =>
89 { 86 {
90 function onLoaded(entry) 87 function onLoaded(entry)
91 { 88 {
92 ext.storage.remove(fileToKey(fromFile), function() 89 ext.storage.remove(fileToKey(fromFile), () =>
93 { 90 {
94 ext.storage.set(keyPrefix + newName, entry, callback); 91 ext.storage.set(keyPrefix + newName, entry, callback);
95 }); 92 });
96 } 93 }
97 94
98 loadFile(fromFile, onLoaded, callback); 95 loadFile(fromFile, onLoaded, callback);
99 }, 96 },
100 97
101 removeFile: function(file, callback) 98 removeFile: (file, callback) =>
102 { 99 {
103 ext.storage.remove(fileToKey(file), callback); 100 ext.storage.remove(fileToKey(file), callback);
104 }, 101 },
105 102
106 statFile: function(file, callback) 103 statFile: (file, callback) =>
107 { 104 {
108 function onLoaded(entry) 105 function onLoaded(entry)
109 { 106 {
110 callback(null, { 107 callback(null, {
111 exists: true, 108 exists: true,
112 lastModified: entry.lastModified 109 lastModified: entry.lastModified
113 }); 110 });
114 } 111 }
115 112
116 loadFile(file, onLoaded, callback); 113 loadFile(file, onLoaded, callback);
117 } 114 }
118 }; 115 };
OLDNEW
« chrome/ext/common.js ('K') | « lib/icon.js ('k') | lib/notificationHelper.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld