-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathScheduledAsyncObserverBase.cs
More file actions
158 lines (134 loc) · 4.45 KB
/
Copy pathScheduledAsyncObserverBase.cs
File metadata and controls
158 lines (134 loc) · 4.45 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
// 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.Collections.Generic;
using System.Reactive.Threading;
using System.Threading;
using System.Threading.Tasks;
namespace System.Reactive
{
internal abstract class ScheduledAsyncObserverBase<T> : AsyncObserverBase<T>, IScheduledAsyncObserver<T>
{
private readonly IAsyncObserver<T> _observer;
private readonly IAsyncGate _lock = AsyncGate.Create();
private readonly Queue<T> _queue = new();
private bool _hasFaulted = false;
private bool _busy = false;
private bool _done;
private Exception _error;
public ScheduledAsyncObserverBase(IAsyncObserver<T> observer)
{
_observer = observer;
}
public ValueTask EnsureActive() => EnsureActive(1);
public async ValueTask EnsureActive(int count)
{
var shouldRun = false;
using (await _lock.LockAsync().ConfigureAwait(false))
{
if (!_hasFaulted && !_busy)
{
_busy = true;
shouldRun = true;
}
}
if (shouldRun)
{
await ScheduleAsync().ConfigureAwait(false);
}
}
protected abstract ValueTask ScheduleAsync();
protected async ValueTask RunAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var values = default(T[]);
var error = default(Exception);
var done = false;
using (await RendezVous(_lock.LockAsync()))
{
if (_queue.Count > 0)
{
values = _queue.ToArray();
_queue.Clear();
}
if (_done)
{
done = _done;
error = _error;
}
if (values == null && !done)
{
_busy = false;
break;
}
}
try
{
if (values != null)
{
foreach (var value in values)
{
await RendezVous(_observer.OnNextAsync(value));
}
}
if (done)
{
if (error != null)
{
await RendezVous(_observer.OnErrorAsync(error));
}
else
{
await RendezVous(_observer.OnCompletedAsync());
}
break;
}
}
catch
{
using (await RendezVous(_lock.LockAsync()))
{
_hasFaulted = true;
_queue.Clear();
}
throw;
}
}
}
protected override async ValueTask OnCompletedAsyncCore()
{
using (await _lock.LockAsync().ConfigureAwait(false))
{
if (!_hasFaulted)
{
_done = true;
}
}
}
protected override async ValueTask OnErrorAsyncCore(Exception error)
{
using (await _lock.LockAsync().ConfigureAwait(false))
{
if (!_hasFaulted)
{
_done = true;
_error = error;
}
}
}
protected override async ValueTask OnNextAsyncCore(T value)
{
using (await _lock.LockAsync().ConfigureAwait(false))
{
if (!_hasFaulted)
{
_queue.Enqueue(value);
}
}
}
protected abstract ValueTaskAwaitable RendezVous(ValueTask task);
protected abstract ValueTaskAwaitable<R> RendezVous<R>(ValueTask<R> task);
public abstract ValueTask DisposeAsync();
}
}