Skip to content

Instantly share code, notes, and snippets.

@TheFo2sh
Created February 9, 2020 22:22
Show Gist options
  • Save TheFo2sh/b9e15108e399a0c88fbc2294a7ddd6c9 to your computer and use it in GitHub Desktop.
Save TheFo2sh/b9e15108e399a0c88fbc2294a7ddd6c9 to your computer and use it in GitHub Desktop.
public class ObservableCancellationTokenSource<T> : CancellationTokenSource,IDisposable
{
private readonly IDisposable _subscription;
public ObservableCancellationTokenSource(IObservable<T> observable)
{
_subscription = observable.Subscribe(token=>this.Cancel());
}
public new void Dispose()
{
_subscription.Dispose();
base.Dispose();
}
}
public static class Extension
{
public static ObservableCancellationTokenSource<T> ToCancellationTokenSource<T>(this IObservable<T> source)
{
return new ObservableCancellationTokenSource<T>(source);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment