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

Side by Side Diff: lib/io.js

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

Powered by Google App Engine
This is Rietveld