Created
January 13, 2014 04:40
-
-
Save robfe/8394805 to your computer and use it in GitHub Desktop.
Ensures that specific observables complete in serial (by a particular key)
This file contains hidden or 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 static class MultiplexedOperationDispatcher | |
| { | |
| static readonly Subject<Tuple<string, IObservable<Unit>>> Operations = new Subject<Tuple<string, IObservable<Unit>>>(); | |
| static MultiplexedOperationDispatcher() | |
| { | |
| Operations.GroupBy(x => x.Item1).Subscribe(g => g.Select(x => x.Item2).Concat().Subscribe()); | |
| } | |
| public static IObservable<T> DispatchedByLockKey<T>(this IObservable<T> source, string lockKey) | |
| { | |
| return Observable.Create<T>(sourceObserver => | |
| { | |
| var disposable = new SingleAssignmentDisposable(); | |
| Operations.OnNext(new Tuple<string, IObservable<Unit>>(lockKey, Observable.Create<Unit>(unitObserver => | |
| { | |
| disposable.Disposable = source.Finally(unitObserver.OnCompleted).Subscribe(sourceObserver); | |
| return disposable; | |
| }))); | |
| return disposable; | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment