private static IObservable <Unit> CalmingWindows <T>(IObservable <T> observable, TimeSpan calmingPeriod, IScheduler scheduler) => Observable.Create <Unit>(observer => { var calmingTimer = new MultipleAssignmentDisposable(); var calmingWindows = new Subject <Unit>(); var calmerSubscription = observable.Subscribe( onNext: next => { calmingTimer.Disposable?.Dispose(); calmingTimer.Disposable = scheduler.Schedule( calmingPeriod, () => calmingWindows.OnNext(Unit.Default)); }, onError: exception => { calmingTimer.Dispose(); calmingWindows.OnError(exception); }, onCompleted: () => { calmingTimer.Dispose(); calmingWindows.OnCompleted(); }); return(new CompositeDisposable { calmingTimer, calmerSubscription, calmingWindows.Subscribe(observer), calmingWindows }); });
public void MultipleAssignmentDisposable() { var m = new MultipleAssignmentDisposable(); var disp1 = false; var d1 = Disposable.Create(() => { disp1 = true; }); m.Disposable = d1; Assert.Same(d1, m.Disposable); Assert.False(m.IsDisposed); var disp2 = false; var d2 = Disposable.Create(() => { disp2 = true; }); m.Disposable = d2; Assert.Same(d2, m.Disposable); Assert.False(m.IsDisposed); Assert.False(disp1); m.Dispose(); Assert.True(disp2); Assert.True(m.IsDisposed); //Assert.Null(m.Disposable); // BREAKING CHANGE v2 > v1.x - Undefined behavior after disposal. m.Disposable.Dispose(); // This should be a nop. var disp3 = false; var d3 = Disposable.Create(() => { disp3 = true; }); m.Disposable = d3; Assert.True(disp3); //Assert.Null(m.Disposable); // BREAKING CHANGE v2 > v1.x - Undefined behavior after disposal. m.Disposable.Dispose(); // This should be a nop. Assert.True(m.IsDisposed); }
public void AssignMultipleTimes() { int i = 0, j = 0, k = 0; var d = new MultipleAssignmentDisposable(); d.Disposable = Disposable.Create(() => i++); d.Disposable = Disposable.Create(() => j++); d.Dispose(); d.Disposable = Disposable.Create(() => k++); // immediately disposed Assert.AreEqual(0, i, "#1"); Assert.AreEqual(1, j, "#2"); Assert.AreEqual(1, k, "#3"); d.Dispose(); // invoke once more Assert.AreEqual(1, j, "#4"); Assert.AreEqual(1, k, "#5"); }
public static Func <Action <TResult>, Action <Exception>, IBackgroundTask> CreateForAsync <TResult>(FSharpAsync <TResult> async, DeviceDescriptionHolder dev) { return((onSuccess, onError) => { var disp = new MultipleAssignmentDisposable(); var backgroundTask = new AnonymousBackgroundTask(); backgroundTask.name = "Restore device " + dev.Name; backgroundTask.description = "Address: " + dev.Address; backgroundTask.state = BackgroundTaskState.InProgress; backgroundTask.disposable = Disposable.Create(() => { disp.Dispose(); backgroundTask.state = BackgroundTaskState.Canceled; }); disp.Disposable = async.Subscribe( res => { onSuccess(res); backgroundTask.state = BackgroundTaskState.Completed; }, err => { onError(err); backgroundTask.state = BackgroundTaskState.Failed; } ); return backgroundTask; }); }
public void SettingDisposableAfterDisposeCausesItToBeDisposed() { var subject = new MultipleAssignmentDisposable(); subject.Dispose(); var disposed = false; subject.Disposable = Disposable.Create(() => disposed = true); Assert.IsTrue(disposed); }
public void AssignNull() { int i = 0; var d = new MultipleAssignmentDisposable(); d.Disposable = Disposable.Create(() => i++); d.Disposable = null; // this should cleanup previous disposable away d.Dispose(); Assert.IsTrue(d.IsDisposed, "#1"); Assert.AreEqual(0, i, "#2"); }
public void DisposesUnderlyingDisposableOnce() { var disposed = 0; var subject = new MultipleAssignmentDisposable { Disposable = Disposable.Create(() => ++ disposed) }; Assert.AreEqual(0, disposed); for (var i = 0; i < 2; ++i) { subject.Dispose(); Assert.AreEqual(1, disposed); } }
public void MultipleAssignment() { var d = new MultipleAssignmentDisposable(); d.IsDisposed.IsFalse(); var id1 = new IdDisp(1); var id2 = new IdDisp(2); var id3 = new IdDisp(3); // dispose first d.Dispose(); d.IsDisposed.IsTrue(); d.Disposable = id1; id1.IsDisposed.IsTrue(); d.Disposable = id2; id2.IsDisposed.IsTrue(); d.Disposable = id3; id3.IsDisposed.IsTrue(); // normal flow d = new MultipleAssignmentDisposable(); id1 = new IdDisp(1); id2 = new IdDisp(2); id3 = new IdDisp(3); d.Disposable = id1; id1.IsDisposed.IsFalse(); d.Dispose(); id1.IsDisposed.IsTrue(); d.Disposable = id2; id2.IsDisposed.IsTrue(); d.Disposable = id3; id3.IsDisposed.IsTrue(); // exception flow d = new MultipleAssignmentDisposable(); id1 = new IdDisp(1); id2 = new IdDisp(2); id3 = new IdDisp(3); d.Disposable = id1; d.Disposable = id2; d.Disposable = id3; d.Dispose(); id1.IsDisposed.IsFalse(); id2.IsDisposed.IsFalse(); id3.IsDisposed.IsTrue(); // null d = new MultipleAssignmentDisposable(); id1 = new IdDisp(1); d.Disposable = null; d.Dispose(); d.Disposable = null; }
public static void MultipleAssignmentDisposable_WillAllowRotatingTheUnderlyingDisposable_WithoutDisposingOfTheReplacedDisposable() { var multipleAssignmentDisposable = new MultipleAssignmentDisposable(); var notDisposedDisposable = Disposable.Create(() => Console.WriteLine("Wouldn't you believe it, I ain't gonna be disposed. Estupido!")); var replacementDisposable = Disposable.Create(() => Console.WriteLine("I'm gonna be disposed by disposing of the multi assignement disposable.")); multipleAssignmentDisposable.Disposable = notDisposedDisposable; multipleAssignmentDisposable.Disposable = replacementDisposable; Console.WriteLine("The switch has been made, but the replaced disposable has not been disposed."); Console.WriteLine("Disposing of the multi assignement disposable."); multipleAssignmentDisposable.Dispose(); Console.WriteLine($"Multi assignement disposable is disposed: {multipleAssignmentDisposable.IsDisposed}."); }
public void Dispose() { lock (_subscription) { if (_state == State.Disposed) { return; } _emitter.Dispose(); _queue = null; _completion = null; _state = State.Disposed; _subscription.Dispose(); } }
public void MultipleAssignment() { var d = new MultipleAssignmentDisposable(); d.IsDisposed.IsFalse(); var id1 = new IdDisp(1); var id2 = new IdDisp(2); var id3 = new IdDisp(3); // dispose first d.Dispose(); d.IsDisposed.IsTrue(); d.Disposable = id1; id1.IsDisposed.IsTrue(); d.Disposable = id2; id2.IsDisposed.IsTrue(); d.Disposable = id3; id3.IsDisposed.IsTrue(); // normal flow d = new MultipleAssignmentDisposable(); id1 = new IdDisp(1); id2 = new IdDisp(2); id3 = new IdDisp(3); d.Disposable = id1; id1.IsDisposed.IsFalse(); d.Dispose(); id1.IsDisposed.IsTrue(); d.Disposable = id2; id2.IsDisposed.IsTrue(); d.Disposable = id3; id3.IsDisposed.IsTrue(); // exception flow d = new MultipleAssignmentDisposable(); id1 = new IdDisp(1); id2 = new IdDisp(2); id3 = new IdDisp(3); d.Disposable = id1; d.Disposable = id2; d.Disposable = id3; d.Dispose(); id1.IsDisposed.IsFalse(); id2.IsDisposed.IsFalse(); id3.IsDisposed.IsTrue(); // null d = new MultipleAssignmentDisposable(); id1 = new IdDisp(1); d.Disposable = null; d.Dispose(); d.Disposable = null; }
public void ReplacingDisposableDoesNotCauseItToBeDisposed() { var first = 0; var subject = new MultipleAssignmentDisposable { Disposable = Disposable.Create(() => ++ first) }; var second = 0; subject.Disposable = Disposable.Create(() => ++ second); Assert.AreEqual(0, first); Assert.AreEqual(0, second); subject.Dispose(); Assert.AreEqual(0, first); Assert.AreEqual(1, second); }
public async Task MultipleAssignmentDisposable_Test() { // arrange var d1 = new BooleanDisposable(); var d2 = new BooleanDisposable(); var d3 = new BooleanDisposable(); // arrange var disp = new MultipleAssignmentDisposable(); disp.Disposable = d1; // act & verify Assert.IsFalse(disp.IsDisposed); Assert.IsFalse(d1.IsDisposed); Assert.IsFalse(d2.IsDisposed); Assert.IsFalse(d3.IsDisposed); disp.Disposable = d2; disp.Dispose(); Assert.IsTrue(disp.IsDisposed); Assert.IsFalse(d1.IsDisposed); Assert.IsTrue(d2.IsDisposed); Assert.IsFalse(d3.IsDisposed); var gate = new ManualResetEventSlim(false); Task t = Task.Run(() => { gate.Wait(); disp.Disposable = d3; }); gate.Set(); await t; Assert.IsTrue(disp.IsDisposed); Assert.IsFalse(d1.IsDisposed); Assert.IsTrue(d2.IsDisposed); Assert.IsTrue(d3.IsDisposed); }
public static Func <Action <TResult>, Action <Exception>, IBackgroundTask> CreateForAsync <TResult>(FSharpAsync <TResult> async) { return((onSuccess, onError) => { var disp = new MultipleAssignmentDisposable(); var backgroundTask = new AnonymousBackgroundTask(); backgroundTask.name = "upgarde firmware"; backgroundTask.state = BackgroundTaskState.InProgress; backgroundTask.disposable = Disposable.Create(() => { disp.Dispose(); backgroundTask.state = BackgroundTaskState.Canceled; }); disp.Disposable = async.Subscribe( res => { onSuccess(res); backgroundTask.state = BackgroundTaskState.Completed; }, err => { onError(err); backgroundTask.state = BackgroundTaskState.Failed; } ); return backgroundTask; }); }
public void Dispose() { _disposables?.Dispose(); _subscriptionDisposable.Dispose(); }
public void Dispose() => _disposable.Dispose();
public void Dispose() { _disposable.Dispose(); }
public void Dispose() { stopper.Dispose(); }