Created
February 21, 2021 00:43
-
-
Save MelbourneDeveloper/b74f845265c2c8fd27aa0ef1a18ff0de to your computer and use it in GitHub Desktop.
Observer with Null Object Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Observer<T> : IObserver<T> | |
{ | |
static Action<Exception> onError = ((e) => { }); | |
static Action onCompleted = (() => { }); | |
private readonly Action<T> _onNext; | |
private readonly Action<Exception> _onError; | |
private readonly Action _onCompleted; | |
public Observer( | |
Action<T> onNext, | |
Action<Exception>? onError = null, | |
Action? onCompleted = null | |
) | |
{ | |
_onNext = onNext; | |
_onError = onError ?? Observer<T>.onError; | |
_onCompleted = onCompleted ?? Observer<T>.onCompleted; | |
} | |
public void OnCompleted() => _onCompleted(); | |
public void OnError(Exception error) => _onError(error); | |
public void OnNext(T value) => _onNext(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment