Left: | ||
Right: |
OLD | NEW |
---|---|
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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 }; | 150 }; |
151 } | 151 } |
152 | 152 |
153 function getObjectStore(dbInstance, storeName) | 153 function getObjectStore(dbInstance, storeName) |
154 { | 154 { |
155 return dbInstance | 155 return dbInstance |
156 .transaction([storeName], IDBTransaction.READ_WRITE) | 156 .transaction([storeName], IDBTransaction.READ_WRITE) |
157 .objectStore(storeName); | 157 .objectStore(storeName); |
158 } | 158 } |
159 | 159 |
160 function reestablishConnection(dbInstance, retries) | |
161 { | |
162 dbInstance.close(); | |
163 retries = retries || 1; | |
164 | |
165 return new Promise((resolve) => | |
166 { | |
167 setTimeout(() => | |
168 { | |
169 db = openDB(dbConfig); | |
170 return resolve(db); | |
171 }, 0); | |
Sebastian Noack
2018/08/21 20:26:25
Is this timeout necessary?
geo
2018/08/31 15:49:25
This part here is mostly from experimentation. If
Sebastian Noack
2018/08/31 18:07:04
Ok, let's stick with the delay then.
| |
172 }) | |
173 .catch(err => | |
174 { | |
175 if (retries == 10) | |
176 return Promise.reject(err); | |
Sebastian Noack
2018/08/21 20:26:25
Nit: You can just throw the error in here. This wi
geo
2018/08/31 15:49:24
Done.
| |
177 | |
178 return reestablishConnection(dbInstance, ++retries); | |
179 }); | |
180 } | |
181 | |
160 function getFile(fileName, dbInstance, storeName) | 182 function getFile(fileName, dbInstance, storeName) |
161 { | 183 { |
184 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) | |
185 .then(indexedDBResult => | |
186 { | |
187 if (!indexedDBResult) | |
188 { | |
189 const {IndexedDBBackup} = require("./indexedDBBackup"); | |
190 | |
191 return IndexedDBBackup.getBackupData() | |
Sebastian Noack
2018/08/21 20:26:25
I wonder whether we also have to trigger the subsc
geo
2018/08/31 15:49:24
No need with the information we are currently savi
| |
192 .then(backupData => | |
193 saveFile( | |
194 { | |
195 fileName: fileToKey(fileName), | |
196 content: backupData.content, | |
197 lastModified: backupData.lastModified | |
198 }, | |
199 dbInstance, | |
200 storeName) | |
201 .then(() => backupData)); | |
202 } | |
203 return indexedDBResult; | |
204 }); | |
205 } | |
206 | |
207 function getFromIndexedDB(fileName, dbInstance, storeName) | |
208 { | |
162 return new Promise((resolve, reject) => | 209 return new Promise((resolve, reject) => |
163 { | 210 { |
164 let store = getObjectStore(dbInstance, storeName); | 211 let store = getObjectStore(dbInstance, storeName); |
165 let req = store.get(fileToKey(fileName)); | 212 let req = store.get(fileName); |
166 | 213 |
167 req.onsuccess = event => | 214 req.onsuccess = event => resolve(event.currentTarget.result); |
168 { | 215 req.onerror = event => reject(event.target.error); |
169 let {result} = event.currentTarget; | 216 }) |
170 | 217 .catch(error => |
171 if (result) | 218 { |
172 resolve(result); | 219 if (error.name == "UnknownError") |
173 else | 220 return reestablishConnection(dbInstance) |
174 reject({type: "NoSuchFile"}); | 221 .then(() => Promise.resolve()); |
Sebastian Noack
2018/08/21 20:26:25
Nit: This seems redundant.
geo
2018/08/31 15:49:25
I want this to resolve with `undefined` so that `g
Sebastian Noack
2018/08/31 18:07:04
I see, then this should be:
.then(() => undefin
geo
2018/09/04 16:12:07
Done.
| |
175 }; | 222 return Promise.reject(error); |
Sebastian Noack
2018/08/21 20:26:25
Nit: See above.
geo
2018/08/31 15:49:25
Done.
| |
176 req.onerror = reject; | |
177 }); | 223 }); |
178 } | 224 } |
179 | 225 |
180 function saveFile(data, dbInstance, storeName) | 226 function saveFile(data, dbInstance, storeName) |
181 { | 227 { |
182 return new Promise((resolve, reject) => | 228 return new Promise((resolve, reject) => |
183 { | 229 { |
184 let store = getObjectStore(dbInstance, storeName); | 230 let store = getObjectStore(dbInstance, storeName); |
185 let req = store.put(data); | 231 let req = store.put(data); |
186 | 232 |
187 req.onsuccess = resolve; | 233 req.onsuccess = resolve; |
188 req.onerror = reject; | 234 req.onerror = event => reject(event.target.error); |
235 }) | |
236 .catch(error => | |
237 { | |
238 if (error.name == "UnknownError") | |
239 { | |
240 return reestablishConnection(dbInstance) | |
241 .then(newDbInstance => | |
242 saveFile(data, newDbInstance, storeName)); | |
243 } | |
244 return Promise.reject(error); | |
189 }); | 245 }); |
190 } | 246 } |
191 | 247 |
192 function deleteFile(fileName, dbInstance, storeName) | 248 function deleteFile(fileName, dbInstance, storeName) |
193 { | 249 { |
194 return new Promise((resolve, reject) => | 250 return new Promise((resolve, reject) => |
195 { | 251 { |
196 let store = getObjectStore(dbInstance, storeName); | 252 let store = getObjectStore(dbInstance, storeName); |
197 let req = store.delete(fileToKey(fileName)); | 253 let req = store.delete(fileToKey(fileName)); |
198 | 254 |
199 req.onsuccess = resolve; | 255 req.onsuccess = resolve; |
200 req.onerror = reject; | 256 req.onerror = event => reject(event.target.error); |
257 }) | |
258 .catch(error => | |
259 { | |
260 if (error.name == "UnknownError") | |
261 { | |
262 return reestablishConnection(dbInstance) | |
263 .then(() => Promise.resolve()); | |
Sebastian Noack
2018/08/21 20:26:25
Nit: This seems redundant.
geo
2018/08/31 15:49:24
Done.
| |
264 } | |
265 | |
266 return Promise.reject(error); | |
Sebastian Noack
2018/08/21 20:26:25
Nit: See above.
geo
2018/08/31 15:49:25
Done.
| |
201 }); | 267 }); |
202 } | 268 } |
203 | 269 |
204 exports.IO = | 270 exports.IO = |
205 { | 271 { |
206 /** | 272 /** |
207 * Writes text lines to a file. | 273 * Writes text lines to a file. |
208 * @param {string} fileName | 274 * @param {string} fileName |
209 * Name of the file to be written | 275 * Name of the file to be written |
210 * @param {Iterable.<string>} data | 276 * @param {Iterable.<string>} data |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 fileName: fileToKey(newName), | 360 fileName: fileToKey(newName), |
295 content: fileData.content, | 361 content: fileData.content, |
296 lastModified: fileData.lastModified | 362 lastModified: fileData.lastModified |
297 }, | 363 }, |
298 dbInstance, | 364 dbInstance, |
299 dbConfig.storeName)) | 365 dbConfig.storeName)) |
300 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); | 366 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); |
301 } | 367 } |
302 }; | 368 }; |
303 | 369 |
OLD | NEW |