Exemple #1
0
        public FrView(Window window, Fridget fr, IListener l)
        {
            StreamSink <MouseEvent> sMouse = new StreamSink <MouseEvent>();
            StreamSink <KeyEvent>   sKey   = new StreamSink <KeyEvent>();

            this.MouseDown += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this)));
            this.MouseUp   += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this)));
            this.MouseMove += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this)));
            CellSink <Maybe <Size> > size = new CellSink <Maybe <Size> >(Maybe.None);

            this.SizeChanged += (sender, args) => size.Send(Maybe.Some(args.NewSize));
            window.KeyDown   += (sender, args) =>
            {
                Key key = args.Key == Key.System ? args.SystemKey : args.Key;
                if (key == Key.Back)
                {
                    sKey.Send(new BackspaceKeyEvent());
                }
            };
            window.TextInput += (sender, args) => sKey.Send(new StringKeyEvent(args.Text));
            CellLoop <long> focus = new CellLoop <long>();

            Fridget.Output fo = fr.Reify(size, sMouse, sKey, focus, new Supply());
            focus.Loop(fo.SChangeFocus.Hold(-1));
            this.drawable = fo.Drawable;
            this.l        = new CompositeListener(new[] { l, this.drawable.Updates.Listen(d => this.InvalidateVisual()) });
        }
Exemple #2
0
        public void TestSwitchEarlySSimultaneous()
        {
            Ss2            ss1  = new Ss2();
            CellSink <Ss2> css  = Cell.CreateSink(ss1);
            Stream <int>   so   = css.Map <Stream <int> >(b => b.S).SwitchEarlyS();
            List <int>     @out = new List <int>();
            IListener      l    = so.Listen(@out.Add);
            Ss2            ss2  = new Ss2();
            Ss2            ss3  = new Ss2();
            Ss2            ss4  = new Ss2();

            ss1.S.Send(0);
            ss1.S.Send(1);
            ss1.S.Send(2);
            css.Send(ss2);
            ss1.S.Send(7);
            ss2.S.Send(3);
            ss2.S.Send(4);
            ss3.S.Send(2);
            css.Send(ss3);
            ss3.S.Send(5);
            ss3.S.Send(6);
            ss3.S.Send(7);
            Transaction.RunVoid(() =>
            {
                ss4.S.Send(8);
                css.Send(ss4);
                ss3.S.Send(2);
            });
            ss4.S.Send(9);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out);
        }
Exemple #3
0
        public void TestSwitchCSimultaneous()
        {
            Sc2            sc1  = new Sc2(0);
            CellSink <Sc2> csc  = new CellSink <Sc2>(sc1);
            Cell <int>     co   = csc.Map <Cell <int> >(b => b.C).SwitchC();
            List <int>     @out = new List <int>();

            using (co.Listen(@out.Add))
            {
                Sc2 sc2 = new Sc2(3);
                Sc2 sc3 = new Sc2(4);
                Sc2 sc4 = new Sc2(7);
                sc1.C.Send(1);
                sc1.C.Send(2);
                csc.Send(sc2);
                sc1.C.Send(3);
                sc2.C.Send(4);
                sc3.C.Send(5);
                csc.Send(sc3);
                sc3.C.Send(6);
                sc3.C.Send(7);
                Transaction.RunVoid(() =>
                {
                    sc3.C.Send(2);
                    csc.Send(sc4);
                    sc4.C.Send(8);
                });
                sc4.C.Send(9);
            }
            CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out);
        }
Exemple #4
0
        public void SwitchEarlySOnCellLoop()
        {
            ValueTuple <Stream <int>, StreamSink <int>, StreamSink <int>, CellSink <Stream <int> > > t = Transaction.Run(() =>
            {
                CellLoop <Stream <int> > loop = Cell.CreateLoop <Stream <int> >();
                StreamSink <int> c1           = Stream.CreateSink <int>();
                StreamSink <int> c2           = Stream.CreateSink <int>();
                Stream <int> c             = loop.SwitchEarlyS();
                CellSink <Stream <int> > s = Cell.CreateSink(c1.AsStream());
                loop.Loop(s);
                return(ValueTuple.Create(c, c1, c2, s));
            });

            List <int> output = new List <int>();
            IListener  l      = t.Item1.Listen(output.Add);

            t.Item2.Send(2);
            t.Item3.Send(12);

            Transaction.RunVoid(() =>
            {
                t.Item2.Send(3);
                t.Item3.Send(13);
                t.Item4.Send(t.Item3);
            });

            t.Item2.Send(4);
            t.Item3.Send(14);

            l.Unlisten();

            CollectionAssert.AreEqual(new[] { 2, 13, 14 }, output);
        }
Exemple #5
0
        public void TestLoopCell()
        {
            CellSink <int> ca = Cell.CreateSink(22);

            (CellLoop <int> cb, Cell <int> cb2, Cell <int> cc) = Transaction.Run(() =>
            {
                CellLoop <int> cbLocal = Cell.CreateLoop <int>();
                Cell <int> ccLocal     = ca.Map(x => x % 10).Lift(cbLocal, (x, y) => x * y);
                Cell <int> cbOut       = ca.Map(x => x / 10);
                cbLocal.Loop(cbOut);
                return(cbLocal, cbOut, ccLocal);
            });
            List <int> @out = new List <int>();
            List <int> out2 = new List <int>();
            List <int> out3 = new List <int>();
            IListener  l    = cb.Listen(@out.Add);
            IListener  l2   = cb2.Listen(out2.Add);
            IListener  l3   = cc.Listen(out3.Add);

            ca.Send(2);
            ca.Send(52);
            l3.Unlisten();
            l2.Unlisten();
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2, 0, 5 }, @out.ToArray());
            CollectionAssert.AreEqual(new[] { 2, 0, 5 }, out2.ToArray());
            CollectionAssert.AreEqual(new[] { 4, 0, 10 }, out3.ToArray());
        }
Exemple #6
0
        public void TestStreamSendInCellMapThrowsException()
        {
            InvalidOperationException actual = null;

            CellSink <int>   c  = Cell.CreateSink(5);
            StreamSink <int> s2 = Stream.CreateSink <int>();

            try
            {
                using (c.Map(
                           v =>
                {
                    s2.Send(v);
                    return(Unit.Value);
                })
                       .Listen(_ => { }))
                {
                }
            }
            catch (InvalidOperationException e)
            {
                actual = e;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual("Send may not be called inside a Sodium callback.", actual.Message);
        }
Exemple #7
0
        public void Issue151_PoolDoubleSubtraction_Fixed()
        {
            CellSink <int>   threshold   = new CellSink <int>(10);
            StreamSink <int> addPoolSink = new StreamSink <int>();

            (Stream <int> input, Cell <int> pool) = Transaction.Run(() =>
            {
                StreamLoop <int> submitPooledAmount = new StreamLoop <int>();

                // Ways that the pool is modified.
                Stream <Func <int, int> > poolAddByInput    = addPoolSink.Map <Func <int, int> >(i => x => x + i);
                Stream <Func <int, int> > poolRemoveByUsage = Operational.Defer(submitPooledAmount.Map <Func <int, int> >(i => x => x - i));

                // The current level of the pool
                Cell <int> poolLocal = poolAddByInput
                                       .Merge(poolRemoveByUsage, (f, g) => x => g(f(x)))
                                       .Accum(0, (f, x) => f(x));

                // The current input changes combined with the pool as a stream
                Stream <int> inputByAdded =
                    poolAddByInput
                    .Snapshot(
                        poolLocal,
                        threshold,
                        (f, x, t) => f(x) >= t
                                ? Maybe.Some(f(x))
                                : Maybe.None)
                    .FilterMaybe();

                // Simple rising edge on pool threshold satisfaction.
                Stream <int> inputBySatisfaction =
                    poolLocal.Updates
                    .Snapshot(
                        poolLocal,
                        threshold,
                        (neu, alt, t) => neu >= t && alt < t
                                ? Maybe.Some(neu)
                                : Maybe.None)
                    .FilterMaybe();

                submitPooledAmount.Loop(inputByAdded.Merge(inputBySatisfaction, Math.Max));

                return(submitPooledAmount, poolLocal);
            });

            List <int> submissions = new List <int>();

            using (input.Listen(submissions.Add))
            {
                // Add amount which can be immediately used based on threshold.
                // Pool should remain zero after the transaction is complete.
                addPoolSink.Send(10);
            }

            Assert.AreEqual(1, submissions.Count);
            Assert.AreEqual(10, submissions[0]);
            Assert.AreEqual(0, pool.Sample());
        }
Exemple #8
0
        public void Issue151_PoolDoubleSubtraction_Broken()
        {
            Exception actual = null;

            try
            {
                CellSink <int>   threshold   = new CellSink <int>(10);
                StreamSink <int> addPoolSink = new StreamSink <int>();

                Transaction.Run(() =>
                {
                    StreamLoop <int> submitPooledAmount = new StreamLoop <int>();

                    // Ways that the pool is modified.
                    Stream <Func <int, int> > poolAddByInput    = addPoolSink.Map <Func <int, int> >(i => x => x + i);
                    Stream <Func <int, int> > poolRemoveByUsage = submitPooledAmount.Map <Func <int, int> >(i => x => x - i);

                    // The current level of the pool
                    Cell <int> poolLocal = poolAddByInput
                                           .Merge(poolRemoveByUsage, (f, g) => x => g(f(x)))
                                           .Accum(0, (f, x) => f(x));

                    // The current input changes combined with the pool as a stream
                    Stream <int> inputByAdded =
                        poolAddByInput
                        .Snapshot(
                            poolLocal,
                            threshold,
                            (f, x, t) => f(x) >= t
                                    ? Maybe.Some(f(x))
                                    : Maybe.None)
                        .FilterMaybe();

                    // Simple rising edge on pool threshold satisfaction.
                    Stream <int> inputBySatisfaction =
                        poolLocal.Updates
                        .Snapshot(
                            poolLocal,
                            threshold,
                            (neu, alt, t) => neu >= t && alt < t
                                    ? Maybe.Some(neu)
                                    : Maybe.None)
                        .FilterMaybe();

                    submitPooledAmount.Loop(inputByAdded.Merge(inputBySatisfaction, Math.Max));

                    return(submitPooledAmount, poolLocal);
                });
            }
            catch (Exception e)
            {
                actual = e;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual("A dependency cycle was detected.", actual.Message);
        }
Exemple #9
0
        public async Task TestListenOnceAsync()
        {
            CellSink <int> c      = Cell.CreateSink(9);
            int            result = await Transaction.Run(() => Operational.Value(c).ListenOnceAsync());

            c.Send(2);
            c.Send(7);
            Assert.AreEqual(9, result);
        }
Exemple #10
0
 public void Run()
 {
     CellSink<int> x = new CellSink<int>(0);
     using (x.Listen(Console.WriteLine))
     {
         x.Send(10);
         x.Send(20);
         x.Send(30);
     }
 }
Exemple #11
0
            public void Run()
            {
                CellSink <int> x = Cell.CreateSink(0);
                IListener      l = x.Listen(Console.WriteLine);

                x.Send(10);
                x.Send(20);
                x.Send(30);
                l.Unlisten();
            }
Exemple #12
0
            public void Run()
            {
                CellSink <int> x = new CellSink <int>(0);

                x.Send(1);
                using (Transaction.Run(() => Sodium.Operational.Value(x).Listen(Console.WriteLine)))
                {
                    x.Send(2);
                    x.Send(3);
                }
            }
Exemple #13
0
            public void Run()
            {
                CellSink <int> x = new CellSink <int>(0);

                using (x.Listen(Console.WriteLine))
                {
                    x.Send(10);
                    x.Send(20);
                    x.Send(30);
                }
            }
Exemple #14
0
            public void Run()
            {
                CellSink <int> x = new CellSink <int>(0);

                x.Send(1);
                using (Sodium.Operational.Updates(x).Listen(Console.WriteLine))
                {
                    x.Send(2);
                    x.Send(3);
                }
            }
Exemple #15
0
            public void Run()
            {
                CellSink <int> x = new CellSink <int>(0);

                x.Send(1);
                IListener l = x.Values.Listen(Console.WriteLine);

                x.Send(2);
                x.Send(3);
                l.Unlisten();
            }
Exemple #16
0
        public void TestValues()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => c.Values().Listen(@out.Add));

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Exemple #17
0
            public void Run()
            {
                CellSink <int> x = new CellSink <int>(0);

                x.Send(1);
                IListener l = Transaction.Run(() => Sodium.Operational.Value(x).Listen(Console.WriteLine));

                x.Send(2);
                x.Send(3);
                l.Unlisten();
            }
Exemple #18
0
 public STextField(string initialText)
 {
     base.Text = initialText;
     CellSink<string> text = new CellSink<string>(initialText);
     this.TextChanged += async (sender, args) =>
     {
         string t = base.Text;
         await Task.Run(() => text.Send(t));
     };
     this.Text = text;
 }
Exemple #19
0
        public void TestUpdates()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Operational.Updates(c).Listen(@out.Add);

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
        }
