Open inexorabletash opened 8 years ago
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
})());
}
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?