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

Delta Between Two Patch Sets: edge/ext/background.js

Issue 29367151: Issue 4721 - Use IndexedDB for storage in Edge (Closed)
Left Patch Set: Created Dec. 9, 2016, 6:38 a.m.
Right Patch Set: Use arrow functions. Remove code duplication. Address comments and nits. Created Dec. 13, 2016, 6:16 a.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 | edge/localforage.min.js » ('j') | no next file with change/comment »
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 (function() 18 (function()
19 { 19 {
20 /* Storage redirection to localforage */ 20 /*
kzar 2016/12/12 14:17:50 If you're going to add a comment here you might as
Oleksandr 2016/12/13 06:21:19 Done. There is no bug report in Microsoft Edge iss
21 * Storage redirection to localforage. In Windows 10 Anniversary Update
kzar 2016/12/13 10:24:04 The comment looks much more useful now thanks. Cou
22 * Microsoft Edge has a 1Mb limit on the amount of bytes per transaction in
23 * storage.local. Also unlimitedStorage permision is not honoured. Which is why
24 * we have to use IndexedDB until those issues are fixed in Creators Update.
25 * For reference it is supposed to be fixed in Windows 10 build 14986.
26 */
21 27
22 ext.storage = { 28 ext.storage = {
23 get: function(keys, callback) 29 get: function(keys, callback)
24 { 30 {
25 var promisedValues = keys.map(function(key) 31 var promisedValues = keys.map(key =>
kzar 2016/12/12 14:17:50 Why not just use localforage.getItem directly? Als
Oleksandr 2016/12/13 06:21:18 localForage.getItem can't handle the array of keys
kzar 2016/12/13 10:24:04 I know. I don't think I worded my suggestion very
26 { 32 {
27 return localforage.getItem(key); 33 return localforage.getItem(key);
28 }); 34 });
29 Promise.all(promisedValues).then(function(values) 35 Promise.all(promisedValues).then(values =>
30 { 36 {
31 if (!Array.isArray(keys)) 37 var items = {};
kzar 2016/12/12 14:17:50 If it's possible that keys isn't an array then wou
Oleksandr 2016/12/13 06:21:19 Fair enough. Removed this check.
kzar 2016/12/13 10:24:04 Well are you sure that keys will always be an Arra
38 for (let i = 0; i < keys.length; i++)
32 { 39 {
33 callback(values[0]); 40 if (values[i] != null)
34 return; 41 items[keys[i]] = values[i];
35 } 42 }
36 var items = {};
37 keys.forEach(function(key, idx)
kzar 2016/12/12 14:17:50 Would be clearer like this: for (let i = 0; i < k
Oleksandr 2016/12/13 06:21:19 Done. I thought functional way was less C++'y :)
kzar 2016/12/13 10:24:04 Thanks. (It's not functional to iteratively mutate
38 {
39 if (values[idx] != null)
40 items[key] = values[idx];
41 });
42 callback(items); 43 callback(items);
43 }).catch(function(err) 44 }).catch(err =>
kzar 2016/12/13 10:24:04 The indentation here for the catch is wrong. Also
44 { 45 {
45 callback(null); 46 callback(null);
46 }); 47 });
47 }, 48 },
48 set: function(key, value, callback) 49 set: function(key, value, callback)
49 { 50 {
50 localforage.getItem(key).then(function(oldValue) 51 function internalSet(oldValue)
kzar 2016/12/13 10:24:04 Nit: setThenDispatch or setThenCallback or somethi
51 { 52 {
52 localforage.setItem(key, value, callback); 53 localforage.setItem(key, value).then(() =>
kzar 2016/12/12 14:17:50 Shouldn't we wait until setItem finishes before di
Oleksandr 2016/12/13 06:21:19 Done.
54 {
55 if (callback)
56 callback(null);
57 var changes = {};
kzar 2016/12/13 10:24:04 Please move the `var changes = {};` line down, so
58 var change = changes[key] = {};
59 change.oldValue = oldValue;
60 change.newValue = value;
53 61
54 var changes = {}; 62 ext.storage.onChanged._dispatch(changes);
55 var change = changes[key] = {}; 63 }).catch((err) =>
56 change.oldValue = oldValue; 64 {
57 change.newValue = value; 65 if (callback)
58 66 callback(err);
59 ext.storage.onChanged._dispatch(changes); 67 });
60 }).catch(function(err) 68 };
61 { 69
62 localforage.setItem(key, value, callback); 70 localforage.getItem(key).then(oldValue => internalSet(oldValue))
kzar 2016/12/12 14:17:50 Seems ugly how this nearly identical logic is dupl
Oleksandr 2016/12/13 06:21:19 Done.
kzar 2016/12/13 10:24:04 No need to wrap internalSet with another function.
63 71 .catch(err => internalSet(null));
64 var changes = {};
65 var change = changes[key] = {};
66 change.oldValue = null;
67 change.newValue = value;
68
69 ext.storage.onChanged._dispatch(changes);
70 });
71 }, 72 },
72 remove: function(key, callback) 73 remove: function(key, callback)
73 { 74 {
74 localforage.removeItem(key); 75 localforage.removeItem(key, callback);
kzar 2016/12/12 14:17:50 What about the callback?
Oleksandr 2016/12/13 06:21:19 Done.
kzar 2016/12/13 10:24:04 Why wrap localforage.removeItem at all? How about
75 }, 76 },
76 onChanged: new ext._EventTarget() 77 onChanged: new ext._EventTarget()
77 }; 78 };
78
kzar 2016/12/12 14:17:50 Nit: No need for this blank line.
Oleksandr 2016/12/13 06:21:19 Done.
79 })(); 79 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld