Last active
December 19, 2015 02:19
-
-
Save sethladd/5882605 to your computer and use it in GitHub Desktop.
How to open two indexeddb stores, one after another?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Database db; | |
window.indexedDB.deleteDatabase('justtesting') | |
.then((_) { | |
print('opening'); | |
return window.indexedDB.open('justtesting', version: 1, | |
onUpgradeNeeded: (e) { | |
print('upgrading to v1'); | |
Database d = e.target.result as Database; | |
d.createObjectStore('store1'); | |
}, | |
onBlocked: (e) => print('blocked on 1')); | |
}) | |
.then((_db) => db = _db) | |
.then((_) => db.close()) | |
.then((_) { | |
return window.indexedDB.open('justtesting', version: 2, | |
onUpgradeNeeded: (e) { | |
print('upgrading to v2'); | |
Database d = e.target.result as Database; | |
d.createObjectStore('store2'); | |
}, | |
onBlocked: (e) => print('blocked on 2')); | |
}) | |
.then((_db) => db = _db) | |
.then((_) { | |
print('fetching data'); | |
Transaction txn = db.transaction('store1', 'readonly'); | |
ObjectStore store = txn.objectStore('store1'); | |
store.getObject('1').then(print); | |
return txn.completed; | |
}) | |
.then((_) { | |
print('all done'); | |
}) | |
.catchError(print); |
And I fixed it by closing the db before I opened it again.
Nit:
Transaction txn = db.transaction('store1', 'readonly');
ObjectStore store = txn.objectStore('store1');
store.getObject('1').then(print);
return txn.completed;
In this, if the getObject('1') fails, then it'll get lost because the transaction still succeeds.
To avoid forking the future chain, use:
Transaction txn = db.transaction('store1', 'readonly');
ObjectStore store = txn.objectStore('store1');
return store.getObject('1').then((value) {
print(value);
return txn.completed;
});
Also for reference, the upgrade scenario is tested with:
https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/tests/html/indexeddb_1_test.dart#16
you're block because you open with 2 differents version number :
from https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB
// Make sure to add a handler to be notified if another page requests a version
// change. We must close the database. This allows the other page to upgrade the database.
// If you don't do this then the upgrade won't happen until the user close the tab.
db.onversionchange = function(event) {
db.close();
alert("A new version of this page is ready. Please reload!");
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has a bug. It gets stuck at 'blocked on 2'