Created
October 22, 2021 17:35
-
-
Save Hackmodford/af28c6ae83908ca2f0fd8aadcfc96a3f to your computer and use it in GitHub Desktop.
readonly BehaviorSubject aka BehaviorObservable
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
public interface IBehaviorObservable<T> : IObservable<T> | |
{ | |
T Value { get; } | |
bool TryGetValue(out T value); | |
} | |
/// <summary> | |
/// Exposes a BehaviorSubject's Value property while hiding the OnNext, OnError, OnComplete methods. | |
/// </summary> | |
/// <typeparam name="T">To be added.</typeparam> | |
public class BehaviorObservable<T> : IBehaviorObservable<T> | |
{ | |
private readonly BehaviorSubject<T> _behaviorSubject; | |
public BehaviorObservable(BehaviorSubject<T> behaviorSubject) | |
{ | |
_behaviorSubject = behaviorSubject; | |
} | |
public IDisposable Subscribe(IObserver<T> observer) => _behaviorSubject.Subscribe(observer); | |
public T Value => _behaviorSubject.Value; | |
public bool TryGetValue(out T value) | |
{ | |
return _behaviorSubject.TryGetValue(out value); | |
} | |
} | |
public static class BehaviorSubjectExtensions | |
{ | |
public static BehaviorObservable<T> ToBehaviorObservable<T>(this BehaviorSubject<T> bs) | |
{ | |
var bo = new BehaviorObservable<T>(bs); | |
return bo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment