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

Delta Between Two Patch Sets: lib/io.js

Issue 29367480: Issue 4721 - Use IndexedDB for storage in Edge (Closed)
Left Patch Set: Created Dec. 14, 2016, 1:48 a.m.
Right Patch Set: Address some nits Created Dec. 20, 2016, 2:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/localforage.min.js » ('j') | metadata.edge » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 var 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 var key = fileToKey(file); 27 let 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.
28 // Make sure we do not have subscriptions in localStorage from older versions first 28
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.
29 var entry = localStorage.getItem(key); 29 // Make sure we do not have subscriptions in localStorage from older
30 // versions first
31 let entry = localStorage.getItem(key);
30 if (typeof entry == "string") 32 if (typeof entry == "string")
31 { 33 {
32 try 34 try
33 { 35 {
34 entry = JSON.parse(entry); 36 entry = JSON.parse(entry);
35 } 37 }
36 catch(err) 38 catch(err)
37 { 39 {
38 setTimeout(errorCallback(new Error("File is corrupted"))); 40 setTimeout(errorCallback(new Error("File is corrupted")));
39 return; 41 return;
40 } 42 }
41 setTimeout(successCallback(entry)); 43 setTimeout(successCallback(entry));
42 return; 44 return;
43 } 45 }
44 // Now try to read from IndexedDB 46 // Now try to read from IndexedDB
45 localforage.getItem(key, function(err, value) 47 localforage.getItem(key, function(err, value)
46 { 48 {
47 if (err || !value) 49 if (err || !value)
48 errorCallback(new Error("File doesn't exist")); 50 errorCallback(new Error("File doesn't exist"));
49 else 51 else
50 successCallback(value); 52 successCallback(value);
51 }); 53 });
52 } 54 }
53 55
54 function saveFile(file, data, callback) 56 function saveFile(file, data, callback)
55 { 57 {
56 var key = fileToKey(file); 58 var key = fileToKey(file);
57 var entry = { 59 var entry = {
58 lastModified: Date.now(), 60 content: Array.from(data),
59 content: Array.from(data) 61 lastModified: Date.now()
Oleksandr 2016/12/14 01:55:55 Note: While the rest of this code is exactly the s
60 }; 62 };
61 63
62 localStorage.removeItem(key); 64 localStorage.removeItem(key);
63 localforage.setItem(key, entry, callback); 65 localforage.setItem(key, entry, callback);
64 } 66 }
65 exports.IO = { 67
68 exports.IO =
69 {
66 resolveFilePath: function(path) 70 resolveFilePath: function(path)
67 { 71 {
68 return new FakeFile(path); 72 return new FakeFile(path);
69 }, 73 },
74
70 readFromFile: function(file, listener, callback) 75 readFromFile: function(file, listener, callback)
71 { 76 {
72 function onLoaded(entry) 77 function onLoaded(entry)
73 { 78 {
74 if ("content" in entry) 79 if ("content" in entry)
75 { 80 {
76 for (var _loopIndex15 = 0; _loopIndex15 < entry.content.length; ++_loopI ndex15) 81 for (let line of entry.content)
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.
77 {
78 var line = entry.content[_loopIndex15];
79 listener.process(line); 82 listener.process(line);
80 }
81 } 83 }
82 listener.process(null); 84 listener.process(null);
83 callback(null); 85 callback(null);
84 } 86 }
87
85 loadFile(file, onLoaded, callback); 88 loadFile(file, onLoaded, callback);
86 }, 89 },
90
87 writeToFile: function(file, data, callback) 91 writeToFile: function(file, data, callback)
88 { 92 {
89 saveFile(file, data, callback); 93 saveFile(file, data, callback);
90 }, 94 },
95
91 copyFile: function(fromFile, toFile, callback) 96 copyFile: function(fromFile, toFile, callback)
92 { 97 {
93 function onLoaded(entry) 98 function onLoaded(entry)
94 { 99 {
95 saveFile(toFile, entry.content, callback); 100 saveFile(toFile, entry.content, callback);
96 } 101 }
97 loadFile(file, onLoaded, callback); 102
103 loadFile(fromFile, onLoaded, callback);
98 }, 104 },
105
99 renameFile: function(fromFile, newName, callback) 106 renameFile: function(fromFile, newName, callback)
100 { 107 {
101 function onLoaded() 108 function onLoaded()
102 { 109 {
103 ext.storage.remove(fileToKey(fromFile), function() 110 ext.storage.remove(fileToKey(fromFile), function()
104 { 111 {
105 ext.storage.set(keyPrefix + newName, entry, callback); 112 ext.storage.set(keyPrefix + newName, entry, callback);
106 }); 113 });
107 } 114 }
108 loadFile(file, onLoaded, callback); 115
116 loadFile(fromFile, onLoaded, callback);
109 }, 117 },
118
110 removeFile: function(file, callback) 119 removeFile: function(file, callback)
111 { 120 {
112 ext.storage.remove(fileToKey(file), callback); 121 ext.storage.remove(fileToKey(file), callback);
113 }, 122 },
123
114 statFile: function(file, callback) 124 statFile: function(file, callback)
115 { 125 {
116 function onLoaded(entry) 126 function onLoaded(entry)
117 { 127 {
118 callback(null, 128 callback(null, {
119 {
120 exists: true, 129 exists: true,
121 lastModified: entry.lastModified 130 lastModified: entry.lastModified
122 }); 131 });
123 } 132 }
133
124 loadFile(file, onLoaded, callback); 134 loadFile(file, onLoaded, callback);
125 } 135 }
126 }; 136 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld