How can I wait until an instance of IndexedDB is closed?

The IndexedDB close method is synchronous , how can I wait for the close to complete? My use case is that I close IndexedDB after automatic tests, and then before the next test I delete the database and open it again.

The problem that I see is that sometimes indexedDB.deleteDatabase fires a blocked event because the database has not yet been closed asynchronously (as you can see from the deleteDatabase documentation that I referenced). What I'm not quite sure about is if the database is still deleted in this case, despite the fact that the event was blocked.

+4
source share
2 answers

You do not need to wait for the completion of completed events, just close, delete the database and re-open it.

As you can see in the IndexedDB API document, the close method does not dispatch a completed event, but the delete database method does this. In any case, you do not need to listen to these events.

+1
source

The database cannot be closed if transactions are still running, so it’s possible. Do you have something like this?

IndexedDB.open("mydb").onsuccess = function(e) { db = e.target.result; trans = db.transaction("someobjectstore"); os = trans.getObjectStore("someobjectstore"); os.put("blah"); db.close(); } 

If so, the transaction may still be running.

No matter what, the database will still be deleted if you receive a blocked event ... when all connections to it are closed.

When you create a database connection, make sure you assign the onversionchange handler to the resulting db. This can help debug what happens.

 IndexedDB.open("mydb").onsuccess = function(e) { db = e.target.result; db.onversionchange = function(e) { console.log("got versionchange event: " + e); } trans = db.transaction("someobjectstore"); os = trans.getObjectStore("someobjectstore"); os.put("blah"); db.close(); } 
+1
source

Source: https://habr.com/ru/post/1500500/


All Articles