Exemple #20
0
            public void Run()
            {
                CellSink <int> x = Cell.CreateSink(0);

                x.Send(1);
                IListener l = Transaction.Run(() => x.Values().Listen(Console.WriteLine));

                x.Send(2);
                x.Send(3);
                l.Unlisten();
            }
Exemple #21
0
 public void TestListenOnce()
 {
     CellSink<int> c = new CellSink<int>(9);
     List<int> @out = new List<int>();
     using (Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add)))
     {
         c.Send(2);
         c.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 9 }, @out);
 }
Exemple #22
0
        public void TestMap()
        {
            CellSink <int> c    = new CellSink <int>(6);
            List <string>  @out = new List <string>();

            using (c.Map(x => x.ToString()).Listen(@out.Add))
            {
                c.Send(8);
            }
            CollectionAssert.AreEqual(new[] { "6", "8" }, @out);
        }
Exemple #23
0
        public void TestValueThenMap()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add));

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
        }
Exemple #24
0
        public void TestListenOnce()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add));

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
Exemple #25
0
 public void TestListen()
 {
     CellSink<int> c = new CellSink<int>(9);
     List<int> @out = new List<int>();
     using (c.Listen(@out.Add))
     {
         c.Send(2);
         c.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
 }
Exemple #26
0
        public void TestValue()
        {
            CellSink <int> b    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(b).Listen(@out.Add)))
            {
                b.Send(2);
                b.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Exemple #27
0
        public void TestLiftCellsInSwitchC()
        {
            List <int>         @out = new List <int>();
            CellSink <int>     s    = new CellSink <int>(0);
            Cell <Cell <int> > c    = Cell.Constant(Cell.Constant(1));
            Cell <Cell <int> > r    = c.Map(c2 => c2.Lift(s, (v1, v2) => v1 + v2));

            r.SwitchC().Listen(@out.Add);
            s.Send(2);
            s.Send(4);
            CollectionAssert.AreEqual(new[] { 1, 3, 5 }, @out);
        }
Exemple #28
0
 public void TestLift()
 {
     CellSink<int> b1 = new CellSink<int>(1);
     CellSink<long> b2 = new CellSink<long>(5L);
     List<string> @out = new List<string>();
     using (b1.Lift(b2, (x, y) => x + " " + y).Listen(@out.Add))
     {
         b1.Send(12);
         b2.Send(6L);
     }
     CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
 }
Exemple #29
0
 public void TestApply()
 {
     CellSink<Func<long, string>> bf = new CellSink<Func<long, string>>(b => "1 " + b);
     CellSink<long> ba = new CellSink<long>(5L);
     List<string> @out = new List<string>();
     using (ba.Apply(bf).Listen(@out.Add))
     {
         bf.Send(b => "12 " + b);
         ba.Send(6L);
     }
     CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
 }
Exemple #30
0
        public void TestValueThenFilter()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c).Filter(x => x % 2 != 0).Listen(@out.Add)))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9, 7 }, @out);
        }
Exemple #31
0
        public void TestUpdates()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Operational.Updates(c).Listen(@out.Add))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
        }
Exemple #32
0
        public void TestValueThenMap()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add)))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
        }
Exemple #33
0
        public STextField(string initialText)
        {
            base.Text = initialText;
            CellSink <string> text = new CellSink <string>(initialText);

            this.TextChanged += async(sender, args) =>
            {
                string t = base.Text;
                await Task.Run(() => text.Send(t));
            };
            this.Text = text;
        }
Exemple #34
0
        public void TestSendNull()
        {
            CellSink <string> c    = new CellSink <string>(string.Empty);
            List <string>     @out = new List <string>();
            IListener         l    = c.Listen(@out.Add);

            c.Send("0");
            c.Send(null);
            c.Send("1");
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { string.Empty, "0", null, "1" }, @out);
        }
Exemple #35
0
        public void TestListenOnce()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add)))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
Exemple #36
0
        public void TestListen()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (c.Listen(@out.Add))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Exemple #37
0
 public void TestSnapshot()
 {
     CellSink<int> c = new CellSink<int>(0);
     StreamSink<long> trigger = new StreamSink<long>();
     List<string> @out = new List<string>();
     using (trigger.Snapshot(c, (x, y) => x + " " + y).Listen(@out.Add))
     {
         trigger.Send(100L);
         c.Send(2);
         trigger.Send(200L);
         c.Send(9);
         c.Send(1);
         trigger.Send(300L);
     }
     CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out);
 }
Exemple #38
0
 public FrView(Window window, Fridget fr, IListener l)
 {
     StreamSink<MouseEvent> sMouse = new StreamSink<MouseEvent>();
     StreamSink<KeyEvent> sKey = new StreamSink<KeyEvent>();
     this.MouseDown += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this)));
     this.MouseUp += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this)));
     this.MouseMove += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this)));
     CellSink<IMaybe<Size>> size = new CellSink<IMaybe<Size>>(Maybe.Nothing<Size>());
     this.SizeChanged += (sender, args) => size.Send(Maybe.Just(args.NewSize));
     window.KeyDown += (sender, args) =>
     {
         Key key = args.Key == Key.System ? args.SystemKey : args.Key;
         if (key == Key.Back)
         {
             sKey.Send(new BackspaceKeyEvent());
         }
     };
     window.TextInput += (sender, args) => sKey.Send(new StringKeyEvent(args.Text));
     CellLoop<long> focus = new CellLoop<long>();
     Fridget.Output fo = fr.Reify(size, sMouse, sKey, focus, new Supply());
     focus.Loop(fo.SChangeFocus.Hold(-1));
     this.drawable = fo.Drawable;
     this.l = new ImmutableCompositeListener(new[] { l, Operational.Updates(this.drawable).Listen(d => this.InvalidateVisual()) });
 }
Exemple #39
0
 public void TestValueThenMap()
 {
     CellSink<int> c = new CellSink<int>(9);
     List<int> @out = new List<int>();
     using (Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add)))
     {
         c.Send(2);
         c.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
 }
Exemple #40
0
 public Sc2(int initialValue)
 {
     this.C = new CellSink<int>(initialValue);
 }
Exemple #41
0
 public void TestMapLateListen()
 {
     CellSink<int> c = new CellSink<int>(6);
     List<string> @out = new List<string>();
     Cell<string> cm = c.Map(x => x.ToString());
     c.Send(2);
     using (cm.Listen(@out.Add))
     {
         c.Send(8);
     }
     CollectionAssert.AreEqual(new[] { "2", "8" }, @out);
 }
Exemple #42
0
 public void TestCalm2()
 {
     CellSink<int> c = new CellSink<int>(2);
     List<int> @out = new List<int>();
     using (Transaction.Run(() => c.Calm().Listen(@out.Add)))
     {
         c.Send(4);
         c.Send(2);
         c.Send(4);
         c.Send(4);
         c.Send(2);
         c.Send(2);
     }
     CollectionAssert.AreEqual(new[] { 2, 4, 2, 4, 2 }, @out);
 }
Exemple #43
0
 public void TestSwitchSSimultaneous()
 {
     Ss2 ss1 = new Ss2();
     CellSink<Ss2> css = new CellSink<Ss2>(ss1);
     Stream<int> so = css.Map<Stream<int>>(b => b.S).SwitchS();
     List<int> @out = new List<int>();
     using (so.Listen(@out.Add))
     {
         Ss2 ss2 = new Ss2();
         Ss2 ss3 = new Ss2();
         Ss2 ss4 = new Ss2();
         ss1.S.Send(0);
         ss1.S.Send(1);
         ss1.S.Send(2);
         css.Send(ss2);
         ss1.S.Send(7);
         ss2.S.Send(3);
         ss2.S.Send(4);
         ss3.S.Send(2);
         css.Send(ss3);
         ss3.S.Send(5);
         ss3.S.Send(6);
         ss3.S.Send(7);
         Transaction.RunVoid(() =>
         {
             ss3.S.Send(8);
             css.Send(ss4);
             ss4.S.Send(2);
         });
         ss4.S.Send(9);
     }
     CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out);
 }
Exemple #44
0
 public void TestSwitchCSimultaneous()
 {
     Sc2 sc1 = new Sc2(0);
     CellSink<Sc2> csc = new CellSink<Sc2>(sc1);
     Cell<int> co = csc.Map<Cell<int>>(b => b.C).SwitchC();
     List<int> @out = new List<int>();
     using (co.Listen(@out.Add))
     {
         Sc2 sc2 = new Sc2(3);
         Sc2 sc3 = new Sc2(4);
         Sc2 sc4 = new Sc2(7);
         sc1.C.Send(1);
         sc1.C.Send(2);
         csc.Send(sc2);
         sc1.C.Send(3);
         sc2.C.Send(4);
         sc3.C.Send(5);
         csc.Send(sc3);
         sc3.C.Send(6);
         sc3.C.Send(7);
         Transaction.RunVoid(() =>
         {
             sc3.C.Send(2);
             csc.Send(sc4);
             sc4.C.Send(8);
         });
         sc4.C.Send(9);
     }
     CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out);
 }
Exemple #45
0
 public void TestValueThenFilter()
 {
     CellSink<int> c = new CellSink<int>(9);
     List<int> @out = new List<int>();
     using (Transaction.Run(() => Operational.Value(c).Filter(x => x % 2 != 0).Listen(@out.Add)))
     {
         c.Send(2);
         c.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 9, 7 }, @out);
 }
Exemple #46
0
 public void TestValueThenMerge()
 {
     CellSink<int> c1 = new CellSink<int>(9);
     CellSink<int> c2 = new CellSink<int>(2);
     List<int> @out = new List<int>();
     using (Transaction.Run(() => Operational.Value(c1).Merge(Operational.Value(c2), (x, y) => x + y).Listen(@out.Add)))
     {
         c1.Send(1);
         c2.Send(4);
     }
     CollectionAssert.AreEqual(new[] { 11, 1, 4 }, @out);
 }
Exemple #47
0
 public void Run()
 {
     CellSink<int> x = new CellSink<int>(0);
     x.Send(1);
     using (Transaction.Run(() => Sodium.Operational.Value(x).Listen(Console.WriteLine)))
     {
         x.Send(2);
         x.Send(3);
     }
 }
Exemple #48
0
 public void TestGate()
 {
     StreamSink<char?> sc = new StreamSink<char?>();
     CellSink<bool> cGate = new CellSink<bool>(true);
     List<char?> @out = new List<char?>();
     using (sc.Gate(cGate).Listen(@out.Add))
     {
         sc.Send('H');
         cGate.Send(false);
         sc.Send('O');
         cGate.Send(true);
         sc.Send('I');
     }
     CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out);
 }
Exemple #49
0
 public void TestValueThenLateListen()
 {
     CellSink<int> c = new CellSink<int>(9);
     List<int> @out = new List<int>();
     Stream<int> value = Operational.Value(c);
     c.Send(8);
     using (value.Listen(@out.Add))
     {
         c.Send(2);
         c.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
 }
Exemple #50
0
 public void TestLiftGlitch()
 {
     CellSink<int> b1 = new CellSink<int>(1);
     Cell<int> b3 = b1.Map(x => x * 3);
     Cell<int> b5 = b1.Map(x => x * 5);
     Cell<string> b = b3.Lift(b5, (x, y) => x + " " + y);
     List<string> @out = new List<string>();
     using (b.Listen(@out.Add))
     {
         b1.Send(2);
     }
     CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out);
 }
Exemple #51
0
 public void TestMap()
 {
     CellSink<int> b = new CellSink<int>(6);
     List<string> @out = new List<string>();
     using (b.Map(x => x.ToString()).Listen(@out.Add))
     {
         b.Send(8);
     }
     CollectionAssert.AreEqual(new[] { "6", "8" }, @out);
 }
Exemple #52
0
 public async Task TestListenOnceTask()
 {
     CellSink<int> c = new CellSink<int>(9);
     int result = await Transaction.Run(() => Operational.Value(c).ListenOnce());
     c.Send(2);
     c.Send(7);
     Assert.AreEqual(9, result);
 }
Exemple #53
0
 public void TestValueThenFilter()
 {
     CellSink<int> b = new CellSink<int>(9);
     List<int> @out = new List<int>();
     using (Transaction.Run(() => Operational.Value(b).Filter(_ => true).Listen(@out.Add)))
     {
         b.Send(2);
         b.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
 }
Exemple #54
0
 public void TestApply()
 {
     CellSink<Func<long, string>> cf = new CellSink<Func<long, string>>(x => "1 " + x);
     CellSink<long> ca = new CellSink<long>(5L);
     List<string> @out = new List<string>();
     using (ca.Apply(cf).Listen(@out.Add))
     {
         cf.Send(x => "12 " + x);
         ca.Send(6L);
     }
     CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out);
 }
Exemple #55
0
 public void TestUpdates()
 {
     CellSink<int> c = new CellSink<int>(9);
     List<int> @out = new List<int>();
     using (Operational.Updates(c).Listen(@out.Add))
     {
         c.Send(2);
         c.Send(7);
     }
     CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
 }
Exemple #56
0
 public void TestLiftGlitch()
 {
     CellSink<int> c1 = new CellSink<int>(1);
     Cell<int> c3 = c1.Map(x => x * 3);
     Cell<int> c5 = c1.Map(x => x * 5);
     Cell<string> c = c3.Lift(c5, (x, y) => x + " " + y);
     List<string> @out = new List<string>();
     using (c.Listen(@out.Add))
     {
         c1.Send(2);
     }
     CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out);
 }
