I feel like I'm trying to reinvent a wheel, so I better ask.
GIVEN
- that I have an
Observable<T> source
- and
Task LoadAsync<T>(T value)
method
WHEN
I use Select/Switch pattern to call LoadMethod when source emits
observable .Select(value => Observable.FromAsync(cancellationToken => LoadAsync(value, cancellationToken))) .Switch() .Subscribe();
THEN
- How do I add reload functionality?
- How do I report IsLoading status: whether the
LoadAsync
is running - How to cancel
LoadAsync
whensource
completes
I want to create some reusable method, or class, that would implement answers to #1 and #2.
I have this so far: https://dotnetfiddle.net/0zPhBE
public class ReactiveLoader<T> : IDisposable
{
private readonly BehaviorSubject<bool> _isLoading = new(false);
private readonly Subject<Unit> _completes = new();
private readonly Subject<T> _reloads = new Subject<T>();
private readonly IDisposable _subscription;
public bool IsLoading => _isLoading.Value;
public IObservable<bool> IsLoadingObservable => _isLoading.Skip(1).DistinctUntilChanged(); //Not nice
public ReactiveLoader(IObservable<T> observable, Func<T, CancellationToken, Task> handler)
{
_subscription = observable
.Finally(() => //Not nice
{
_completes.OnNext(Unit.Default);
})
.Merge(_reloads)
.Do(_ => _isLoading.OnNext(true))
.Select(value => Observable.FromAsync(cancellationToken => handler(value, cancellationToken)))
.Switch()
.Do(_ => _isLoading.OnNext(false))
.TakeUntil(_completes) //cancels loading when observable completes
.Subscribe();
}
public void Reload()
{
_reloads.OnNext(??); //needs last value of source
}
public void Dispose()
{
_completes.OnNext(Unit.Default);
_subscription.Dispose();
}
}