Esempio n. 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);
        }
Esempio n. 2
0
 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);
 }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
 public void TestCollect()
 {
     var ea = new FirableEvent<int>();
     var o = new List<int>();
     var sum = ea.Hold(100).Collect((a, s) => new Tuple<int, int>(a + s, a + s), 0);
     var l = sum.SubscribeWithInitialFire(o.Add);
     ea.Fire(5);
     ea.Fire(7);
     ea.Fire(1);
     ea.Fire(2);
     ea.Fire(3);
     l.Dispose();
     ea.Dispose();
     sum.Dispose();
     AssertArraysEqual(Arrays<int>.AsList(100, 105, 112, 113, 115, 118), o);
 }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public Form1()
        {
            InitializeComponent();

            startBtn.Click += (o, s) => runningEvent.Fire(true);

            stopBtn.Click += (o, s) =>
            {
                degreesSignal.Stop();
                runningEvent.Fire(false);
            };

            frequency.ValueChanged += (o, s) => intervalChanged.Fire(frequency.Value);

            degreesSignal = new Signal<double>(new AutoDouble(0.0, 0.001), Frequency.Hz(0.0), this.CreateDispatcher());
            degreesSignal.Subscribe(SetDegValue);

            signalTickCount = degreesSignal.Snapshot(new AutoLong());
            signalTickCount.Subscribe(SetTickCount);

            radiansSignal = degreesSignal.Map(d => d * RadsPerDeg);
            radiansSignal.Subscribe(SetRadValue);

            sineSignal = radiansSignal.Map(Math.Sin);
            sineSignal.Subscribe(SetSinValue);

            cosineSignal = radiansSignal.Map(Math.Cos);
            cosineSignal.Subscribe(SetCosValue);

            runningEvent = new FirableEvent<bool>();
            runningEvent.Subscribe(r => { degreesSignal.Running = r; });

            runningBehavior = runningEvent.Hold(false);
            runningBehavior.SubscribeWithInitialFire(EnableControls);

            intervalChanged = new FirableEvent<decimal>();
            intervalBehavior = intervalChanged.Hold(frequency.Value);
            intervalBehavior.Map(Frequency.Hz).SubscribeWithInitialFire(FrequencyChanged);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
 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);
 }
Esempio n. 9
0
 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);
 }