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 = 10) | |
161 { | |
162 dbInstance.close(); | |
163 db = openDB(dbConfig); | |
164 | |
165 return db.catch(err => | |
166 { | |
167 if (!retries) | |
168 throw err; | |
169 | |
170 return reestablishConnection(dbInstance, --retries); | |
171 }); | |
172 } | |
173 | |
160 function getFile(fileName, dbInstance, storeName) | 174 function getFile(fileName, dbInstance, storeName) |
161 { | 175 { |
176 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) | |
177 .then(indexedDBResult => | |
178 { | |
179 if (!indexedDBResult) | |
180 { | |
181 const {IndexedDBBackup} = require("./indexedDBBackup"); | |
182 | |
183 return IndexedDBBackup.getBackupData() | |
184 .then(backupData => | |
185 saveFile( | |
186 { | |
187 fileName: fileToKey(fileName), | |
188 content: backupData.content, | |
189 lastModified: backupData.lastModified | |
190 }, | |
191 dbInstance, | |
192 storeName).then(() => backupData) | |
193 ); | |
194 } | |
195 return indexedDBResult; | |
196 }); | |
197 } | |
198 | |
199 function getFromIndexedDB(fileName, dbInstance, storeName) | |
200 { | |
162 return new Promise((resolve, reject) => | 201 return new Promise((resolve, reject) => |
163 { | 202 { |
164 let store = getObjectStore(dbInstance, storeName); | 203 let store = getObjectStore(dbInstance, storeName); |
165 let req = store.get(fileToKey(fileName)); | 204 let req = store.get(fileName); |
166 | 205 |
167 req.onsuccess = event => | 206 req.onsuccess = event => resolve(event.currentTarget.result); |
168 { | 207 req.onerror = event => reject(event.target.error); |
169 let {result} = event.currentTarget; | 208 }) |
170 | 209 .catch(error => |
171 if (result) | 210 { |
172 resolve(result); | 211 if (error.name == "UnknownError") |
173 else | 212 return reestablishConnection(dbInstance) |
174 reject({type: "NoSuchFile"}); | 213 .then(() => undefined); |
Sebastian Noack
2018/09/04 21:00:01
Nit: Wrapping here seems unnecessary. It appears t
geo
2018/09/05 14:04:34
Done.
| |
175 }; | |
176 req.onerror = reject; | |
177 }); | 214 }); |
178 } | 215 } |
179 | 216 |
180 function saveFile(data, dbInstance, storeName) | 217 function saveFile(data, dbInstance, storeName) |
181 { | 218 { |
182 return new Promise((resolve, reject) => | 219 return new Promise((resolve, reject) => |
183 { | 220 { |
184 let store = getObjectStore(dbInstance, storeName); | 221 let store = getObjectStore(dbInstance, storeName); |
185 let req = store.put(data); | 222 let req = store.put(data); |
186 | 223 |
187 req.onsuccess = resolve; | 224 req.onsuccess = resolve; |
188 req.onerror = reject; | 225 req.onerror = event => reject(event.target.error); |
226 }) | |
227 .catch(error => | |
228 { | |
229 if (error.name == "UnknownError") | |
230 { | |
231 return reestablishConnection(dbInstance).then(newDbInstance => | |
232 saveFile(data, newDbInstance, storeName) | |
233 ); | |
234 } | |
189 }); | 235 }); |
190 } | 236 } |
191 | 237 |
192 function deleteFile(fileName, dbInstance, storeName) | 238 function deleteFile(fileName, dbInstance, storeName) |
193 { | 239 { |
194 return new Promise((resolve, reject) => | 240 return new Promise((resolve, reject) => |
195 { | 241 { |
196 let store = getObjectStore(dbInstance, storeName); | 242 let store = getObjectStore(dbInstance, storeName); |
197 let req = store.delete(fileToKey(fileName)); | 243 let req = store.delete(fileToKey(fileName)); |
198 | 244 |
199 req.onsuccess = resolve; | 245 req.onsuccess = resolve; |
200 req.onerror = reject; | 246 req.onerror = event => reject(event.target.error); |
247 }) | |
248 .catch(error => | |
249 { | |
250 if (error.name == "UnknownError") | |
251 return reestablishConnection(dbInstance); | |
201 }); | 252 }); |
202 } | 253 } |
203 | 254 |
204 exports.IO = | 255 exports.IO = |
205 { | 256 { |
206 /** | 257 /** |
207 * Writes text lines to a file. | 258 * Writes text lines to a file. |
208 * @param {string} fileName | 259 * @param {string} fileName |
209 * Name of the file to be written | 260 * Name of the file to be written |
210 * @param {Iterable.<string>} data | 261 * @param {Iterable.<string>} data |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 fileName: fileToKey(newName), | 345 fileName: fileToKey(newName), |
295 content: fileData.content, | 346 content: fileData.content, |
296 lastModified: fileData.lastModified | 347 lastModified: fileData.lastModified |
297 }, | 348 }, |
298 dbInstance, | 349 dbInstance, |
299 dbConfig.storeName)) | 350 dbConfig.storeName)) |
300 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); | 351 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); |
301 } | 352 } |
302 }; | 353 }; |
303 | 354 |
OLD | NEW |