Created
December 5, 2016 15:57
-
-
Save GeorgeTsiokos/8c0414c187eca0e881d9cf6ffd6d4c59 to your computer and use it in GitHub Desktop.
Rx Subscribe where onNext returns a IDisposable
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 static IDisposable Subscribe<TSource> (this IObservable<TSource> sequence, Func<TSource, IDisposable> onNext) { | |
var disposePreviousValues = sequence.Aggregate (Disposable.Empty, | |
(disposable, value) => { | |
using (disposable) | |
return onNext (value); | |
}); | |
var disposeLastValue = disposePreviousValues.TakeLast (1).Subscribe (last => last.Dispose ()); | |
return disposeLastValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment