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; | |
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); | 163 db = openDB(dbConfig); |
165 | 164 |
166 return db.catch(err => | 165 return db.catch(err => |
167 { | 166 { |
168 if (retries == 10) | 167 if (!retries) |
169 throw err; | 168 throw err; |
170 | 169 |
171 return reestablishConnection(dbInstance, ++retries); | 170 return reestablishConnection(dbInstance, --retries); |
172 }); | 171 }); |
173 } | 172 } |
174 | 173 |
175 function getFile(fileName, dbInstance, storeName) | 174 function getFile(fileName, dbInstance, storeName) |
176 { | 175 { |
177 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) | 176 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) |
178 .then(indexedDBResult => | 177 .then(indexedDBResult => |
179 { | 178 { |
180 if (!indexedDBResult) | 179 if (!indexedDBResult) |
181 { | 180 { |
182 const {IndexedDBBackup} = require("./indexedDBBackup"); | 181 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 | 182 |
184 return IndexedDBBackup.getBackupData() | 183 return IndexedDBBackup.getBackupData() |
185 .then(backupData => | 184 .then(backupData => |
186 saveFile( | 185 saveFile( |
187 { | 186 { |
188 fileName: fileToKey(fileName), | 187 fileName: fileToKey(fileName), |
189 content: backupData.content, | 188 content: backupData.content, |
190 lastModified: backupData.lastModified | 189 lastModified: backupData.lastModified |
191 }, | 190 }, |
192 dbInstance, | 191 dbInstance, |
193 storeName) | 192 storeName).then(() => backupData) |
194 .then(() => backupData)); | 193 ); |
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 } | 194 } |
196 return indexedDBResult; | 195 return indexedDBResult; |
197 }); | 196 }); |
198 } | 197 } |
199 | 198 |
200 function getFromIndexedDB(fileName, dbInstance, storeName) | 199 function getFromIndexedDB(fileName, dbInstance, storeName) |
201 { | 200 { |
202 return new Promise((resolve, reject) => | 201 return new Promise((resolve, reject) => |
203 { | 202 { |
204 let store = getObjectStore(dbInstance, storeName); | 203 let store = getObjectStore(dbInstance, storeName); |
205 let req = store.get(fileName); | 204 let req = store.get(fileName); |
206 | 205 |
207 req.onsuccess = event => resolve(event.currentTarget.result); | 206 req.onsuccess = event => resolve(event.currentTarget.result); |
208 req.onerror = event => reject(event.target.error); | 207 req.onerror = event => reject(event.target.error); |
209 }) | 208 }) |
210 .catch(error => | 209 .catch(error => |
211 { | 210 { |
212 if (error.name == "UnknownError") | 211 if (error.name == "UnknownError") |
213 return reestablishConnection(dbInstance) | 212 return reestablishConnection(dbInstance).then(() => undefined); |
214 .then(() => Promise.resolve()); | |
215 }); | 213 }); |
216 } | 214 } |
217 | 215 |
218 function saveFile(data, dbInstance, storeName) | 216 function saveFile(data, dbInstance, storeName) |
219 { | 217 { |
220 return new Promise((resolve, reject) => | 218 return new Promise((resolve, reject) => |
221 { | 219 { |
222 let store = getObjectStore(dbInstance, storeName); | 220 let store = getObjectStore(dbInstance, storeName); |
223 let req = store.put(data); | 221 let req = store.put(data); |
224 | 222 |
225 req.onsuccess = resolve; | 223 req.onsuccess = resolve; |
226 req.onerror = event => reject(event.target.error); | 224 req.onerror = event => reject(event.target.error); |
227 }) | 225 }) |
228 .catch(error => | 226 .catch(error => |
229 { | 227 { |
230 if (error.name == "UnknownError") | 228 if (error.name == "UnknownError") |
231 { | 229 { |
232 return reestablishConnection(dbInstance) | 230 return reestablishConnection(dbInstance).then(newDbInstance => |
233 .then(newDbInstance => | 231 saveFile(data, newDbInstance, storeName) |
234 saveFile(data, newDbInstance, storeName)); | 232 ); |
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 } | 233 } |
236 }); | 234 }); |
237 } | 235 } |
238 | 236 |
239 function deleteFile(fileName, dbInstance, storeName) | 237 function deleteFile(fileName, dbInstance, storeName) |
240 { | 238 { |
241 return new Promise((resolve, reject) => | 239 return new Promise((resolve, reject) => |
242 { | 240 { |
243 let store = getObjectStore(dbInstance, storeName); | 241 let store = getObjectStore(dbInstance, storeName); |
244 let req = store.delete(fileToKey(fileName)); | 242 let req = store.delete(fileToKey(fileName)); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 fileName: fileToKey(newName), | 344 fileName: fileToKey(newName), |
347 content: fileData.content, | 345 content: fileData.content, |
348 lastModified: fileData.lastModified | 346 lastModified: fileData.lastModified |
349 }, | 347 }, |
350 dbInstance, | 348 dbInstance, |
351 dbConfig.storeName)) | 349 dbConfig.storeName)) |
352 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); | 350 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); |
353 } | 351 } |
354 }; | 352 }; |
355 | 353 |
LEFT | RIGHT |