-
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
Ergonomic AsyncContext.wrap / AsyncResource alternative #21
Comments
Thank you. It would be worth pointing out that the implementation of snapshot() is as simple as: static snapshot() {
return AsyncContext.wrap((cb, ... args) => cb(... args));
} |
I do not think Node.js I might be crossing the issues a bit, but AsyncResource does help with support for EventEmitter and similar: https://github.com/nodejs/node/blob/60996372c4e0f3b535460460ad1ca6d9a2d564b0/lib/events.js#L167. As a side note: I think the Having a |
You can create a single wrapped function, and reuse its context-restoration for multiple callbacks. This is the example given by const snapshot = AsyncContext.wrap((cb, ...args) => cb(...args));
snapshot((a, b, c) => { … } , 1, 2, 3);
snapshot(fn1);
snapshot(fn2); We might ease this by making a static |
@mcollina ... it's not // Using AsyncResource
class Foo extends AsyncResource {
constructor() { super("Foo") }
bar() { this.runInAsyncScope(() => {}) }
}
// Using AsyncContext.snapshot / AsyncContext.wrap((cb, ...args) => cb(...args))
class Foo {
#runInAsyncScope = AsyncContext.snapshot();
bar() { this.runInAsyncScope(() => {}) }
} |
This is ok, thanks for the clarification. I would recommend including |
If we chose to model this as a class, I would like to keep the ability to "wrap" functions to run in the snapshotted context. I think that could be solved by having a method on these snapshot instances, e.g.: const wrapped = snapshot.bind(fn); |
During today's meeting, we decided that the ergonomics of
The exact naming and structure is TBD. Do we use a namespace? Factory methods? Two global classes that can be constructed? Don't know, but we're definitely going to remove |
AsyncContext.wrap
captures the storage with a function. If multiple callbacks needs to be wrapped in the same context,AsyncContext.wrap
needs to be called multiple times and the storage needs to be captured respectively.A snapshotting API would be helpful for capturing the context used for multiple callbacks, instead of wrapping each individually. Node.js provides
AsyncResource
to capture the storage and allow multiple callbacks to be run with the captured storage:asyncResource.runInAsyncScope(cb)
.Possible API:
Or it can be reified as a proper class like
AsyncContext.Snapshot
./cc @jasnell
The text was updated successfully, but these errors were encountered: