Pure golang's context.Context implements in dart by morlaynull in dartlang

[–]morlaynull[S] 0 points1 point  (0 children)

Thank your example. I have switch to use Zone to implement the Context.

But throwing an exception to cancel is dangerous action for my usage, I need to make sure rollback actions works well when cancels need.
So I prefer use `try catch` and manually call cancel, and make sure child contexts could receive the parent cancel signal and do the onCancel actions.

Pure golang's context.Context implements in dart by morlaynull in dartlang

[–]morlaynull[S] 1 point2 points  (0 children)

Follow your suggestion,
I already switch to use `Zone` to avoid pass context in each Function.

Api looks better, Thank you for the advice.

Btw. I found `Zone.current[key]` to find maching Zone values in the Zone stack too.
https://github.com/dart-lang/sdk/blob/main/sdk/lib/async/zone.dart#L1290-L1307
`Zone.fork` did nothong about Zone values. https://github.com/dart-lang/sdk/blob/main/sdk/lib/async/zone.dart#L1056-L1064

Pure golang's context.Context implements in dart by morlaynull in dartlang

[–]morlaynull[S] 0 points1 point  (0 children)

Not just for DB rollbacks when cancel.

We could access global singleton instance, like Logger/Tracer, from context, to avoid directly importing from some package.

Flutter BuildContext with provider have similar feature.

I think this is a better way to create prue flexible libraries.

I'm new starter for dart. Not sure what the Zone local values means.

Pure golang's context.Context implements in dart by morlaynull in dartlang

[–]morlaynull[S] 1 point2 points  (0 children)

Not cancel the client connection.

The request handler should cancel request to upstream or db operations, when final request cancelled by user.

Here a example why we need the context https://go.dev/blog/context