[whatwg/dom] Allow ExtendableEvent within Document (Issue #1167)

Currently, `ExtendableEvent` is only available inside within a Service Worker scope. Nothing in the spec seems to make the ExtendableEvent dependent on the scope.

It would be useful for custom, authored `EventTarget` objects be able to dispatch these events and hook into the promise resolution. The alternative is to go outside of EventTarget or use multiple events that just communicate the status of a promise.

I'm currently using a "polyfill" of sorts for ExtendableEvent though mixed in with CustomEvent:

````js
/**
 * @param {EventTarget} eventTarget
 * @param {string} type
 * @param {any} detail
 * @return {Promise<boolean>} preventDefault called
 */
export async function dispatchExtendableEvent(eventTarget, type, detail) {
  let event;
  if (detail == null) {
    event = new Event(type);
  } else if (typeof CustomEvent === 'undefined') {
    event = new Event(type);
    event.detail = detail;
  } else {
    event = new CustomEvent(type, { detail });
  }
  let pendingPromise;
  event.waitUntil = (promise) => { pendingPromise = promise; };
  const result = eventTarget.dispatchEvent(event);
  if (pendingPromise) await pendingPromise;
  return result;
}
````

Reference: https://w3c.github.io/ServiceWorker/#extendableevent-interface

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1167
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1167@github.com>

Received on Thursday, 23 February 2023 17:15:29 UTC