indexeddb-promises
indexeddb-promises copied to clipboard
replace waitUntil with async callback
Inspired by the work over in https://github.com/inexorabletash/web-locks and suggested by @dominiccooney at TPAC 2016...
Rather than the confusing usage of waitUntil(), we could do something like:
db.transaction(scope, mode, async tx => {
const store = tx.objectStore('s');
const value = await store.get('key');
const response = await fetch(`${url}?value=${value}');
if (response.ok) {
await store.put(await response.text(), 'stuff');
}
// tx commits when this async function's promise resolves
});
Lots of details to be worked out (e.g. what does transaction()
return here? (IDBTransaction or Promise<void>)
? what do cursors look like, etc?)
(This is assuming we want to tackle promisifying transactions in the first place, vs. a completely revised API. Tracking it here for posterity.)
I really like this as a way of 'fixing' the IDB API without breaking compatibility.
I think the return of transaction()
can remain the same, but add a .complete
promise to the IDBTransaction
. The inner tx
(shall we call it IDBPromiseTransaction
?) won't have .complete
, as it'd likely lead to deadlock.
I think we'd need something similar for opening the database:
const openRequest = indexedDB.open(name, version, async upgradeTransaction => {
const store = upgradeTransaction.createObjectStore('blah');
// etc
});
Again, IDBOpenDBRequest
needs some kind of promise to get the IDBDatabase
.