public static IObservable <T> AsObservable <T>(this INotifyDisposable disposable, Func <T> lastValue = null) { return(Observable.Create <T>(observer => { if (disposable.IsDisposed) { if (lastValue != null) { observer.OnNext(lastValue()); } observer.OnCompleted(); return Disposables.Empty; } var handler = Disposables.Call(() => { if (lastValue != null) { observer.OnNext(lastValue()); } observer.OnCompleted(); }); disposable.Attach(handler, keepAlive: true); return Disposables.Call(() => { disposable.Dettach(handler); }); })); }
public static INotifyDisposable WhenDisposed(this INotifyDisposable source, Action action) { var call = Disposables.Call(action); source.Attach(call, keepAlive: true); return(source); }
public static T DisposedBy <T>(this T child, INotifyDisposable disposer) where T : IDisposable { if (child is Task) { throw new ArgumentException("Can not register Task for late disposal", nameof(child)); } disposer.Attach(child); return(child); }
public void Attach(IDisposable child, bool keepAlive = false) => _handler.Attach(child, keepAlive);