public virtual AsyncSubject <TSource> GetAwaiter <TSource>(IConnectableObservable <TSource> source) { var s = new AsyncSubject <TSource>(); source.SubscribeSafe(s); source.Connect(); return(s); }
public static AwaitableAsyncSubject <TSource> GetAwaiter <TSource>(this IConnectableObservable <TSource> source) { var s = new AwaitableAsyncSubject <TSource>(); source.SubscribeSafe(s); source.Connect(); return(s); }
public virtual AsyncSubject <TSource> RunAsync <TSource>(IConnectableObservable <TSource> source, CancellationToken cancellationToken) { var s = new AsyncSubject <TSource>(); if (cancellationToken.IsCancellationRequested) { return(Cancel(s)); } var d = source.SubscribeSafe(s); var c = source.Connect(); if (cancellationToken.CanBeCanceled) { RegisterCancelation(s, new CompositeDisposable(d, c), cancellationToken); } return(s); }
public static AwaitableAsyncSubject <TSource> RunAsync <TSource>(this IConnectableObservable <TSource> source, CancellationToken cancellationToken) { var s = new AwaitableAsyncSubject <TSource>(); var cancel = new Action(() => s.OnError(new OperationCanceledException())); if (cancellationToken.IsCancellationRequested) { cancel(); return(s); } var d = new CompositeDisposable(source.SubscribeSafe(s), source.Connect()); cancellationToken.Register(d.Dispose); cancellationToken.Register(cancel); return(s); }
public static IObservable <TSource> Prime <TSource>( this IConnectableObservable <TSource> source, Action <IDisposable> whenConnected) { Contract.Requires(source != null); Contract.Requires(whenConnected != null); Contract.Ensures(Contract.Result <IObservable <TSource> >() != null); object gate = new object(); bool isFirst = true; return(Observable.Create <TSource>( observer => { var subscription = source.SubscribeSafe(observer); lock (gate) { if (isFirst) { isFirst = false; var connection = source.Connect(); whenConnected(Disposable.Create(() => { lock (gate) { if (!isFirst) { connection.Dispose(); isFirst = true; } } })); } } return subscription; })); }