public void Test(int iterations) { var finalizers = new List<IDisposable>(); var behaviorMapFinalizers = new List<IDisposable>(); var evt = new FirableEvent<int?>(); finalizers.Add(evt); var behavior = evt.Hold(0); finalizers.Add(behavior); // TODO - etens isn't being used. Seems like it should be var etens = evt.Map(x => x / 10); finalizers.Add(etens); var snapshotEvent = evt.Snapshot((neu, old) => neu.Equals(old) ? null : neu, behavior); finalizers.Add(snapshotEvent); var changeTens = snapshotEvent.Filter(x => x != null); finalizers.Add(changeTens); var eventOfBehaviors = changeTens.Map(tens => { DisposeFinalizers(behaviorMapFinalizers); var mapped = behavior.Map(tt => new Tuple<int?, int?>(tens, tt)); behaviorMapFinalizers.Add(mapped); return mapped; }); finalizers.Add(eventOfBehaviors); var behaviorMap = behavior.Map(tt => new Tuple<int?, int?>(0, tt)); finalizers.Add(behaviorMap); var tensTupleWrappedBehavior = eventOfBehaviors.Hold(behaviorMap); finalizers.Add(tensTupleWrappedBehavior); var tensTupleBehavior = tensTupleWrappedBehavior.Switch(); finalizers.Add(tensTupleBehavior); var listener = tensTupleBehavior.SubscribeWithInitialFire(tu => { }); var i = 0; while (i < iterations) { evt.Fire(i); i++; } listener.Dispose(); DisposeFinalizers(finalizers); DisposeFinalizers(behaviorMapFinalizers); }
public void TestSnapshot() { var publisher = new FirableEvent<int>(); var behavior = new Behavior<int>(0, publisher); var evt = new FirableEvent<long>(); var results = new List<string>(); Func<long, int, string> snapshotFunction = (x, y) => string.Format("{0} {1}", x, y); var listener = evt.Snapshot(snapshotFunction, behavior).Subscribe(results.Add); evt.Fire(100L); publisher.Fire(2); evt.Fire(200L); publisher.Fire(9); publisher.Fire(1); evt.Fire(300L); listener.Dispose(); behavior.Dispose(); evt.Dispose(); AssertArraysEqual(Arrays<string>.AsList("100 0", "200 2", "300 1"), results); }
public void TestHoldIsDelayed() { var evt = new FirableEvent<int>(); var behavior = evt.Hold(0); var pair = evt.Snapshot((a, b) => a + " " + b, behavior); var results = new List<string>(); var listener = pair.Subscribe(results.Add); evt.Fire(2); evt.Fire(3); listener.Dispose(); evt.Dispose(); behavior.Dispose(); pair.Dispose(); AssertArraysEqual(Arrays<string>.AsList("2 0", "3 2"), results); }