-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions.Observable.cs
45 lines (36 loc) · 1.11 KB
/
Extensions.Observable.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
using System;
namespace Open.Threading.Dataflow;
public static partial class DataFlowExtensions
{
class Observer<T> : IObserver<T>, IDisposable
{
public static Observer<T> New(
Action<T>? onNext,
Action<Exception>? onError,
Action? onCompleted) => new()
{
_onNext = onNext,
_onError = onError,
_onCompleted = onCompleted
};
Action? _onCompleted;
Action<Exception>? _onError;
Action<T>? _onNext;
public void OnNext(T value) => _onNext?.Invoke(value);
public void OnError(Exception error) => _onError?.Invoke(error);
public void OnCompleted() => _onCompleted?.Invoke();
public void Dispose()
{
_onNext = null;
_onError = null;
_onCompleted = null;
}
}
public static IDisposable Subscribe<T>(this IObservable<T> observable,
Action<T> onNext,
Action<Exception> onError,
Action? onCompleted = null) => observable.Subscribe(Observer<T>.New(onNext, onError, onCompleted));
public static IDisposable Subscribe<T>(this IObservable<T> observable,
Action<T> onNext,
Action? onCompleted = null) => observable.Subscribe(Observer<T>.New(onNext, null, onCompleted));
}