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 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) | 160 function reestablishConnection(dbInstance, retries = 10) |
161 { | 161 { |
162 dbInstance.close(); | 162 dbInstance.close(); |
163 retries = retries || 1; | 163 db = openDB(dbConfig); |
164 | 164 |
165 return new Promise((resolve) => | 165 return db.catch(err => |
166 { | 166 { |
167 setTimeout(() => | 167 if (!retries) |
168 { | 168 throw err; |
169 db = openDB(dbConfig); | 169 |
170 return resolve(db); | 170 return reestablishConnection(dbInstance, --retries); |
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 }); | 171 }); |
180 } | 172 } |
181 | 173 |
182 function getFile(fileName, dbInstance, storeName) | 174 function getFile(fileName, dbInstance, storeName) |
183 { | 175 { |
184 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) | 176 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) |
185 .then(indexedDBResult => | 177 .then(indexedDBResult => |
186 { | 178 { |
187 if (!indexedDBResult) | 179 if (!indexedDBResult) |
188 { | 180 { |
189 const {IndexedDBBackup} = require("./indexedDBBackup"); | 181 const {IndexedDBBackup} = require("./indexedDBBackup"); |
190 | 182 |
191 return IndexedDBBackup.getBackupData() | 183 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 => | 184 .then(backupData => |
193 saveFile( | 185 saveFile( |
194 { | 186 { |
195 fileName: fileToKey(fileName), | 187 fileName: fileToKey(fileName), |
196 content: backupData.content, | 188 content: backupData.content, |
197 lastModified: backupData.lastModified | 189 lastModified: backupData.lastModified |
198 }, | 190 }, |
199 dbInstance, | 191 dbInstance, |
200 storeName) | 192 storeName).then(() => backupData) |
201 .then(() => backupData)); | 193 ); |
202 } | 194 } |
203 return indexedDBResult; | 195 return indexedDBResult; |
204 }); | 196 }); |
205 } | 197 } |
206 | 198 |
207 function getFromIndexedDB(fileName, dbInstance, storeName) | 199 function getFromIndexedDB(fileName, dbInstance, storeName) |
208 { | 200 { |
209 return new Promise((resolve, reject) => | 201 return new Promise((resolve, reject) => |
210 { | 202 { |
211 let store = getObjectStore(dbInstance, storeName); | 203 let store = getObjectStore(dbInstance, storeName); |
212 let req = store.get(fileName); | 204 let req = store.get(fileName); |
213 | 205 |
214 req.onsuccess = event => resolve(event.currentTarget.result); | 206 req.onsuccess = event => resolve(event.currentTarget.result); |
215 req.onerror = event => reject(event.target.error); | 207 req.onerror = event => reject(event.target.error); |
216 }) | 208 }) |
217 .catch(error => | 209 .catch(error => |
218 { | 210 { |
219 if (error.name == "UnknownError") | 211 if (error.name == "UnknownError") |
220 return reestablishConnection(dbInstance) | 212 return reestablishConnection(dbInstance).then(() => undefined); |
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.
| |
222 return Promise.reject(error); | |
Sebastian Noack
2018/08/21 20:26:25
Nit: See above.
geo
2018/08/31 15:49:25
Done.
| |
223 }); | 213 }); |
224 } | 214 } |
225 | 215 |
226 function saveFile(data, dbInstance, storeName) | 216 function saveFile(data, dbInstance, storeName) |
227 { | 217 { |
228 return new Promise((resolve, reject) => | 218 return new Promise((resolve, reject) => |
229 { | 219 { |
230 let store = getObjectStore(dbInstance, storeName); | 220 let store = getObjectStore(dbInstance, storeName); |
231 let req = store.put(data); | 221 let req = store.put(data); |
232 | 222 |
233 req.onsuccess = resolve; | 223 req.onsuccess = resolve; |
234 req.onerror = event => reject(event.target.error); | 224 req.onerror = event => reject(event.target.error); |
235 }) | 225 }) |
236 .catch(error => | 226 .catch(error => |
237 { | 227 { |
238 if (error.name == "UnknownError") | 228 if (error.name == "UnknownError") |
239 { | 229 { |
240 return reestablishConnection(dbInstance) | 230 return reestablishConnection(dbInstance).then(newDbInstance => |
241 .then(newDbInstance => | 231 saveFile(data, newDbInstance, storeName) |
242 saveFile(data, newDbInstance, storeName)); | 232 ); |
243 } | 233 } |
244 return Promise.reject(error); | |
245 }); | 234 }); |
246 } | 235 } |
247 | 236 |
248 function deleteFile(fileName, dbInstance, storeName) | 237 function deleteFile(fileName, dbInstance, storeName) |
249 { | 238 { |
250 return new Promise((resolve, reject) => | 239 return new Promise((resolve, reject) => |
251 { | 240 { |
252 let store = getObjectStore(dbInstance, storeName); | 241 let store = getObjectStore(dbInstance, storeName); |
253 let req = store.delete(fileToKey(fileName)); | 242 let req = store.delete(fileToKey(fileName)); |
254 | 243 |
255 req.onsuccess = resolve; | 244 req.onsuccess = resolve; |
256 req.onerror = event => reject(event.target.error); | 245 req.onerror = event => reject(event.target.error); |
257 }) | 246 }) |
258 .catch(error => | 247 .catch(error => |
259 { | 248 { |
260 if (error.name == "UnknownError") | 249 if (error.name == "UnknownError") |
261 { | 250 return reestablishConnection(dbInstance); |
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.
| |
267 }); | 251 }); |
268 } | 252 } |
269 | 253 |
270 exports.IO = | 254 exports.IO = |
271 { | 255 { |
272 /** | 256 /** |
273 * Writes text lines to a file. | 257 * Writes text lines to a file. |
274 * @param {string} fileName | 258 * @param {string} fileName |
275 * Name of the file to be written | 259 * Name of the file to be written |
276 * @param {Iterable.<string>} data | 260 * @param {Iterable.<string>} data |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 fileName: fileToKey(newName), | 344 fileName: fileToKey(newName), |
361 content: fileData.content, | 345 content: fileData.content, |
362 lastModified: fileData.lastModified | 346 lastModified: fileData.lastModified |
363 }, | 347 }, |
364 dbInstance, | 348 dbInstance, |
365 dbConfig.storeName)) | 349 dbConfig.storeName)) |
366 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); | 350 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); |
367 } | 351 } |
368 }; | 352 }; |
369 | 353 |
LEFT | RIGHT |