@@ -52,6 +52,10 @@ class RandomAccessIdb extends EventEmitter {
52
52
}
53
53
54
54
async ensureDBReady ( ) {
55
+ if ( this . closing || this . purging ) {
56
+ return ; // Don't reinitialize if closing or purging
57
+ }
58
+
55
59
if ( ! this . opened || this . closed ) {
56
60
await this . _initializeDB ( ) ; // Initialize if the database is not open
57
61
}
@@ -64,6 +68,7 @@ class RandomAccessIdb extends EventEmitter {
64
68
}
65
69
66
70
get fileName ( ) {
71
+ if ( ! this . meta ) return null ;
67
72
return this . meta . fileName ;
68
73
}
69
74
@@ -219,23 +224,30 @@ class RandomAccessIdb extends EventEmitter {
219
224
}
220
225
221
226
close ( cb = ( ) => { } ) {
222
- this . queue . addTask ( async ( ) => {
227
+ // Check if the file is already closed
228
+ if ( this . closed ) {
229
+ console . warn ( 'File is already closed.' ) ;
230
+ cb ( null ) ;
231
+ return ;
232
+ }
233
+
234
+ // Add close task to the queue, waiting for all pending tasks
235
+ this . queue . waitUntilQueueEmpty ( ) . then ( async ( ) => {
223
236
try {
224
- // Ensure any pending operations are completed
225
- await this . ensureDBReady ( ) ;
237
+ // Clear the database connection and other resources
226
238
this . db = null ;
227
- this . meta = null ;
228
- this . queue = null ; // Assuming the queue should also be cleared
229
- // console.log('File closed successfully');
230
- this . emit ( 'close' ) ;
239
+ this . queue = { } ; // Clearing queue to avoid further operations
240
+ this . closed = true ; // Mark file as closed
241
+ console . log ( 'File closed successfully' ) ;
231
242
cb ( null ) ;
232
243
} catch ( error ) {
233
- // console.error('Error during close:', error.message);
244
+ console . error ( 'Error during close:' , error . message ) ;
234
245
cb ( error ) ;
235
246
}
236
247
} ) . catch ( cb ) ;
237
248
}
238
249
250
+
239
251
truncate ( offset , cb = ( ) => { } ) {
240
252
this . queue . addTask ( async ( ) => {
241
253
await this . ensureDBReady ( ) ;
@@ -305,36 +317,70 @@ class RandomAccessIdb extends EventEmitter {
305
317
} , 0 ) ;
306
318
}
307
319
320
+ // Updated purge method
321
+ async _verifyDatabaseExists ( ) {
322
+ if ( ! ( 'databases' in indexedDB ) ) {
323
+ console . warn ( 'IndexedDB.databases() is not supported in this browser' ) ;
324
+ return true ; // Assume it exists for browsers without support
325
+ }
326
+
327
+ const databases = await indexedDB . databases ( ) ;
328
+ return databases . some ( db => db . name === this . fileName ) ;
329
+ }
330
+
308
331
purge ( cb = ( ) => { } ) {
309
- this . queue . resumeQueue ( ) ;
310
- this . queue . addTask ( async ( ) => {
311
- await this . ensureDBReady ( ) . catch ( e => false ) ;
332
+ if ( this . closed ) {
333
+ console . log ( 'File already closed, purging directly' ) ;
334
+ this . _performPurge ( cb ) ;
335
+ } else {
336
+ this . queue . waitUntilQueueEmpty ( )
337
+ . then ( ( ) => this . _performPurge ( cb ) )
338
+ . catch ( cb ) ;
339
+ }
340
+ }
312
341
313
- try {
314
- // console.log(`Purging file ${this.fileName}`);
342
+ async _performPurge ( cb = ( ) => { } ) {
343
+ try {
344
+ // Check if the database exists
345
+ const dbExists = await this . _verifyDatabaseExists ( ) ;
346
+ if ( ! dbExists ) {
347
+ console . warn ( `Database ${ this . fileName } does not exist, skipping purge.` ) ;
348
+ return cb ( null ) ;
349
+ }
315
350
316
- // Delete the file from IndexedDB
317
- await IDB . deleteDB ( this . fileName ) ;
351
+ console . log ( `Purging file ${ this . fileName } ` ) ;
318
352
319
- // Delete the metadata associated with this file
320
- await this . metaManager . del ( this . fileName ) ;
353
+ // Delete the file from IndexedDB using idb library
354
+ await IDB . deleteDB ( this . fileName ) ;
321
355
322
- // Mark the database as purged by setting db to null
323
- this . db = null ;
356
+ // Verify the deletion
357
+ const isDeleted = ! ( await this . _verifyDatabaseExists ( ) ) ;
358
+ if ( ! isDeleted ) {
359
+ throw new Error ( `Failed to delete database: ${ this . fileName } ` ) ;
360
+ }
324
361
325
- // Reset the metadata specific to this file
326
- this . meta = { fileName : this . fileName , chunkSize : this . chunkSize , length : 0 } ;
362
+ // Delete the metadata associated with this file
363
+ await this . metaManager . del ( this . fileName ) ;
327
364
328
- // Remove the file from the allLoadedFiles map
329
- allLoadedFiles . delete ( this . fileName ) ;
330
- // console.log(`Removed ${this.fileName} from allLoadedFiles`);
365
+ // Mark the database as purged by setting db to null
366
+ this . db = null ;
331
367
332
- // console.log(`Purge complete for file ${this.fileName}`);
333
- } catch ( e ) {
334
- cb ( null ) ;
335
- }
368
+ // Reset the metadata specific to this file
369
+ this . meta = { fileName : this . fileName , chunkSize : this . chunkSize , length : 0 } ;
370
+
371
+ // Remove the file from the allLoadedFiles map
372
+ allLoadedFiles . delete ( this . fileName ) ;
373
+
374
+ console . log ( `Purge complete for file ${ this . fileName } ` ) ;
336
375
cb ( null ) ;
337
- } ) . catch ( cb ) ; // Handle any errors
376
+ } catch ( e ) {
377
+ console . error ( `Error during purge: ${ e . message } ` ) ;
378
+ cb ( e ) ;
379
+ }
380
+ }
381
+
382
+ unlink ( cb = ( ) => { } ) {
383
+ this . purge ( cb ) ;
338
384
}
339
385
340
386
_blocks ( start , end ) {
0 commit comments