Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
 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);
 }
Beispiel #4
0
        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();
        }
Beispiel #5
0
        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();
        }
Beispiel #6
0
 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);
 }
Beispiel #7
0
 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 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);
        }