@pixagram/lacerta-db 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +26 -17
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -287,34 +287,41 @@ class IndexedDBUtility {
|
|
|
287
287
|
});
|
|
288
288
|
}
|
|
289
289
|
|
|
290
|
-
//
|
|
290
|
+
// Improved transaction handling with better retry logic
|
|
291
291
|
static async performTransaction(db, storeNames, mode, callback, retries = 3, operationId = null) {
|
|
292
292
|
let lastError = null;
|
|
293
|
-
|
|
293
|
+
|
|
294
294
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
295
295
|
try {
|
|
296
|
-
if (!db) {
|
|
297
|
-
throw new Error('Database connection is not available.');
|
|
296
|
+
if (!db || db.readyState !== 'done') {
|
|
297
|
+
throw new Error('Database connection is not available or ready.');
|
|
298
298
|
}
|
|
299
|
-
|
|
299
|
+
|
|
300
300
|
const tx = db.transaction(Array.isArray(storeNames) ? storeNames : [storeNames], mode);
|
|
301
|
-
const stores = Array.isArray(storeNames)
|
|
302
|
-
? storeNames.map(name => tx.objectStore(name))
|
|
301
|
+
const stores = Array.isArray(storeNames)
|
|
302
|
+
? storeNames.map(name => tx.objectStore(name))
|
|
303
303
|
: [tx.objectStore(storeNames)];
|
|
304
|
-
|
|
305
|
-
//
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
304
|
+
|
|
305
|
+
// Add transaction timeout
|
|
306
|
+
const timeoutId = setTimeout(() => {
|
|
307
|
+
tx.abort();
|
|
308
|
+
}, 30000); // 30 second timeout
|
|
309
|
+
|
|
310
|
+
const result = await callback(...stores, operationId);
|
|
309
311
|
|
|
310
312
|
return new Promise((resolve, reject) => {
|
|
311
|
-
tx.oncomplete = () =>
|
|
313
|
+
tx.oncomplete = () => {
|
|
314
|
+
clearTimeout(timeoutId);
|
|
315
|
+
resolve(result);
|
|
316
|
+
};
|
|
312
317
|
tx.onerror = () => {
|
|
318
|
+
clearTimeout(timeoutId);
|
|
313
319
|
lastError = new Error(`Transaction failed: ${tx.error ? tx.error.message : 'unknown error'}`);
|
|
314
320
|
reject(lastError);
|
|
315
321
|
};
|
|
316
322
|
tx.onabort = () => {
|
|
317
|
-
|
|
323
|
+
clearTimeout(timeoutId);
|
|
324
|
+
lastError = new Error(`Transaction aborted: ${tx.error ? tx.error.message : 'timeout or abort'}`);
|
|
318
325
|
reject(lastError);
|
|
319
326
|
};
|
|
320
327
|
});
|
|
@@ -322,12 +329,13 @@ class IndexedDBUtility {
|
|
|
322
329
|
lastError = error;
|
|
323
330
|
if (attempt < retries) {
|
|
324
331
|
console.warn(`Transaction failed, retrying... (${retries - attempt} attempts left): ${error.message}`);
|
|
325
|
-
// Exponential backoff
|
|
326
|
-
|
|
332
|
+
// Exponential backoff with jitter
|
|
333
|
+
const delay = Math.pow(2, attempt) * 100 + Math.random() * 100;
|
|
334
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
327
335
|
}
|
|
328
336
|
}
|
|
329
337
|
}
|
|
330
|
-
|
|
338
|
+
|
|
331
339
|
throw new Error(`Transaction ultimately failed after ${retries + 1} attempts: ${lastError.message}`);
|
|
332
340
|
}
|
|
333
341
|
|
|
@@ -2075,3 +2083,4 @@ export class Collection {
|
|
|
2075
2083
|
return `[Collection: ${this.name} | Database: ${this.database.name} | Size: ${this.sizeKB.toFixed(2)}KB | Documents: ${this.length}]`;
|
|
2076
2084
|
}
|
|
2077
2085
|
}
|
|
2086
|
+
|