-
Notifications
You must be signed in to change notification settings - Fork 2
Can you schedule more tasks after waiting promises resolve? #11
Comments
I reacted exactly like @inexorabletash about this. It's not clear in README what implications the "waiting" state has on that transactions, except by the code snippets, where it seems possible to use the transaction as if it was in the active state. I hope that is the case, and that the state remains "waiting" after a transaction has been operated on. Assuming that waiting state is equivalent to active state when operating on the database, we could see that waitUntil() will be the thing you always begin with after creating a transaction, so that the rest of the code doesn't need to worry about transaction committing but can start rely on promises instead. If I've understood this correctly, the pattern that everyone will likely use, is to always start a transaction using waitUntil() and then do everything in a promise-returning function: function doSomethingInTransaction (db) {
let trans = db.transaction(['items'], 'readwrite');
return trans.waitUntil((async ()=>{
// Do your code here
})());
} |
Inspired by @wwoods solution described in #374 and the new [indexeddb-promises proposal](inexorabletash/indexeddb-promises#11), I've added the method Dexie.waitFor() that may wait on a non-indexedDB Promise or Promise-returning function to complete and keep transaction alive during the whole lifetime of that task. Implementation was also inspired by the elegant code in [polyfill.js](https://github.com/inexorabletash/indexeddb-promises/blob/master/polyfill.js) by @inexorabletash.
Inspired by @wwoods solution described in #374 and the new [indexeddb-promises proposal](inexorabletash/indexeddb-promises#11), I've added the method Dexie.waitFor() that may wait on a non-indexedDB Promise or Promise-returning function to complete and keep transaction alive during the whole lifetime of that task. Implementation was also inspired by the elegant code in [polyfill.js](https://github.com/inexorabletash/indexeddb-promises/blob/master/polyfill.js) by @inexorabletash.
Let's dig up the old example of get-fetch-put:
It is conceptually simpler to write as:
(See commentary in #3 and #9 about simplifying the x_p/waitUntil(X_p)/await x_p pattern)
... but to do that we need to ensure that after the waiting promise fires we get another "tick" to schedule more work, otherwise the commit will start before control returns to script:
One way to address this would be to specify that:
That is, don't try to commit immediately but queue a microtask to schedule a commit. That will ensure that microtasks queued on a waiting promise get to execute before we try to commit. But it's still subtle.
Maybe we need to queue a task rather than queue a microtask?
The text was updated successfully, but these errors were encountered: