/
FirstLastBlocking.cs
53 lines (45 loc) · 1.23 KB
/
FirstLastBlocking.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Threading;
namespace System.Reactive.Linq.ObservableImpl
{
internal abstract class BaseBlocking<T> : ManualResetEventSlim, IObserver<T>
{
internal T _value;
internal bool _hasValue;
internal Exception _error;
internal BaseBlocking() { }
public void OnCompleted()
{
Set();
}
public void OnError(Exception error)
{
_value = default;
_error = error;
Set();
}
public abstract void OnNext(T value);
}
internal sealed class FirstBlocking<T> : BaseBlocking<T>
{
public override void OnNext(T value)
{
if (!_hasValue)
{
_value = value;
_hasValue = true;
Set();
}
}
}
internal sealed class LastBlocking<T> : BaseBlocking<T>
{
public override void OnNext(T value)
{
_value = value;
_hasValue = true;
}
}
}