Пример #1
0
            public void Run()
            {
                CellSink <int> x = Cell.CreateSink(0);

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

                x.Send(2);
                x.Send(3);
                l.Unlisten();
            }
Пример #2
0
        public void TestListenOnceUpdates()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => c.Updates().ListenOnce(@out.Add));

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2 }, @out);
        }
Пример #3
0
        public void TestUpdates()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = c.Updates().Listen(@out.Add);

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
        }
Пример #4
0
        public void FunctionalCellLoop()
        {
            CellSink <int> s      = Cell.CreateSink(0);
            Cell <int>     result = Cell.Loop <int>().WithoutCaptures(l => s.Updates().Snapshot(l, (n, o) => n + o).Hold(0));

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

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

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Пример #5
0
        public void FunctionalCellLoopWithCaptures()
        {
            CellSink <int> s = Cell.CreateSink(0);

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

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

            using (Transaction.Run(() => result.Listen(@out.Add)))
                using (Transaction.Run(() => 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);
        }
Пример #6
0
        public void ImperativeCellLoop()
        {
            CellSink <int> s      = Cell.CreateSink(0);
            Cell <int>     result = Transaction.Run(
                () =>
            {
                CellLoop <int> l       = new CellLoop <int>();
                Cell <int> resultLocal = s.Updates().Snapshot(l, (n, o) => n + o).Hold(0);
                l.Loop(resultLocal);
                return(resultLocal);
            });

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

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

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Пример #7
0
        public void ImperativeCellLoopFailsWhenLoopedInSeparateTransaction()
        {
            InvalidOperationException actual = null;

            CellLoop <int> l = null;

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

            try
            {
                CellSink <int> s = Cell.CreateSink(0);
                Transaction.RunVoid(
                    () =>
                {
                    Thread.Sleep(250);
                    Cell <int> resultLocal = s.Updates().Snapshot(l, (n, o) => n + o).Hold(0);
                    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);
        }
Пример #8
0
        public void ImperativeCellLoopFailsWhenLoopedTwice()
        {
            InvalidOperationException actual = null;

            try
            {
                CellSink <int> s = Cell.CreateSink(0);
                Transaction.RunVoid(
                    () =>
                {
                    CellLoop <int> l       = new CellLoop <int>();
                    Cell <int> resultLocal = s.Updates().Snapshot(l, (n, o) => n + o).Hold(0);
                    l.Loop(resultLocal);
                    l.Loop(resultLocal);
                });
            }
            catch (InvalidOperationException e)
            {
                actual = e;
            }

            Assert.IsNotNull(actual);
            Assert.AreEqual("Loop was looped more than once.", actual.Message);
        }