Synchronization Context Rx.Net Async #1967
Replies: 1 comment 1 reply
-
Historically, Rx has never made any attempt to flow context of any kind. Sometimes calls are processed immediately (e.g., I believe previous maintainers of Rx have looked at this from time to time, and there would be non-trivial costs to flowing context that would have a negative impact on existing Rx users. We don't want to impose that overhead across the board because it would be supplying a feature that existing Rx users were definitely coping without. We do have an open work item to look at this again in a future version of Rx to see if anything can be done to address the scenarios that lead to people asking for this. I think any solution would need to be an "edge" type thing, where context would be optionally reestablished as events leave the world of Rx, but I don't know if we could do it without explicitly flowing context through. E.g. you could imagine a Regarding Async Rx.NET, you said:
What do you mean by "filters" here? Are you thinking specifically predicates for things like Since Async Rx.NET offers overloads of those sorts of operators where the callbacks can be async (they can return a public static class AsyncEx
{
public static Func<TIn, ValueTask<TResult>> OnContext<TIn, TResult>(
this Func<TIn, ValueTask<TResult>> m,
SynchronizationContext context)
{
return async arg =>
{
TaskCompletionSource<TResult> tcs = new();
context.Post(
async o =>
{
(TIn a, Func<TIn, ValueTask<TResult>> m, TaskCompletionSource<TResult> c) = ((TIn, Func<TIn, ValueTask<TResult>>, TaskCompletionSource<TResult>))o!;
try
{
tcs.SetResult(await m(a));
}
catch (Exception x)
{
tcs.SetException(x);
}
},
(arg, m, tcs));
return await tcs.Task;
};
}
} Then if you have some predicate like this: Func<int, ValueTask<bool>> isEven = async i =>
{
Console.WriteLine($"Predicate invoked on {Environment.CurrentManagedThreadId}");
return i % 2 == 0;
}; then given some Func<int, ValueTask<bool>> isEvenOnCtx = isEven.OnContext(ctx); and now |
Beta Was this translation helpful? Give feedback.
-
Hi!
Will Rx.Net Async consider Synchronization Context?
For example, if I apply a Where operator with an async filter, exists a way to guarantee that the filters are posted on a Synchronization Context. Basically is it possible to decide wether or not to use ConfigureAwait(false) internally?
Thank you
Beta Was this translation helpful? Give feedback.
All reactions