Skip to content

Instantly share code, notes, and snippets.

@robfe
Created January 13, 2014 04:40
Show Gist options
  • Select an option

  • Save robfe/8394805 to your computer and use it in GitHub Desktop.

Select an option

Save robfe/8394805 to your computer and use it in GitHub Desktop.
Ensures that specific observables complete in serial (by a particular key)
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