Esempio n. 1
0
        public void TestBatch()
        {
            var a = ObservableValue <int> .From(2);

            var b = ObservableValue <int> .From(3);

            var areader = a as IValueReader <int>;
            var awriter = a as IValueWriter <int>;

            var breader = b as IValueReader <int>;
            var bwriter = b as IValueWriter <int>;

            var c = ComputedValue <int> .From(() => areader.Value *breader.Value);

            var d = ComputedValue <int> .From(() => c.Value *breader.Value);

            var values    = new List <int>();
            var diposable = d.Observe(change =>
            {
                values.Add(change.NewValue);
            });

            awriter.Value = 4;
            bwriter.Value = 5;

            // Note, 60 should not happen! (that is d beign computed before c after update of b)
            Assert.Equal(2, values.Count);
            Assert.Equal(36, values[0]);
            Assert.Equal(100, values[1]);

            var x = Reactions.Transaction <int>(() =>
            {
                awriter.Value = 2;
                bwriter.Value = 3;
                awriter.Value = 6;

                Assert.Equal(54, d.Value);

                return(2);
            });

            Assert.Equal(2, x);
            Assert.Equal(3, values.Count);
            Assert.Equal(36, values[0]);
            Assert.Equal(100, values[1]);
            Assert.Equal(54, values[2]);
        }
Esempio n. 2
0
        public void TestTransaction()
        {
            var x1 = ObservableValue <int> .From(3);

            var x2 = ObservableValue <int> .From(5);

            var x1reader = x1 as IValueReader <int>;
            var x1writer = x1 as IValueWriter <int>;

            var x2reader = x2 as IValueReader <int>;
            var x2writer = x2 as IValueWriter <int>;

            var y = ComputedValue <int> .From(() => x1reader.Value + x2reader.Value);

            var values = new List <int>();

            var diposable = y.Observe(change =>
            {
                values.Add(change.NewValue);
            }, true);

            Assert.Equal(8, y.Value);

            x1writer.Value = 4;

            Assert.Equal(9, y.Value);

            Reactions.Transaction(() =>
            {
                x1writer.Value = 5;
                x2writer.Value = 6;
            });

            Assert.Equal(11, y.Value);
            Assert.Equal(3, values.Count);

            Assert.Equal(8, values[0]);
            Assert.Equal(9, values[1]);
            Assert.Equal(11, values[2]);
        }
Esempio n. 3
0
        public void TestTransactionInspection()
        {
            var a = ObservableValue <int> .From(2);

            var areader = a as IValueReader <int>;
            var awriter = a as IValueWriter <int>;

            var calcs = 0;
            var b     = ComputedValue <int> .From(() =>
            {
                calcs++;
                return(areader.Value * 2);
            });

            // if not inspected during transaction, postpone value to end
            Reactions.Transaction(() =>
            {
                awriter.Value = 3;
                Assert.Equal(6, b.Value);
                Assert.Equal(1, calcs);
            });

            Assert.Equal(6, b.Value);
            Assert.Equal(2, calcs);

            // if inspected, evaluate eagerly
            Reactions.Transaction(() =>
            {
                awriter.Value = 4;
                Assert.Equal(8, b.Value);
                Assert.Equal(3, calcs);
            });

            Assert.Equal(8, b.Value);
            Assert.Equal(4, calcs);
        }