| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 function fileToKey(fileName) | 22 function fileToKey(fileName) |
| 23 { | 23 { |
| 24 return keyPrefix + fileName; | 24 return keyPrefix + fileName; |
| 25 } | 25 } |
| 26 | 26 |
| 27 function loadFile(fileName) | 27 function loadFile(fileName) |
| 28 { | 28 { |
| 29 return new Promise((resolve, reject) => | 29 return new Promise((resolve, reject) => |
| 30 { | 30 { |
| 31 let key = fileToKey(fileName); | 31 localforage.getItem(fileToKey(fileName), (err, value) => |
| 32 | |
| 33 // Make sure we do not have subscriptions in localStorage from older | |
|
Sebastian Noack
2017/08/24 12:11:46
We use IndexedDB for quite a while, now. So I gues
Oleksandr
2017/08/24 14:43:58
Fair enough.
| |
| 34 // versions first | |
| 35 let entry = localStorage.getItem(key); | |
| 36 if (typeof entry == "string") | |
| 37 { | 32 { |
| 38 try | 33 if (err || value == null) |
| 39 { | |
| 40 entry = JSON.parse(entry); | |
| 41 } | |
| 42 catch (err) | |
| 43 { | |
| 44 reject({type: "NoSuchFile"}); | |
| 45 return; | |
| 46 } | |
| 47 resolve(entry); | |
| 48 return; | |
| 49 } | |
| 50 // Now try to read from IndexedDB | |
| 51 localforage.getItem(key, (err, value) => | |
| 52 { | |
| 53 if (err || !value) | |
|
Sebastian Noack
2017/08/24 12:11:46
Any particular reason, we check for `!value`? It t
Oleksandr
2017/08/24 14:43:58
Based on https://localforage.github.io/localForage
Sebastian Noack
2017/08/24 14:56:27
Based on that information we might want to explici
Oleksandr
2017/08/24 16:53:29
Done.
| |
| 54 reject({type: "NoSuchFile"}); | 34 reject({type: "NoSuchFile"}); |
| 55 else | 35 else |
| 56 resolve(value); | 36 resolve(value); |
| 57 }); | 37 }); |
| 58 }); | 38 }); |
| 59 } | 39 } |
| 60 | 40 |
| 61 function saveFile(fileName, data) | 41 function saveFile(fileName, data) |
| 62 { | 42 { |
| 63 let key = fileToKey(fileName); | 43 let key = fileToKey(fileName); |
| 64 localStorage.removeItem(key); | |
| 65 return localforage.setItem(key, { | 44 return localforage.setItem(key, { |
| 66 content: Array.from(data), | 45 content: Array.from(data), |
| 67 lastModified: Date.now() | 46 lastModified: Date.now() |
| 68 }); | 47 }); |
| 69 } | 48 } |
| 70 | 49 |
| 71 function removeFile(fileName) | 50 function removeFile(fileName) |
| 72 { | 51 { |
| 73 return new Promise((resolve, reject) => | 52 return new Promise((resolve, reject) => |
| 74 { | 53 { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 90 * Name of the file to be read | 69 * Name of the file to be read |
| 91 * @param {TextSink} listener | 70 * @param {TextSink} listener |
| 92 * Function that will be called for each line in the file | 71 * Function that will be called for each line in the file |
| 93 * @return {Promise} | 72 * @return {Promise} |
| 94 * Promise to be resolved or rejected once the operation is completed | 73 * Promise to be resolved or rejected once the operation is completed |
| 95 */ | 74 */ |
| 96 readFromFile(fileName, listener) | 75 readFromFile(fileName, listener) |
| 97 { | 76 { |
| 98 return loadFile(fileName).then(entry => | 77 return loadFile(fileName).then(entry => |
| 99 { | 78 { |
| 100 if ("content" in entry) | 79 for (let line of entry.content) |
|
Sebastian Noack
2017/08/24 12:11:46
Why is this check necessary?
Oleksandr
2017/08/24 14:43:58
If content is undefined (for whatever reason) than
Sebastian Noack
2017/08/24 14:56:27
This theoretical scenario isn't handled in upstrea
Oleksandr
2017/08/24 16:53:29
Removed the check.
| |
| 101 { | 80 listener(line); |
| 102 for (let line of entry.content) | |
| 103 listener(line); | |
| 104 } | |
| 105 }); | 81 }); |
| 106 }, | 82 }, |
| 107 | 83 |
| 108 /** | 84 /** |
| 109 * Writes text lines to a file. | 85 * Writes text lines to a file. |
| 110 * @param {string} fileName | 86 * @param {string} fileName |
| 111 * Name of the file to be written | 87 * Name of the file to be written |
| 112 * @param {Iterable.<string>} data | 88 * @param {Iterable.<string>} data |
| 113 * An array-like or iterable object containing the lines (without line | 89 * An array-like or iterable object containing the lines (without line |
| 114 * endings) | 90 * endings) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 lastModified: entry.lastModified | 156 lastModified: entry.lastModified |
| 181 }; | 157 }; |
| 182 }).catch(error => | 158 }).catch(error => |
| 183 { | 159 { |
| 184 if (error.type == "NoSuchFile") | 160 if (error.type == "NoSuchFile") |
| 185 return {exists: false}; | 161 return {exists: false}; |
| 186 throw error; | 162 throw error; |
| 187 }); | 163 }); |
| 188 } | 164 } |
| 189 }; | 165 }; |
| LEFT | RIGHT |