Last active
July 31, 2020 17:52
-
-
Save canton7/767ea4fde94984323b31 to your computer and use it in GitHub Desktop.
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 ReactiveExtensionsExtensions | |
{ | |
public static async Task SubscribeAsync<T>(this IObservable<T> observable, Action<T> onNext, CancellationToken? cancellationToken = null) | |
{ | |
CancellationToken token = cancellationToken ?? CancellationToken.None; | |
var cts = new TaskCompletionSource<bool>(); | |
using(token.Register(() => cts.SetCanceled())) | |
{ | |
observable.Subscribe(onNext, ex => cts.SetException(ex), () => cts.SetResult(true), token); | |
await cts.Task; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment