Example #1
0
        public async Task TestListenOnceAsync()
        {
            BehaviorSink <int> b = Behavior.CreateSink(9);
            int result           = await Transaction.Run(() => Operational.Value(b).ListenOnceAsync());

            b.Send(2);
            b.Send(7);
            Assert.AreEqual(9, result);
        }
Example #2
0
    public STextBox(String initText, int width)
    {
      Text = initText;
      Width = width;

      BehaviorSink<String> text = new BehaviorSink<String>(initText);
      TextBehavior = text;
      TextChanged += (sender, args) => text.Send(this.Text);
    }
Example #3
0
        public SComboBox(IEnumerable <TA> aModel)
        {
            ItemsSource = aModel;

            BehaviorSink <TA> selectedItem = new BehaviorSink <TA>((TA)SelectedItem);

            SelectionChanged    += (sender, args) => selectedItem.Send((TA)SelectedItem);
            SelectedItemBehavior = selectedItem;
        }
 public void TestValuesThenMap()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   Listener l = b.Value().Map<int>(x => x + 100).Listen(x => { @out.Add(x); });
   b.Send(2);
   b.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
 }
 public void TestValues()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   var l = b.Value().Listen(x => @out.Add(x));
   b.Send(2);
   b.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
 }
Example #6
0
        public STextBox(String initText, int width)
        {
            Text  = initText;
            Width = width;

            BehaviorSink <String> text = new BehaviorSink <String>(initText);

            TextBehavior = text;
            TextChanged += (sender, args) => text.Send(this.Text);
        }
Example #7
0
        public void TestValuesThenMap()
        {
            var      b    = new BehaviorSink <int>(9);
            var      @out = new List <int>();
            Listener l    = b.Value().Map <int>(x => x + 100).Listen(x => { @out.Add(x); });

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
        }
Example #8
0
        public void TestUpdates()
        {
            BehaviorSink <int> b    = Behavior.CreateSink(9);
            List <int>         @out = new List <int>();
            IListener          l    = Operational.Updates(b).Listen(@out.Add);

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
        }
Example #9
0
        public void TestValuesTwiceThenCoalesce()
        {
            var      b    = new BehaviorSink <int>(9);
            var      @out = new List <int>();
            Listener l    = DoubleUp(b.Value()).Coalesce((fst, snd) => fst + snd).Listen(x => { @out.Add(x); });

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 18, 4, 14 }, @out);
        }
Example #10
0
        public void TestValuesThenFilter()
        {
            var      b    = new BehaviorSink <int>(9);
            var      @out = new List <int>();
            Listener l    = b.Value().Filter(a => true).Listen(x => { @out.Add(x); });

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Example #11
0
        public void TestMapB()
        {
            var      b    = new BehaviorSink <int>(6);
            var      @out = new List <String>();
            Listener l    = b.Map(x => x.ToString())
                            .Value().Listen(x => { @out.Add(x); });

            b.Send(8);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "6", "8" }, @out);
        }
Example #12
0
        public void TestValuesThenCoalesce()
        {
            var      b    = new BehaviorSink <int>(9);
            var      @out = new List <int>();
            Listener l    = b.Value().Coalesce((fst, snd) => snd).Listen(x => { @out.Add(x); });

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Example #13
0
        public void TestValues()
        {
            var b    = new BehaviorSink <int>(9);
            var @out = new List <int>();
            var l    = b.Value().Listen(x => @out.Add(x));

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Example #14
0
        public void TestListenOnce()
        {
            BehaviorSink <int> b    = Behavior.CreateSink(9);
            List <int>         @out = new List <int>();
            IListener          l    = Transaction.Run(() => Operational.Value(b).ListenOnce(@out.Add));

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
Example #15
0
        public void TestValuesTwiceThenOnce()
        {
            var      b    = new BehaviorSink <int>(9);
            var      @out = new List <int>();
            Listener l    = DoubleUp(b.Value()).Once().Listen(x => { @out.Add(x); });

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
Example #16
0
        public void TestApply()
        {
            var      bf   = new BehaviorSink <Func <long, String> >(b => "1 " + b);
            var      ba   = new BehaviorSink <long>(5L);
            var      @out = new List <String>();
            Listener l    = Behavior <long> .Apply(bf, ba).Value().Listen(x => { @out.Add(x); });

            bf.Send(b => "12 " + b);
            ba.Send(6L);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
        }
Example #17
0
        public void TestValuesLateListen()
        {
            var         b     = new BehaviorSink <int>(9);
            var         @out  = new List <int>();
            Event <int> value = b.Value();

            b.Send(8);
            Listener l = value.Listen(x => { @out.Add(x); });

            b.Send(2);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 8, 2 }, @out);
        }
Example #18
0
        public void TestValuesThenMerge()
        {
            var      bi   = new BehaviorSink <int>(9);
            var      bj   = new BehaviorSink <int>(2);
            var      @out = new List <int>();
            Listener l    = Event <int> .MergeWith((x, y) => x + y, bi.Value(), bj.Value())
                            .Listen(x => { @out.Add(x); });

            bi.Send(1);
            bj.Send(4);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 11, 1, 4 }, @out);
        }
Example #19
0
        public void TestValuesTwiceThenSnapshot()
        {
            var      bi   = new BehaviorSink <int>(9);
            var      bc   = new BehaviorSink <char>('a');
            var      @out = new List <char>();
            Listener l    = DoubleUp(bi.Value()).Snapshot(bc).Listen(x => { @out.Add(x); });

            bc.Send('b');
            bi.Send(2);
            bc.Send('c');
            bi.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 'a', 'a', 'b', 'b', 'c', 'c' }, @out);
        }
Example #20
0
        public void TestLiftGlitch()
        {
            var               a  = new BehaviorSink <int>(1);
            Behavior <int>    a3 = a.Map <int>(x => x * 3);
            Behavior <int>    a5 = a.Map <int>(x => x * 5);
            Behavior <String> b  = Behavior <int> .Lift((x, y) => x + " " + y, a3, a5);

            var      @out = new List <String>();
            Listener l    = b.Value().Listen(x => { @out.Add(x); });

            a.Send(2);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out);
        }
Example #21
0
        public void TestGate()
        {
            StreamSink <char?>  sc    = Stream.CreateSink <char?>();
            BehaviorSink <bool> cGate = Behavior.CreateSink(true);
            List <char?>        @out  = new List <char?>();
            IListener           l     = sc.Gate(cGate).Listen(@out.Add);

            sc.Send('H');
            cGate.Send(false);
            sc.Send('O');
            cGate.Send(true);
            sc.Send('I');
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out);
        }
Example #22
0
        public void TestGate()
        {
            var      ec    = new EventSink <char?>();
            var      epred = new BehaviorSink <Boolean>(true);
            var      @out  = new List <char?>();
            Listener l     = ec.Gate(epred).Listen(x => { @out.Add(x); });

            ec.Send('H');
            epred.Send(false);
            ec.Send('O');
            epred.Send(true);
            ec.Send('I');
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out);
        }
Example #23
0
        public void TestLift()
        {
            var      a    = new BehaviorSink <int>(1);
            var      b    = new BehaviorSink <long>(5L);
            var      @out = new List <String>();
            Listener l    = Behavior <int> .Lift(
                (x, y) => x + " " + y,
                a,
                b
                ).Value().Listen(x => { @out.Add(x); });

            a.Send(12);
            b.Send(6L);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
        }
Example #24
0
 public void TestSnapshot()
 {
   var b = new BehaviorSink<int>(0);
   var trigger = new EventSink<long>();
   var @out = new List<string>();
   var l = trigger.Snapshot(b, (x, y) => x + " " + y)
       .Listen(new Handler<string> { Run = x => @out.Add(x) });
   trigger.Send(100L);
   b.Send(2);
   trigger.Send(200L);
   b.Send(9);
   b.Send(1);
   trigger.Send(300L);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out);
 }
Example #25
0
        public void TestSnapshot()
        {
            BehaviorSink <int> b       = Behavior.CreateSink(0);
            StreamSink <long>  trigger = Stream.CreateSink <long>();
            List <string>      @out    = new List <string>();
            IListener          l       = trigger.Snapshot(b, (x, y) => x + " " + y).Listen(@out.Add);

            trigger.Send(100L);
            b.Send(2);
            trigger.Send(200L);
            b.Send(9);
            b.Send(1);
            trigger.Send(300L);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out);
        }
Example #26
0
        public void FunctionalBehaviorLoop()
        {
            BehaviorSink <int> s      = Behavior.CreateSink(0);
            Behavior <int>     result = Behavior.Loop <int>().WithoutCaptures(l => Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior());

            List <int> @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
            }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Example #27
0
        public void TestSnapshot()
        {
            var b       = new BehaviorSink <int>(0);
            var trigger = new EventSink <long>();
            var @out    = new List <string>();
            var l       = trigger.Snapshot(b, (x, y) => x + " " + y)
                          .Listen(new Handler <string> {
                Run = x => @out.Add(x)
            });

            trigger.Send(100L);
            b.Send(2);
            trigger.Send(200L);
            b.Send(9);
            b.Send(1);
            trigger.Send(300L);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out);
        }
Example #28
0
        public void TestLiftLoop()
        {
            var @out            = new List <string>();
            var b               = new BehaviorSink <string>("kettle");
            Behavior <String> c = Transaction.Run(() =>
            {
                var a = new BehaviorLoop <string>();
                Behavior <String> c_ = Behavior <string> .Lift(
                    (aa, bb) => aa + " " + bb,
                    a, b);
                a.Loop(new Behavior <String>("tea"));
                return(c_);
            });
            Listener l = c.Value().Listen(x => { @out.Add(x); });

            b.Send("caddy");
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "tea kettle", "tea caddy" }, @out);
        }
Example #29
0
        public TimerSystem(ITimerSystemImplementation <T> implementation, Action <Exception> handleException)
        {
            this.implementation = implementation;
            this.implementation.Start(handleException);
            BehaviorSink <T> timeSink = new BehaviorSink <T>(this.implementation.Now);

            this.Time = timeSink;
            Transaction.OnStart(
                () =>
            {
                T t = this.implementation.Now;
                this.implementation.RunTimersTo(t);
                while (true)
                {
                    Event ev = null;
                    // Pop all events earlier than t.
                    lock (this.eventQueue)
                    {
                        if (this.eventQueue.Count > 0)
                        {
                            Event tempEvent = this.eventQueue.Peek();
                            if (tempEvent != null && tempEvent.Time.CompareTo(t) <= 0)
                            {
                                ev = this.eventQueue.Dequeue();
                            }
                        }
                    }

                    if (ev != null)
                    {
                        timeSink.Send(ev.Time);
                        ev.Alarm.Send(ev.Time);
                    }
                    else
                    {
                        break;
                    }
                }

                timeSink.Send(t);
            });
        }
Example #30
0
        public void FunctionalBehaviorLoopWithCaptures()
        {
            BehaviorSink <int> s = Behavior.CreateSink(0);

            (Behavior <int> result, Behavior <int> s2) = Behavior.Loop <int>()
                                                         .WithCaptures(l => (Behavior: Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior(), Captures: s.Map(v => 2 * v)));

            List <int> @out = new List <int>();
            List <int> out2 = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
                using (Transaction.Run(() => Operational.Value(s2).Listen(out2.Add)))

                {
                    s.Send(1);
                    s.Send(2);
                    s.Send(3);
                }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
            CollectionAssert.AreEqual(new[] { 0, 2, 4, 6 }, out2);
        }
Example #31
0
        public void ImperativeBehaviorLoop()
        {
            BehaviorSink <int> s      = Behavior.CreateSink(0);
            Behavior <int>     result = Transaction.Run(
                () =>
            {
                BehaviorLoop <int> l       = new BehaviorLoop <int>();
                Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                l.Loop(resultLocal);
                return(resultLocal);
            });

            List <int> @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
            }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Example #32
0
        public void ImperativeBehaviorLoopFailsWhenLoopedTwice()
        {
            InvalidOperationException actual = null;

            try
            {
                BehaviorSink <int> s = Behavior.CreateSink(0);
                Transaction.RunVoid(
                    () =>
                {
                    BehaviorLoop <int> l       = new BehaviorLoop <int>();
                    Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                    l.Loop(resultLocal);
                    l.Loop(resultLocal);
                });
            }
            catch (InvalidOperationException e)
            {
                actual = e;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual("Loop was looped more than once.", actual.Message);
        }
Example #33
0
        public void ImperativeBehaviorLoopFailsWhenLoopedInSeparateTransaction()
        {
            InvalidOperationException actual = null;

            BehaviorLoop <int> l = null;

            new Thread(
                () =>
                Transaction.RunVoid(
                    () =>
            {
                l = new BehaviorLoop <int>();
                Thread.Sleep(500);
            })).Start();

            try
            {
                BehaviorSink <int> s = Behavior.CreateSink(0);
                Transaction.RunVoid(
                    () =>
                {
                    Thread.Sleep(250);
                    Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                    l.Loop(resultLocal);
                });
            }
            catch (InvalidOperationException e)
            {
                actual = e;
            }

            Thread.Sleep(500);

            Assert.IsNotNull(actual);
            Assert.AreEqual("Loop must be looped in the same transaction that it was created in.", actual.Message);
        }
Example #34
0
 public void TestValuesTwiceThenSnapshot()
 {
   var bi = new BehaviorSink<int>(9);
   var bc = new BehaviorSink<char>('a');
   var @out = new List<char>();
   Listener l = DoubleUp(bi.Value()).Snapshot(bc).Listen(x => { @out.Add(x); });
   bc.Send('b');
   bi.Send(2);
   bc.Send('c');
   bi.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 'a', 'a', 'b', 'b', 'c', 'c' }, @out);
 }
Example #35
0
 public void TestValuesThenFilter()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   Listener l = b.Value().Filter(a => true).Listen(x => { @out.Add(x); });
   b.Send(2);
   b.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
 }
Example #36
0
 public void TestValuesTwiceThenOnce()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   Listener l = DoubleUp(b.Value()).Once().Listen(x => { @out.Add(x); });
   b.Send(2);
   b.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 9 }, @out);
 }
Example #37
0
 public void TestValuesLateListen()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   Event<int> value = b.Value();
   b.Send(8);
   Listener l = value.Listen(x => { @out.Add(x); });
   b.Send(2);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 8, 2 }, @out);
 }
Example #38
0
 public void TestMapBLateListen()
 {
   var b = new BehaviorSink<int>(6);
   var @out = new List<String>();
   Behavior<String> bm = b.Map(x => x.ToString());
   b.Send(2);
   Listener l = bm.Value().Listen(x => { @out.Add(x); });
   b.Send(8);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { "2", "8" }, @out);
 }
Example #39
0
 public void TestApply()
 {
   var bf = new BehaviorSink<Func<long, String>>(b => "1 " + b);
   var ba = new BehaviorSink<long>(5L);
   var @out = new List<String>();
   Listener l = Behavior<long>.Apply(bf, ba).Value().Listen(x => { @out.Add(x); });
   bf.Send(b => "12 " + b);
   ba.Send(6L);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
 }
Example #40
0
 public void TestLift()
 {
   var a = new BehaviorSink<int>(1);
   var b = new BehaviorSink<long>(5L);
   var @out = new List<String>();
   Listener l = Behavior<int>.Lift(
     (x, y) => x + " " + y,
     a,
     b
   ).Value().Listen(x => { @out.Add(x); });
   a.Send(12);
   b.Send(6L);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
 }
Example #41
0
 public void TestLiftGlitch()
 {
   var a = new BehaviorSink<int>(1);
   Behavior<int> a3 = a.Map<int>(x => x * 3);
   Behavior<int> a5 = a.Map<int>(x => x * 5);
   Behavior<String> b = Behavior<int>.Lift((x, y) => x + " " + y, a3, a5);
   var @out = new List<String>();
   Listener l = b.Value().Listen(x => { @out.Add(x); });
   a.Send(2);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out);
 }
Example #42
0
 public void TestLiftLoop()
 {
   var @out = new List<string>();
   var b = new BehaviorSink<string>("kettle");
   Behavior<String> c = Transaction.Run(() =>
   {
     var a = new BehaviorLoop<string>();
     Behavior<String> c_ = Behavior<string>.Lift(
         (aa, bb) => aa + " " + bb,
         a, b);
     a.Loop(new Behavior<String>("tea"));
     return c_;
   });
   Listener l = c.Value().Listen(x => { @out.Add(x); });
   b.Send("caddy");
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { "tea kettle", "tea caddy" }, @out);
 }
Example #43
0
 public void TestValuesTwiceThenCoalesce()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   Listener l = DoubleUp(b.Value()).Coalesce((fst, snd) => fst + snd).Listen(x => { @out.Add(x); });
   b.Send(2);
   b.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 18, 4, 14 }, @out);
 }
Example #44
0
 public void TestValuesThenCoalesce()
 {
   var b = new BehaviorSink<int>(9);
   var @out = new List<int>();
   Listener l = b.Value().Coalesce((fst, snd) => snd).Listen(x => { @out.Add(x); });
   b.Send(2);
   b.Send(7);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
 }
Example #45
0
 public void TestGate()
 {
   var ec = new EventSink<char?>();
   var epred = new BehaviorSink<Boolean>(true);
   var @out = new List<char?>();
   Listener l = ec.Gate(epred).Listen(x => { @out.Add(x); });
   ec.Send('H');
   epred.Send(false);
   ec.Send('O');
   epred.Send(true);
   ec.Send('I');
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out);
 }
Example #46
0
 public void TestValuesThenMerge()
 {
   var bi = new BehaviorSink<int>(9);
   var bj = new BehaviorSink<int>(2);
   var @out = new List<int>();
   Listener l = Event<int>.MergeWith((x, y) => x + y, bi.Value(), bj.Value())
       .Listen(x => { @out.Add(x); });
   bi.Send(1);
   bj.Send(4);
   l.Unlisten();
   CollectionAssert.AreEqual(new[] { 11, 1, 4 }, @out);
 }