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; | |
Sebastian Noack
2018/08/31 18:07:05
You can use default arguments. Also I would rather
geo
2018/09/04 16:12:08
Done.
| |
164 db = openDB(dbConfig); | |
165 | |
166 return db.catch(err => | |
167 { | |
168 if (retries == 10) | |
169 throw err; | |
170 | |
171 return reestablishConnection(dbInstance, ++retries); | |
172 }); | |
173 } | |
174 | |
160 function getFile(fileName, dbInstance, storeName) | 175 function getFile(fileName, dbInstance, storeName) |
161 { | 176 { |
177 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) | |
178 .then(indexedDBResult => | |
179 { | |
180 if (!indexedDBResult) | |
181 { | |
182 const {IndexedDBBackup} = require("./indexedDBBackup"); | |
Sebastian Noack
2018/08/31 18:07:05
Nit: Can this import be moved to the top of the mo
geo
2018/09/04 16:12:08
Yes, we end up with a circular dependency, as inde
| |
183 | |
184 return IndexedDBBackup.getBackupData() | |
185 .then(backupData => | |
186 saveFile( | |
187 { | |
188 fileName: fileToKey(fileName), | |
189 content: backupData.content, | |
190 lastModified: backupData.lastModified | |
191 }, | |
192 dbInstance, | |
193 storeName) | |
194 .then(() => backupData)); | |
Sebastian Noack
2018/08/31 18:07:05
Nit: The indentation is a little off here:
.the
geo
2018/09/04 16:12:08
I've changed a bit the indentation, hopefully it's
| |
195 } | |
196 return indexedDBResult; | |
197 }); | |
198 } | |
199 | |
200 function getFromIndexedDB(fileName, dbInstance, storeName) | |
201 { | |
162 return new Promise((resolve, reject) => | 202 return new Promise((resolve, reject) => |
163 { | 203 { |
164 let store = getObjectStore(dbInstance, storeName); | 204 let store = getObjectStore(dbInstance, storeName); |
165 let req = store.get(fileToKey(fileName)); | 205 let req = store.get(fileName); |
166 | 206 |
167 req.onsuccess = event => | 207 req.onsuccess = event => resolve(event.currentTarget.result); |
168 { | 208 req.onerror = event => reject(event.target.error); |
169 let {result} = event.currentTarget; | 209 }) |
170 | 210 .catch(error => |
171 if (result) | 211 { |
172 resolve(result); | 212 if (error.name == "UnknownError") |
173 else | 213 return reestablishConnection(dbInstance) |
174 reject({type: "NoSuchFile"}); | 214 .then(() => Promise.resolve()); |
175 }; | |
176 req.onerror = reject; | |
177 }); | 215 }); |
178 } | 216 } |
179 | 217 |
180 function saveFile(data, dbInstance, storeName) | 218 function saveFile(data, dbInstance, storeName) |
181 { | 219 { |
182 return new Promise((resolve, reject) => | 220 return new Promise((resolve, reject) => |
183 { | 221 { |
184 let store = getObjectStore(dbInstance, storeName); | 222 let store = getObjectStore(dbInstance, storeName); |
185 let req = store.put(data); | 223 let req = store.put(data); |
186 | 224 |
187 req.onsuccess = resolve; | 225 req.onsuccess = resolve; |
188 req.onerror = reject; | 226 req.onerror = event => reject(event.target.error); |
227 }) | |
228 .catch(error => | |
229 { | |
230 if (error.name == "UnknownError") | |
231 { | |
232 return reestablishConnection(dbInstance) | |
233 .then(newDbInstance => | |
234 saveFile(data, newDbInstance, storeName)); | |
Sebastian Noack
2018/08/31 18:07:05
Nit: For consistent with other code, can you inden
geo
2018/09/04 16:12:08
Done.
| |
235 } | |
189 }); | 236 }); |
190 } | 237 } |
191 | 238 |
192 function deleteFile(fileName, dbInstance, storeName) | 239 function deleteFile(fileName, dbInstance, storeName) |
193 { | 240 { |
194 return new Promise((resolve, reject) => | 241 return new Promise((resolve, reject) => |
195 { | 242 { |
196 let store = getObjectStore(dbInstance, storeName); | 243 let store = getObjectStore(dbInstance, storeName); |
197 let req = store.delete(fileToKey(fileName)); | 244 let req = store.delete(fileToKey(fileName)); |
198 | 245 |
199 req.onsuccess = resolve; | 246 req.onsuccess = resolve; |
200 req.onerror = reject; | 247 req.onerror = event => reject(event.target.error); |
248 }) | |
249 .catch(error => | |
250 { | |
251 if (error.name == "UnknownError") | |
252 return reestablishConnection(dbInstance); | |
201 }); | 253 }); |
202 } | 254 } |
203 | 255 |
204 exports.IO = | 256 exports.IO = |
205 { | 257 { |
206 /** | 258 /** |
207 * Writes text lines to a file. | 259 * Writes text lines to a file. |
208 * @param {string} fileName | 260 * @param {string} fileName |
209 * Name of the file to be written | 261 * Name of the file to be written |
210 * @param {Iterable.<string>} data | 262 * @param {Iterable.<string>} data |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 fileName: fileToKey(newName), | 346 fileName: fileToKey(newName), |
295 content: fileData.content, | 347 content: fileData.content, |
296 lastModified: fileData.lastModified | 348 lastModified: fileData.lastModified |
297 }, | 349 }, |
298 dbInstance, | 350 dbInstance, |
299 dbConfig.storeName)) | 351 dbConfig.storeName)) |
300 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); | 352 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); |
301 } | 353 } |
302 }; | 354 }; |
303 | 355 |
OLD | NEW |