You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in Python if you use a context, you can specify more than one value at a time. However, since the run function is defined on the Variable, there is no easy way to work around this.
I am wondering if a different, inverted API couldn't prove both more flexible and better readable?
consta=Symbol('a')constb=Symbol('b')const{ run, get }=AsyncContextrun({[a]: 'x',[b]: 'y'},async()=>{console.log([get(a),get(b)])})
Note: this proposal could possible address the issue in #76 by allowing the internals to use a Object instead of a Map to store.
If we detach the scoping mechanism from the setting mechanism, as I have described in other previous conversations about AsyncContext, we would not have this problem.
This is essentially what already happens internally anyway that it has to first create and enter a scope before it can set the value, but the API as it is designed at present requires that every set comes with a new scope rather than being able to share or reuse scopes. The current design also results in rather poor ergonomics for async-await code, which this design resolves too.
Thank you for highlight the other discussions. My problem with the scope example would be that the context variables are assumed to be set within the "scope" at any given time causing possibly confusing behavior:
However, I don't try to weigh in on the single-swoop or detached definition approach here and rather mean try to reduce the API surface and at the same time make declaration easier. For the detached scope definition I could analogously imagine:
You can avoid that issue by just wrapping the calls in more scopes, if you want to contain their changes. It's somewhat correct that linear code like that would behave that way though--you can think of context as being a lot like a global, but only within a particular scope. Changes will appear to any related code, so long as it originates in that scope.
Extending the proposal I would also think that it might make sense to add it as function to Snapshot
constasyncVar=Symbol('asyncVar')constsnapshot=AsyncContext.Snapshot()letvalue;value=snapshot.run(()=>AsyncContext.get(asyncVar))// Equal to the line abovevalue=snapshot.get(asyncVar)
that way a unnecessary workaround could be avoided.
For cases where someone might use multiple like that, it would likely just be cleaner to use a single AsyncContext.Variable with a compound value... so instead of...
@jasnell that is under the assumption that multiple can be grouped, I would assume that they are from different subsystems to be configured at the same time.
If you combine two independent stores into one they always propagate together and are activated/deactivated together. This might be not intended in all cases.
Mutating a part of this combined store to reflect this difference results in side effects that different propagation chains are effected as well.
But I wonder about the use case to have more AsyncContext.Variable() instances in hand from different subsystems. I would expect these instances are internals of these subsystem and should not leak to a global place.
With the proposed API it is up to the user to define multiple at once or a single one. You can nest it like the current API.
The variables can be private but they can also be exposed and likely will be exposed by some libraries. Setting the values for the libraries in an overarching context.
Then the snapshot can be used as any other AsyncContext.Snapshot
Essentially, the AsyncContext.Map would allow fine grained control over what context is actually propagated in the snapshot rather than taking everything in the current
The main reason why I opened this proposal is for readability reasons, to make it more natural to use and to reduce the amount of specification introduced. Adding a Map to the AsyncContext would look something like this:
import{Request,Response}from'server/symbol'import{Trace}from'system-logger/symbol'functionhandleRequest(req,res){constcontroller=newAbortController()// etc.constctx=newAsyncContext.Map([// assuming the constructor is somewhat like the Map constructor[Request,req],[Response,res],[Trace,crypto.randomUUID()],[AbortController.context,controller.signal],])ctx.snapshot().run(async()=>{// etc.})}
To me this is a lot more uncomfortable to write and deal with and overall the complexity feels a lot higher as well.
Activity
Qard commentedon Oct 10, 2024
If we detach the scoping mechanism from the setting mechanism, as I have described in other previous conversations about AsyncContext, we would not have this problem.
This is essentially what already happens internally anyway that it has to first create and enter a scope before it can set the value, but the API as it is designed at present requires that every set comes with a new scope rather than being able to share or reuse scopes. The current design also results in rather poor ergonomics for async-await code, which this design resolves too.
martinheidegger commentedon Oct 10, 2024
Thank you for highlight the other discussions. My problem with the scope example would be that the context variables are assumed to be set within the "scope" at any given time causing possibly confusing behavior:
However, I don't try to weigh in on the single-swoop or detached definition approach here and rather mean try to reduce the API surface and at the same time make declaration easier. For the detached scope definition I could analogously imagine:
Qard commentedon Oct 10, 2024
You can avoid that issue by just wrapping the calls in more scopes, if you want to contain their changes. It's somewhat correct that linear code like that would behave that way though--you can think of context as being a lot like a global, but only within a particular scope. Changes will appear to any related code, so long as it originates in that scope.
martinheidegger commentedon Oct 11, 2024
Note: I updated the API proposal in the initial post to use Symbols instead of a Map
martinheidegger commentedon Oct 11, 2024
Extending the proposal I would also think that it might make sense to add it as function to
Snapshotthat way a unnecessary workaround could be avoided.
Try Inverted API
jasnell commentedon Dec 3, 2024
For cases where someone might use multiple like that, it would likely just be cleaner to use a single
AsyncContext.Variablewith a compound value... so instead of...It would be...
martinheidegger commentedon Dec 3, 2024
@jasnell that is under the assumption that multiple can be grouped, I would assume that they are from different subsystems to be configured at the same time.
Flarna commentedon Dec 3, 2024
If you combine two independent stores into one they always propagate together and are activated/deactivated together. This might be not intended in all cases.
Mutating a part of this combined store to reflect this difference results in side effects that different propagation chains are effected as well.
But I wonder about the use case to have more
AsyncContext.Variable()instances in hand from different subsystems. I would expect these instances are internals of these subsystem and should not leak to a global place.martinheidegger commentedon Dec 3, 2024
With the proposed API it is up to the user to define multiple at once or a single one. You can nest it like the current API.
The variables can be private but they can also be exposed and likely will be exposed by some libraries. Setting the values for the libraries in an overarching context.
I imagine something like:
jasnell commentedon Dec 3, 2024
If we're going for something like that then I would almost prefer a Map-like model for
AsyncContext... perhaps something like...Then the
snapshotcan be used as any otherAsyncContext.SnapshotEssentially, the
AsyncContext.Mapwould allow fine grained control over what context is actually propagated in the snapshot rather than taking everything in the currentmartinheidegger commentedon Dec 3, 2024
The main reason why I opened this proposal is for readability reasons, to make it more natural to use and to reduce the amount of specification introduced. Adding a
Mapto theAsyncContextwould look something like this:To me this is a lot more uncomfortable to write and deal with and overall the complexity feels a lot higher as well.
Sidenote: The
snapshot()is probably superfluous.martinheidegger commentedon Nov 15, 2025
Cc. @jridgewell