wintercg / proposal-common-minimum-api

https://common-min-api.proposal.wintercg.org/
Other
221 stars 15 forks source link

ISSUE 2: Should `globalThis` be required to extend from `EventTarget`? #16

Open XadillaX opened 2 years ago

XadillaX commented 2 years ago

https://proposal-common-min-api.deno.dev/#issue-69352234

I think globalThis SHOULD extend from EventTarget. Or at lease there will be one EventTarget instance, and globalThis should mount that instance's *EventListener(), dispatchEvent().

And also, I think the lifetime and events (install, activate, fetch, etc.) also should be embodied.

ljharb commented 2 years ago

That seems like something that would conflict pretty heavily with browsers.

lucacasonato commented 2 years ago

That seems like something that would conflict pretty heavily with browsers.

Why? In all three primary browser realm types (main thread, workers, service workers etc), globalThis has a prototype that extends EventTarget.

ljharb commented 2 years ago

ah, i think i was confusing it with event emitters.

It makes sense that there's a global addEventListener, for example, in the browser, because the DOM is ever-present there. That's not the case outside the browser. What types of targets and events would addEventListener interact with outside the browser?

lucacasonato commented 2 years ago

Deno uses the global event target for "load" and "unload" events for example. CFW uses it for the global "fetch" event.

XadillaX commented 2 years ago

ah, i think i was confusing it with event emitters.

It makes sense that there's a global addEventListener, for example, in the browser, because the DOM is ever-present there. That's not the case outside the browser. What types of targets and events would addEventListener interact with outside the browser?

fetch event in service worker.

JakeChampion commented 2 years ago

Deno uses the global event target for "load" and "unload" events for example. CFW uses it for the global "fetch" event.

Fastly also use it for the global "fetch" event -- https://js-compute-reference-docs.edgecompute.app/index.html#addeventlistener

jasnell commented 2 years ago

We discussed this issue on the last wintercg call. Where we landed is that the idea that globalThis should extend from EventTarget would qualify as "normative optional"... that is, it is not something that should be required of all Web-interoperable runtimes but should behave consistently for those that do implement it. As opposed to including it in the common minimum API doc, the idea will be to write up a separate note that describes the expected interoperable behavior.