Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created February 21, 2021 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/b74f845265c2c8fd27aa0ef1a18ff0de to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/b74f845265c2c8fd27aa0ef1a18ff0de to your computer and use it in GitHub Desktop.
Observer with Null Object Pattern
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