public void TestDelay() { var e = new FirableEvent<char>(); var b = e.Hold(' '); var o = new List<char>(); var l = e.Delay().Snapshot(b).Subscribe(o.Add); e.Fire('C'); e.Fire('B'); e.Fire('A'); l.Dispose(); AssertArraysEqual(Arrays<char>.AsList('C', 'B', 'A'), o); }
public void TestCollect() { var ea = new FirableEvent<int>(); var o = new List<int>(); var sum = ea.Collect((a, s) => new Tuple<int, int>(a + s, a + s), 100); var l = sum.Subscribe(o.Add); ea.Fire(5); ea.Fire(7); ea.Fire(1); ea.Fire(2); ea.Fire(3); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(105, 112, 113, 115, 118), o); }
public void TestAccum() { var ea = new FirableEvent<int>(); var o = new List<int>(); var sum = ea.Accum((a, s) => a + s, 100); var l = sum.Subscribe(o.Add); ea.Fire(5); ea.Fire(7); ea.Fire(1); ea.Fire(2); ea.Fire(3); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(105, 112, 113, 115, 118), o); }
public void TestCoalesce() { var e1 = new FirableEvent<int>(); var e2 = new FirableEvent<int>(); var o = new List<int>(); var evt2 = e1.Map(x => x * 100); var evt = evt2 | e2; var l = e1.Merge(evt) .Coalesce((a, b) => a + b) .Subscribe(o.Add); e1.Fire(2); e1.Fire(8); e2.Fire(40); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(202, 808, 40), o); }
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); }
private void InitializeMouseHandler() { mouseButtonEvent = new FirableEvent<MouseStatus>(); mouseMoveEvent = new FirableEvent<MouseEventArgs>(); mouseButtonBehavior = mouseButtonEvent.Hold(MouseStatus.Up); this.mouseButtonDown = new EqualityPredicate<MouseStatus>(mouseButtonBehavior, MouseStatus.Down); mouseDragEvent = mouseMoveEvent.Gate(this.mouseButtonDown); mouseButtonBehavior.SubscribeWithInitialFire(a => { status.Text = string.Format("{0} {1}", a, DateTime.Now); }); mouseDragEvent.Map(e => new Tuple<string, string>(e.X.ToString(), e.Y.ToString())).Subscribe(t => { x.Text = t.Item1; y.Text = t.Item2; }); MouseMove += (s, e) => mouseMoveEvent.Fire(e); MouseDown += (s, e) => mouseButtonEvent.Fire(MouseStatus.Down); MouseUp += (s, e) => mouseButtonEvent.Fire(MouseStatus.Up); }
public void Test(int iterations) { var sink = new FirableEvent<int>(); var obserable = sink.Hold(0); var listener = obserable.SubscribeWithInitialFire(tt => { }); var i = 0; while (i < iterations) { sink.Fire(i); i++; } listener.Dispose(); }
private void InitializeMouseHandler() { // Initialize the Sodium Event evt = new FirableEvent<MouseEventArgs>(); formattedEvent = evt.Map(e => new Tuple<string, string>(e.X.ToString(), e.Y.ToString())); formattedEvent.Subscribe(t => { x.Text = t.Item1; y.Text = t.Item2; }); // Register the mouse move event to fire on the Potassium.Event MouseMove += (s, e) => evt.Fire(e); }
public void Test(int iterations) { var evt = new FirableEvent<int>(); Event<int> nestedEvent = new Event<int>(); var behaviorOfEvents = evt.Map(x => nestedEvent).Hold(nestedEvent); var observable = behaviorOfEvents.Switch(); var listen = observable.Subscribe(tt => { }); var i = 0; while (i < iterations) { evt.Fire(i); i++; } listen.Dispose(); observable.Dispose(); }
public void Test(int iterations) { var evt = new FirableEvent<int>(); var et = new Event<int>(); var behavior = et.Hold(0); var eventOfBehaviors = evt.Map(x => behavior).Hold(behavior); var observable = eventOfBehaviors.Switch(); var l = observable.SubscribeWithInitialFire(tt => { }); var i = 0; while (i < iterations) { evt.Fire(i); i++; } l.Dispose(); }
public void TestCombine() { var e1 = new FirableEvent<int>(); var b1 = e1.Hold(100); var e2 = new FirableEvent<int>(); var b2 = e2.Hold(200); var b = b1.Combine(b2, (x, y) => x + y); var results = new List<int>(); b.SubscribeWithInitialFire(results.Add); e1.Fire(101); e2.Fire(250); AssertArraysEqual(Arrays<int>.AsList(300, 301, 351), results); }
public void TestApply() { var pbf = new FirableEvent<Func<long, string>>(); var bf = new Behavior<Func<long, string>>(b => "1 " + b, pbf); var pba = new FirableEvent<long>(); var ba = new Behavior<long>(5L, pba); var results = new List<string>(); var apply = Functor.Apply(bf, ba); var listener = apply.SubscribeWithInitialFire(results.Add); pbf.Fire(b => "12 " + b); pba.Fire(6L); listener.Dispose(); apply.Dispose(); bf.Dispose(); ba.Dispose(); AssertArraysEqual(Arrays<string>.AsList("1 5", "12 5", "12 6"), results); }
public void TestRepeat2() { var ea = new FirableEvent<int>(); var eb = new EventRepeater<int>(); var evt = ea.Map(x => x % 10); var ec = evt.Merge(eb).Coalesce((x, y) => x + y); var ebO = ea.Map(x => x / 10) & (x => x != 0); eb.Repeat(ebO); var o = new List<int>(); var l = ec.Subscribe(o.Add); ea.Fire(2); ea.Fire(52); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(2, 7), o); }
public void TestRepeat1() { FirableEvent<int> src = new FirableEvent<int>(); var r = src.Repeat(); var o = new List<int>(); var s = r.Subscribe(o.Add); src.Fire(1); src.Fire(2); src.Fire(3); AssertArraysEqual(Arrays<int>.AsList(1,2,3), o); s.Dispose(); r.Dispose(); src.Dispose(); }
public void TestPublishEvent() { var e = new FirableEvent<int>(); var o = new List<int>(); var l = e.Subscribe(o.Add); e.Fire(5); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(5), o); e.Fire(6); AssertArraysEqual(Arrays<int>.AsList(5), o); }
public void TestMapB3() { var publisher = new FirableEvent<int>(); var behavior = new Behavior<int>(1, publisher); var behavior1 = behavior.Map(x => x * 3); var results = new List<int>(); var listener = behavior1.SubscribeWithInitialFire(results.Add); publisher.Fire(2); listener.Dispose(); behavior.Dispose(); AssertArraysEqual(Arrays<int>.AsList(3, 6), results); }
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 TestFilterNotNull() { var e = new FirableEvent<string>(); var o = new List<string>(); var l = e.Filter(x => x != null).Subscribe(o.Add); e.Fire("tomato"); e.Fire(null); e.Fire("peach"); l.Dispose(); AssertArraysEqual(Arrays<string>.AsList("tomato", "peach"), o); }
public void TestMap() { var e = new FirableEvent<int>(); var m = e.Map(x => x.ToString(CultureInfo.InvariantCulture)); var o = new List<string>(); var l = m.Subscribe(o.Add); e.Fire(5); l.Dispose(); AssertArraysEqual(Arrays<string>.AsList("5"), o); }
public void TestHold() { var evt = new FirableEvent<int>(); var behavior = evt.Hold(0); var results = new List<int>(); var listener = behavior.Subscribe(results.Add); evt.Fire(2); evt.Fire(9); listener.Dispose(); evt.Dispose(); behavior.Dispose(); AssertArraysEqual(Arrays<int>.AsList(2, 9), results); }
public void TestSwitchE() { var ese = new FirableEvent<Se>(); var ea = ese.Map(s => s.C1); var eb = ese.Map(s => s.C2); var tmp1 = ese.Map(s => s.Event); var tmp2 = tmp1.Filter(x => x != null); var bsw = tmp2.Hold(ea); var o = new List<char>(); var eo = bsw.Switch(); var l = eo.Subscribe(o.Add); ese.Fire(new Se('A', 'a', null)); ese.Fire(new Se('B', 'b', null)); ese.Fire(new Se('C', 'c', eb)); ese.Fire(new Se('D', 'd', null)); ese.Fire(new Se('E', 'e', ea)); ese.Fire(new Se('F', 'f', null)); ese.Fire(new Se('G', 'g', eb)); ese.Fire(new Se('H', 'h', ea)); ese.Fire(new Se('I', 'i', ea)); l.Dispose(); ese.Dispose(); ea.Dispose(); eb.Dispose(); bsw.Dispose(); eo.Dispose(); AssertArraysEqual(Arrays<char>.AsList('A', 'B', 'C', 'd', 'e', 'F', 'G', 'h', 'I'), o); }
public void TestSwitchB() { var sink = new FirableEvent<Sb>(); // Split each field o of SB so we can update multiple behaviors in a // single transaction. var behaviorA = sink.Map(s => s.C1).Filter(x => x != null).Hold('A'); var behaviorB = sink.Map(s => s.C2).Filter(x => x != null).Hold('a'); var bsw = sink.Map(s => s.Behavior).Filter(x => x != null).Hold(behaviorA); var behavior = bsw.Switch(); var results = new List<char>(); var listener = behavior.SubscribeWithInitialFire(c => { Assert.IsNotNull(c, "c != null"); results.Add(c.Value); }); sink.Fire(new Sb('B', 'b', null)); sink.Fire(new Sb('C', 'c', behaviorB)); sink.Fire(new Sb('D', 'd', null)); sink.Fire(new Sb('E', 'e', behaviorA)); sink.Fire(new Sb('F', 'f', null)); sink.Fire(new Sb(null, null, behaviorB)); sink.Fire(new Sb(null, null, behaviorA)); sink.Fire(new Sb('G', 'g', behaviorB)); sink.Fire(new Sb('H', 'h', behaviorA)); sink.Fire(new Sb('I', 'i', behaviorA)); listener.Dispose(); behaviorA.Dispose(); behaviorB.Dispose(); bsw.Dispose(); behavior.Dispose(); AssertArraysEqual(Arrays<char>.AsList('A', 'B', 'c', 'd', 'E', 'F', 'f', 'F', 'g', 'H', 'I'), results); }
public void TestLiftGlitch() { var publisher = new FirableEvent<int>(); var behavior = new Behavior<int>(1, publisher); var mappedBehavior1 = behavior.Map(x => x * 3); var mappedBehavior2 = behavior.Map(x => x * 5); var results = new List<string>(); var combinedBehavior = Functor.Lift((x, y) => x + " " + y, mappedBehavior1, mappedBehavior2); var listener = combinedBehavior.SubscribeWithInitialFire(results.Add); publisher.Fire(2); listener.Dispose(); behavior.Dispose(); AssertArraysEqual(Arrays<string>.AsList("3 5", "6 10"), results); }
public void TestMergeNonSimultaneous() { var e1 = new FirableEvent<int>(); var e2 = new FirableEvent<int>(); var o = new List<int>(); var l = e1.Merge(e2).Subscribe(o.Add); e1.Fire(7); e2.Fire(9); e1.Fire(8); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(7, 9, 8), o); }
public void TestLift() { var pub1 = new FirableEvent<int>(); var behavior1 = new Behavior<int>(1, pub1); var pub2 = new FirableEvent<long>(); var behavior2 = new Behavior<long>(5L, pub2); var results = new List<string>(); var combinedBehavior = Functor.Lift((x, y) => x + " " + y, behavior1, behavior2); var listener = combinedBehavior.SubscribeWithInitialFire(results.Add); pub1.Fire(12); pub2.Fire(6L); listener.Dispose(); behavior1.Dispose(); behavior2.Dispose(); AssertArraysEqual(Arrays<string>.AsList("1 5", "12 5", "12 6"), results); }
public void TestMergeSimultaneous() { var e = new FirableEvent<int>(); var o = new List<int>(); var l = e.Merge(e).Subscribe(o.Add); e.Fire(7); e.Fire(9); l.Dispose(); AssertArraysEqual(Arrays<int>.AsList(7, 7, 9, 9), o); }
public void TestFilter() { var e = new FirableEvent<char>(); var o = new List<char>(); var l = (e & (char.IsUpper)).Subscribe(o.Add); e.Fire('H'); e.Fire('o'); e.Fire('I'); l.Dispose(); AssertArraysEqual(Arrays<char>.AsList('H', 'I'), o); }
public void TestOnce() { var e = new FirableEvent<char>(); var o = new List<char>(); var l = e.Once().Subscribe(o.Add); e.Fire('A'); e.Fire('B'); e.Fire('C'); l.Dispose(); AssertArraysEqual(Arrays<char>.AsList('A'), o); }
public void TestGate() { var enabled = new IdentityPredicate(true); var ec = new FirableEvent<char>(); var epred = new QueryPredicate(() => enabled.Value); var o = new List<char>(); var l = ec.Gate(epred).Subscribe(o.Add); ec.Fire('H'); enabled = new IdentityPredicate(false); ec.Fire('O'); enabled = new IdentityPredicate(true); ec.Fire('I'); l.Dispose(); AssertArraysEqual(Arrays<char>.AsList('H', 'I'), o); }
public void TestMapBLateListen() { var publisher = new FirableEvent<int>(); var behavior = new Behavior<int>(6, publisher); var results = new List<string>(); var map = behavior.Map(x => x.ToString(CultureInfo.InvariantCulture)); publisher.Fire(2); var listener = map.SubscribeWithInitialFire(results.Add); publisher.Fire(8); listener.Dispose(); map.Dispose(); behavior.Dispose(); AssertArraysEqual(Arrays<string>.AsList("2", "8"), results); }