Exemple #57
0
 public void TestLiftFromSimultaneous()
 {
     Tuple<CellSink<int>, CellSink<int>> t = Transaction.Run(() =>
     {
         CellSink<int> localC1 = new CellSink<int>(3);
         CellSink<int> localC2 = new CellSink<int>(5);
         localC2.Send(7);
         return Tuple.Create(localC1, localC2);
     });
     CellSink<int> c1 = t.Item1;
     CellSink<int> c2 = t.Item2;
     List<int> @out = new List<int>();
     using (c1.Lift(c2, (x, y) => x + y).Listen(@out.Add))
     {
     }
     CollectionAssert.AreEqual(new[] { 10 }, @out);
 }
            public Implementation(PetrolPumpWindow petrolPump)
            {
                SComboBox<IPump> logic = new SComboBox<IPump>(
                    new IPump[]
                    {
                        new LifeCyclePump(),
                        new AccumulatePulsesPump(),
                        new ShowDollarsPump(),
                        new ClearSalePump(),
                        new KeypadPump(),
                        new PresetAmountPump()
                    },
                    p => p.GetType().FullName);
                petrolPump.LogicComboBoxPlaceholder.Children.Add(logic);

                STextField textPrice1 = new STextField("2.149") { Width = 100 };
                petrolPump.Price1Placeholder.Children.Add(textPrice1);

                STextField textPrice2 = new STextField("2.341") { Width = 100 };
                petrolPump.Price2Placeholder.Children.Add(textPrice2);

                STextField textPrice3 = new STextField("1.499") { Width = 100 };
                petrolPump.Price3Placeholder.Children.Add(textPrice3);

                Func<string, double> parseDoubleSafe = s =>
                {
                    double n;
                    if (double.TryParse(s, out n))
                    {
                        return n;
                    }

                    return 0.0;
                };

                StreamSink<Key> sKey = new StreamSink<Key>();
                Dictionary<Key, FrameworkElement> containersByKey = new Dictionary<Key, FrameworkElement>
                {
                    { Key.One, petrolPump.Keypad1Button },
                    { Key.Two, petrolPump.Keypad2Button },
                    { Key.Three, petrolPump.Keypad3Button },
                    { Key.Four, petrolPump.Keypad4Button },
                    { Key.Five, petrolPump.Keypad5Button },
                    { Key.Six, petrolPump.Keypad6Button },
                    { Key.Seven, petrolPump.Keypad7Button },
                    { Key.Eight, petrolPump.Keypad8Button },
                    { Key.Nine, petrolPump.Keypad9Button },
                    { Key.Zero, petrolPump.Keypad0Button },
                    { Key.Clear, petrolPump.KeypadClearButton }
                };
                foreach (KeyValuePair<Key, FrameworkElement> containerAndKey in containersByKey)
                {
                    containerAndKey.Value.MouseDown += async (sender, args) =>
                    {
                        if (args.LeftButton == MouseButtonState.Pressed)
                        {
                            await Task.Run(() => sKey.Send(containerAndKey.Key));
                        }
                    };
                }

                CellLoop<UpDown> nozzle1 = new CellLoop<UpDown>();
                CellLoop<UpDown> nozzle2 = new CellLoop<UpDown>();
                CellLoop<UpDown> nozzle3 = new CellLoop<UpDown>();

                Cell<double> calibration = Cell.Constant(0.001);
                Cell<double> price1 = textPrice1.Text.Map(parseDoubleSafe);
                Cell<double> price2 = textPrice2.Text.Map(parseDoubleSafe);
                Cell<double> price3 = textPrice3.Text.Map(parseDoubleSafe);
                CellSink<Stream<Unit>> csClearSale = new CellSink<Stream<Unit>>(Sodium.Stream.Never<Unit>());
                Stream<Unit> sClearSale = csClearSale.SwitchS();

                StreamSink<int> sFuelPulses = new StreamSink<int>();
                Cell<Outputs> outputs = logic.SelectedItem.Map(
                    pump => pump.Create(new Inputs(
                        Operational.Updates(nozzle1),
                        Operational.Updates(nozzle2),
                        Operational.Updates(nozzle3),
                        sKey,
                        sFuelPulses,
                        calibration,
                        price1,
                        price2,
                        price3,
                        sClearSale)));

                Cell<Delivery> delivery = outputs.Map(o => o.Delivery).SwitchC();
                Cell<string> presetLcd = outputs.Map(o => o.PresetLcd).SwitchC();
                Cell<string> saleCostLcd = outputs.Map(o => o.SaleCostLcd).SwitchC();
                Cell<string> saleQuantityLcd = outputs.Map(o => o.SaleQuantityLcd).SwitchC();
                Cell<string> priceLcd1 = outputs.Map(o => o.PriceLcd1).SwitchC();
                Cell<string> priceLcd2 = outputs.Map(o => o.PriceLcd2).SwitchC();
                Cell<string> priceLcd3 = outputs.Map(o => o.PriceLcd3).SwitchC();
                Stream<Unit> sBeep = outputs.Map(o => o.SBeep).SwitchS();
                Stream<Sale> sSaleComplete = outputs.Map(o => o.SSaleComplete).SwitchS();

                SoundPlayer beepPlayer = new SoundPlayer(GetResourceStream(@"sounds\beep.wav"));
                this.listeners.Add(sBeep.Listen(_ => new Thread(() => beepPlayer.PlaySync()) { IsBackground = true }.Start()));

                SoundPlayer fastRumblePlayer = new SoundPlayer(GetResourceStream(@"sounds\fast.wav"));
                Action stopFast = () => { };
                Action playFast = () =>
                {
                    ManualResetEvent mre = new ManualResetEvent(false);
                    new Thread(() =>
                    {
                        fastRumblePlayer.PlayLooping();
                        mre.WaitOne();
                        fastRumblePlayer.Stop();
                    })
                    { IsBackground = true }.Start();
                    stopFast = () => mre.Set();
                };

                SoundPlayer slowRumblePlayer = new SoundPlayer(GetResourceStream(@"sounds\slow.wav"));
                Action stopSlow = () => { };
                Action playSlow = () =>
                {
                    ManualResetEvent mre = new ManualResetEvent(false);
                    new Thread(() =>
                    {
                        slowRumblePlayer.PlayLooping();
                        mre.WaitOne();
                        slowRumblePlayer.Stop();
                    })
                    { IsBackground = true }.Start();
                    stopSlow = () => mre.Set();
                };

                this.listeners.Add(delivery.Changes().Listen(d =>
                {
                    petrolPump.Dispatcher.InvokeIfNecessary(() =>
                    {
                        if (d == Delivery.Fast1 || d == Delivery.Fast2 || d == Delivery.Fast3)
                        {
                            playFast();
                        }
                        else
                        {
                            stopFast();
                        }

                        if (d == Delivery.Slow1 || d == Delivery.Slow2 || d == Delivery.Slow3)
                        {
                            playSlow();
                        }
                        else
                        {
                            stopSlow();
                        }
                    });
                }));

                StackPanel presetLcdStackPanel = new StackPanel { Orientation = Orientation.Horizontal };
                petrolPump.PresetPlaceholder.Children.Add(presetLcdStackPanel);
                this.listeners.Add(presetLcd.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(presetLcdStackPanel, t, 5, true))));

                StackPanel saleCostStackPanel = new StackPanel { Orientation = Orientation.Horizontal };
                petrolPump.DollarsPlaceholder.Children.Add(saleCostStackPanel);
                this.listeners.Add(saleCostLcd.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(saleCostStackPanel, t, 5, true))));

                StackPanel saleQuantityLcdStackPanel = new StackPanel { Orientation = Orientation.Horizontal };
                petrolPump.LitersPlaceholder.Children.Add(saleQuantityLcdStackPanel);
                this.listeners.Add(saleQuantityLcd.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(saleQuantityLcdStackPanel, t, 5, true))));

                StackPanel priceLcd1StackPanel = new StackPanel { Orientation = Orientation.Horizontal };
                petrolPump.Fuel1Placeholder.Children.Add(priceLcd1StackPanel);
                this.listeners.Add(priceLcd1.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(priceLcd1StackPanel, t, 5, false))));

                StackPanel priceLcd2StackPanel = new StackPanel { Orientation = Orientation.Horizontal };
                petrolPump.Fuel2Placeholder.Children.Add(priceLcd2StackPanel);
                this.listeners.Add(priceLcd2.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(priceLcd2StackPanel, t, 5, false))));

                StackPanel priceLcd3StackPanel = new StackPanel { Orientation = Orientation.Horizontal };
                petrolPump.Fuel3Placeholder.Children.Add(priceLcd3StackPanel);
                this.listeners.Add(priceLcd3.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(priceLcd3StackPanel, t, 5, false))));

                Dictionary<CellLoop<UpDown>, Image> nozzles = new Dictionary<CellLoop<UpDown>, Image>
                {
                    { nozzle1, petrolPump.Nozzle1Image },
                    { nozzle2, petrolPump.Nozzle2Image },
                    { nozzle3, petrolPump.Nozzle3Image }
                };
                this.listeners.AddRange(nozzles.Select(nozzle => nozzle.Key.Listen(p => petrolPump.Dispatcher.InvokeIfNecessary(() => nozzle.Value.Margin = p == UpDown.Up ? new Thickness(0, 0, 0, 0) : new Thickness(0, 30, 0, 0)))));

                foreach (KeyValuePair<CellLoop<UpDown>, Image> nozzle in nozzles)
                {
                    StreamSink<Unit> nozzleClicks = new StreamSink<Unit>();
                    nozzle.Value.MouseDown += async (sender, args) =>
                    {
                        if (args.LeftButton == MouseButtonState.Pressed)
                        {
                            await Task.Run(() => nozzleClicks.Send(Unit.Value));
                        }
                    };
                    nozzle.Key.Loop(nozzleClicks.Snapshot(nozzle.Key, (_, n) => n == UpDown.Down ? UpDown.Up : UpDown.Down).Hold(UpDown.Down));
                }

                this.listeners.Add(sSaleComplete.Listen(sale =>
                {
                    Task.Run(() =>
                    {
                        petrolPump.Dispatcher.InvokeIfNecessary(() =>
                        {
                            SaleCompleteDialog dialog = new SaleCompleteDialog(
                                sale.Fuel.ToString(),
                                Formatters.FormatPrice(sale.Price, null),
                                Formatters.FormatSaleCost(sale.Cost),
                                Formatters.FormatSaleQuantity(sale.Quantity));
                            dialog.Owner = petrolPump;
                            csClearSale.Send(dialog.SOkClicked);
                            dialog.Show();
                            IListener l = null;
                            // ReSharper disable once RedundantAssignment
                            l = dialog.SOkClicked.Listen(_ =>
                            {
                                petrolPump.Dispatcher.InvokeIfNecessary(() => dialog.Close());

                                // ReSharper disable once AccessToModifiedClosure
                                using (l)
                                {
                                }
                            });
                        });
                    });
                }));

                Task.Run(async () =>
                {
                    while (true)
                    {
                        Transaction.RunVoid(() =>
                        {
                            switch (delivery.Sample())
                            {
                                case Delivery.Fast1:
                                case Delivery.Fast2:
                                case Delivery.Fast3:
                                    sFuelPulses.Send(40);
                                    break;
                                case Delivery.Slow1:
                                case Delivery.Slow2:
                                case Delivery.Slow3:
                                    sFuelPulses.Send(2);
                                    break;
                            }
                        });

                        await Task.Delay(200).ConfigureAwait(false);
                    }
                    // ReSharper disable once FunctionNeverReturns
                });
            }
Exemple #59
0
 public void Run()
 {
     CellSink<int> x = new CellSink<int>(0);
     x.Send(1);
     using (Sodium.Operational.Updates(x).Listen(Console.WriteLine))
     {
         x.Send(2);
         x.Send(3);
     }
 }