Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

replace waitUntil with async callback #14

Open
inexorabletash opened this issue Feb 15, 2018 · 1 comment
Open

replace waitUntil with async callback #14

inexorabletash opened this issue Feb 15, 2018 · 1 comment

Comments

@inexorabletash
Copy link
Owner

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.)

@jakearchibald
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants