-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathAsyncObservableBase.cs
More file actions
183 lines (153 loc) · 6.03 KB
/
Copy pathAsyncObservableBase.cs
File metadata and controls
183 lines (153 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Threading;
using System.Threading.Tasks;
namespace System.Reactive
{
public abstract class AsyncObservableBase<T> : IAsyncObservable<T>
{
public async ValueTask<IAsyncDisposable> SubscribeAsync(IAsyncObserver<T> observer)
{
if (observer == null)
throw new ArgumentNullException(nameof(observer));
var autoDetach = new AutoDetachAsyncObserver(observer);
var subscription = await SubscribeAsyncCore(autoDetach).ConfigureAwait(false);
await autoDetach.AssignAsync(subscription).ConfigureAwait(false);
return autoDetach;
}
protected abstract ValueTask<IAsyncDisposable> SubscribeAsyncCore(IAsyncObserver<T> observer);
private sealed class AutoDetachAsyncObserver : AsyncObserverBase<T>, IAsyncDisposable
{
private readonly IAsyncObserver<T> _observer;
private TaskCompletionSource<object> _pendingOnSomethingCallsTcs;
private readonly AsyncLocal<bool> _reentrancyFlag = new(); // If any On* method, calls OnDisposeAsync, this will be true
private readonly object _gate = new();
private IAsyncDisposable _subscription;
private bool _disposing;
public AutoDetachAsyncObserver(IAsyncObserver<T> observer)
{
_observer = observer;
}
public async ValueTask AssignAsync(IAsyncDisposable subscription)
{
var shouldDispose = false;
lock (_gate)
{
if (_disposing)
{
shouldDispose = true;
}
else
{
_subscription = subscription;
}
}
if (shouldDispose)
{
await subscription.DisposeAsync().ConfigureAwait(false);
}
}
public async ValueTask DisposeAsync()
{
await FinishAsync().ConfigureAwait(false);
}
protected override async ValueTask OnCompletedAsyncCore()
{
ValueTask task;
lock (_gate)
{
if (_disposing)
{
return;
}
task = WithReentrancyFlagOn(static (@this, _) => @this._observer.OnCompletedAsync(), (object)null);
}
try
{
await task.ConfigureAwait(false);
}
finally
{
await FinishAsync().ConfigureAwait(false);
}
}
protected override async ValueTask OnErrorAsyncCore(Exception error)
{
ValueTask task;
lock (_gate)
{
if (_disposing)
{
return;
}
task = WithReentrancyFlagOn(static (@this, error) => @this._observer.OnErrorAsync(error), error);
}
try
{
await task.ConfigureAwait(false);
}
finally
{
await FinishAsync().ConfigureAwait(false);
}
}
protected override async ValueTask OnNextAsyncCore(T value)
{
ValueTask task;
lock (_gate)
{
if (_disposing)
{
return;
}
task = WithReentrancyFlagOn(static (@this, value) => @this._observer.OnNextAsync(value), value);
}
await task.ConfigureAwait(false);
}
private async ValueTask FinishAsync()
{
// On synchronous Rx, if Dispose is called while we're in the middle of an OnNext/OnError/OnCompleted,
// we immediately execute the Dispose() method.
// So it's possible that the On* method finishes after the Dispose() method has completed.
// What it's impossible is that another On* method STARTS AFTER Dispose() has completed.
Task onSomethingCall;
IAsyncDisposable subscription;
lock (_gate)
{
if (_disposing)
{
return;
}
_disposing = true;
subscription = _subscription;
onSomethingCall = _reentrancyFlag.Value ? null : _pendingOnSomethingCallsTcs?.Task;
}
if (onSomethingCall != null)
{
await onSomethingCall.ConfigureAwait(false);
}
if (subscription != null)
{
await subscription.DisposeAsync().ConfigureAwait(false);
}
}
private async ValueTask WithReentrancyFlagOn<TState>(Func<AutoDetachAsyncObserver, TState, ValueTask> asyncAction, TState state)
{
var runningMethod = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
_pendingOnSomethingCallsTcs = runningMethod;
_reentrancyFlag.Value = true;
try
{
await asyncAction(this, state).ConfigureAwait(false);
}
finally
{
_reentrancyFlag.Value = false;
_pendingOnSomethingCallsTcs = null;
runningMethod.SetResult(null!);
}
}
}
}
}