javascript - indexedDB doesn't reset version when you delete a database on Chrome -- bug or user error? -
the following code throws error in chrome 35 (but not firefox 29) if set buggy
true
.
since i'm pretty new in indexeddb wanted ask whether ought work or not. if delete database , open again version=1
, shouldn't onupgradeneeded
callback called?
<html> <head> <script type="text/javascript"> var = 1000; var buggy = true; function open() { var version = buggy ? 1 : 1001 - i; var request = indexeddb.open("test", version); var upgraded = false; request.onupgradeneeded = function() { upgraded = true; console.log("upgraded ok"); } request.onsuccess = function() { if (!upgraded) { throw "not upgraded"; } console.log("open ok"); if (--i != 0) { obliterate(); } } request.onerror = function() { throw "error in open"; } } function obliterate() { var request = indexeddb.deletedatabase("test"); request.onsuccess = function() { console.log("delete ok"); open(); } request.onerror = function(event) { throw "error in obliterate."; } } obliterate(); </script> </head> <body> </body> </html>
in chrome, buggy=true
, get:
delete ok test.html:29 upgraded ok test.html:12 open ok test.html:18 delete ok test.html:29 uncaught not upgraded
in firefox works fine.
as side note, in both chrome , firefox, runs incredibly -- 5-10 seconds create , delete database single time. normal/expected? doing wrong?
this user error.
deletions in indexeddb fast in both firefox , chrome. haven't done measurement. put things perspective: in test suite synceddb delete , create database between every single test. there 67 tests , execute in less second. , database deletions , creations not time consuming part of tests.
what you've stumbled upon 1 of tricky parts of how indexeddb works. indexeddb has notion of database connection. relevant parts regards problem is:
- you establish connection opening database
open
function. connection represented idbdatabase acquire throughopen
. - a database can not deleted long open connections it.
- when attempt made delete database 'versionchange' event fired against open idbdatabase objects connected database. event have
newversion
property setnull
.
the problem call obliterate
function inside open
requests onsuccess
event handler. point trying delete database @ have open connection. problematic , why example code not work in chrome (it appears firefox times out connection , completes deletion, albeit huge delay).
the fix attach listener versionchange
event in onsuccess
event handler. add after line if (--i != 0) { obliterate(); }
:
request.result.onversionchange = function(e) { if (e.newversion === null) { // attempt made delete db e.target.close(); // manually close our connection db } };
inserting see both firefox , chrome no rapidly create , delete tests
database.
Comments
Post a Comment