Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Queue a microtask to unset active on transaction #27

Closed
inexorabletash opened this issue Aug 10, 2015 · 3 comments
Closed

Proposal: Queue a microtask to unset active on transaction #27

inexorabletash opened this issue Aug 10, 2015 · 3 comments

Comments

@inexorabletash
Copy link
Member

Ported from https://www.w3.org/Bugs/Public/show_bug.cgi?id=29049 by @jakearchibald

When a transaction is created, 'active' is unset when control is returned to the event loop http://w3c.github.io/IndexedDB/#dom-idbdatabase-transaction (8).

When onsuccess is fired, 'active' is unset once the event has dispatched http://w3c.github.io/IndexedDB/#fire-a-success-event (4).

If both of these were "queue a microtask to unset 'active'", it would be more consistent and allow promise-based APIs to interact with IDB.

Currently, building an abstraction like this is impossible:

beginTransaction();
getValue('foo').then(function(val) {
  console.log(val);
  return setValue(val)
});

Assuming getValue returns a compliant promise, setValue fails as the transaction is inactive.

@inexorabletash
Copy link
Member Author

This seems like a safe, web-compatible change; the queued microtask would run after any microtasks enqueued by event handlers, so the microtask for the Promise returned by getvalue in @jakearchibald's example would run before the "unset 'active'" microtask.

This would fail, though:

getValue('foo')
  .then(function(val) {
    console.log(val);
    return val;
  })
  .then(function(val) {
    return setValue(val)
  });

.... which seems fragile. But that's what proposals like https://gist.github.com/inexorabletash/8c122c84a65de65c35b3 are for.

I need feedback from other implementers and Promise/Microtask-savvy folks though.

@sicking
Copy link
Contributor

sicking commented Aug 10, 2015

I don't see why @jakearchibald's example would fail in a compliant Promise implementation.

The 'active' flag gets set to false once the success handler for the "getValue" returns. But the Promise implementation should run then-callbacks at the end of the microtask, which is before the event handler returns, no?

@jakearchibald
Copy link
Contributor

Y'know, I believe @sicking is right. When I followed this through the spec, https://dom.spec.whatwg.org/#dispatching-events didn't link to https://html.spec.whatwg.org/multipage/webappapis.html#calling-scripts so I missed it.

If a spec calls a callback, and that spec hasn't been synchronously triggered by script, then microtasks are processed per callback.

So yeah, Chrome is doing the right thing here, and it's the only browser getting it right.

Feel free to close this.

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

No branches or pull requests

3 participants