-
Notifications
You must be signed in to change notification settings - Fork 14
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
EventEmitter/EventTarget integration #19
Comments
I don't think WHATWG would be strictly opposed to this (I might be wrong) and Node certainly wouldn't be opposed - but I'm wondering if the spec shouldn't provide abstract hooks the host environment can override for deciding how to propagate context and then WHATWG can specify that propagation. Then stuff like:
Would be specced and mandated by WHATWG using the host hooks. |
There's actually a bit of a difference there (potentially), since If target.addEventListener(evt, fn, { captureAsyncContext: true });
// ...
target.removeEventListener(evt, fn); // does nothing because the `captureAsyncContext` value doesn't match
target.removeEventListener(evt, fn, { captureAsyncContext: true }); // works Alternatively, per #22, event listeners could be always implicitly wrapped. |
That's a good point |
The situation is really no different than once wrappers. |
I thought that but Kevin's point about EventTarget using function equality made me reconsider since it means |
Our implementations of both EventEmitter and EventTarget already use wrappers. This really wouldn't be any different. |
Can you elaborate on what you mean here? |
I strongly think that events should use registration time by default. Like from a user's perspective if we have some code like: someScheduler.runTask("highest-priority", async () => {
// some preparation work
window.addEventListener("load", async function callback() {
// setup...
await someTask();
// more setup...
});
}); then it would be rather surprising if Further basically all other callback functions are presumed to use registration time: const variable = new AsyncContext.Variable();
// All of these will print "REGISTRATION TIME"
variable.run("REGISTRATION TIME", () => {
queueMictrotask(() => console.log(variable.get()));
setTimeout(() => console.log(variable.get()));
somePromise.then(() => console.log(variable.get()));
}); and from most user's perspectives, events are just another callback API, there's no reason they should be special in anyway to others. Having an option like Like the core motivation of this proposal is that it is too much of a pain for users to thread certain dependencies (like tracers/schedulers) through a bunch of async code, having users need to be aware of the async context basically defeats the major point of the feature. If users can pass (Highly related #22). |
Events are widely used in async operations, e.g. Host APIs like Node.js EventEmitters
Server
,Stream
, and web browser EventTargetsXMLHttpRequest
.Expectations can be that the invocation of
addEventListener
acts as a creation of an async resource and capture the current async context frame to be used when the event is emitted.We could make this an option passed into
addEventListener
which would essentially be a convenient path for manually callingAsyncContext.wrap
on the handler function.Originally posted by @jasnell and @jridgewell.
The text was updated successfully, but these errors were encountered: