dmonad / lib0

Monorepo of isomorphic utility functions
MIT License
345 stars 63 forks source link

indexeddb: ignore blocked event #57

Closed raineorshine closed 1 year ago

raineorshine commented 1 year ago

55

raineorshine commented 1 year ago

I have commented out the onblocked handlers and added a basic test.

Emitting the blocked event is another question. The request object is wrapped in a promise, so there is no way to bubble up the event with the existing API. The ideas I have so far are:

  1. Change the indexeddb module from promise-based to event-based, returning an emitter that emits success, error, and blocked events. Major change to the API.
  2. Return a custom promise which supports .on('blocked, ...)`. Non-breaking, but unconventional.
  3. Fire a custom event on the window object. Non-breaking, but needs a way for the subscriber to identify the request.
    const e = new CustomEvent('lib0.indexeddb.blocked', { detail: { request } })
    dispatchEvent(e)
  4. Don't re-emit blocked at all. In most cases, just waiting allows the request to complete. In hypothetical cases where it's permanently blocked, the app probably needs a timeout mechanism, which doesn't rely on the blocked event anyway. There may be a case in the future where blocked really does need to be detected, but it can be dealt with then.

Open to your thoughts.

raineorshine commented 1 year ago

@dmonad

dmonad commented 1 year ago

The blocked event is, as the spec describes it, a "progress report". So there can potentially be multiple blocked events. Hence I wouldn't go with 1-3.

I agree with 4. If the database can't be opened, we could reject the promise with a custom error. This is only necessary when opening the database - other requests can't be blocked (if I understand the spec correctly).

If you want to add a PR for this: I suggest making the timeout configurable but with a sensible default (e.g. 15s).

dmonad commented 1 year ago

Thanks for the PR!

I will release it in a bit ;)