Beispiel #1
0
        public void Deactivation() => TestZone.Run(tick =>
        {
            var activation = "";

            var source = Atom.Value(
                0,
                onActive: () => activation   += "S",
                onInactive: () => activation += "s");

            var middle = Atom.Computed(
                () => source.Value + 1,
                onActive: () => activation   += "M",
                onInactive: () => activation += "m");

            var target = Atom.Computed(
                () => middle.Value + 1,
                onActive: () => activation   += "T",
                onInactive: () => activation += "t");

            target.Get();
            Assert.AreEqual("", activation);

            var autoRun = Atom.AutoRun(() => target.Get());
            Assert.AreEqual("TMS", activation);

            autoRun.Dispose();
            Assert.AreEqual("TMS", activation);

            tick(0);
            Assert.AreEqual("TMStms", activation);
        });
Beispiel #2
0
        public static IDisposable RunApp([NotNull] ViewPanel root, [NotNull] WidgetBuilder builder)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            IView view    = root;
            var   context = new BuildContext(null, null);
            var   state   = State.Create(context, builder);
            var   render  = Atom.AutoRun(() => root.Render(state.Value));

            // ReSharper disable once ImplicitlyCapturedClosure
            return(new ActionDisposable(() =>
            {
                render.Dispose();

                if (!Engine.IsApplicationQuiting)
                {
                    view.ResetSource();
                }
            }));
        }
Beispiel #3
0
        public void ObsoleteAtomsActualizedInInitialOrder() => TestZone.Run(tick =>
        {
            var actualization = "";

            var source = Atom.Value(1);
            var middle = Atom.Computed(() =>
            {
                actualization += "M";
                return(source.Value);
            });
            var target = Atom.Computed(() =>
            {
                actualization += "T";
                source.Get();
                return(middle.Value);
            });

            var autoRun = Atom.AutoRun(() => target.Get());
            Assert.AreEqual("TM", actualization);

            source.Value = 2;

            tick(0);
            Assert.AreEqual("TMTM", actualization);

            autoRun.Dispose();
        });
Beispiel #4
0
        public void NoReactivationDuringPulling() => TestZone.Run(tick =>
        {
            var activation = "";

            var activationSource = Atom.Computed(
                pull: () => 1,
                onActive: () => activation   += "A",
                onInactive: () => activation += "D");

            var modifiedSource = Atom.Value(1);
            var listener       = Atom.Computed(() => activationSource.Value + modifiedSource.Value);

            Assert.AreEqual("", activation);

            var autoRun = Atom.AutoRun(() => listener.Get());
            Assert.AreEqual("A", activation);

            modifiedSource.Value = 2;
            Assert.AreEqual("A", activation);

            tick(0);
            Assert.AreEqual("A", activation);

            autoRun.Dispose();
        });
Beispiel #5
0
        private void OnEnable()
        {
            _reaction = Atom.AutoRun(() =>
            {
                Debug.Log($"Tasks left: {_todoList.UnfinishedTodoCount}");
                unfinishedTodoCountText.text = $"Tasks left: {_todoList.UnfinishedTodoCount}";
            });

            addTodoButton.onClick.AddListener(AddTodo);
        }
Beispiel #6
0
        public void AutoUnsubscribed_Reaction() => TestZone.Run(tick =>
        {
            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1);
            var target = Atom.Computed(() => middle.Value + source.Value);

            var run = Atom.AutoRun(() => target.Get());

            Assert.AreEqual(2, source.SubscribersCount());
            Assert.AreEqual(1, middle.SubscribersCount());
            Assert.AreEqual(1, target.SubscribersCount());

            run.Dispose();
            tick(0);

            Assert.AreEqual(0, source.SubscribersCount());
            Assert.AreEqual(0, middle.SubscribersCount());
            Assert.AreEqual(0, target.SubscribersCount());
        });
Beispiel #7
0
        public void NoReactivationDuringModification() => TestZone.Run(tick =>
        {
            var activation = "";

            var atom = Atom.Value(1,
                                  onActive: () => activation   += "A",
                                  onInactive: () => activation += "D");

            Assert.AreEqual("", activation);

            var autoRun = Atom.AutoRun(() => atom.Get());
            Assert.AreEqual("A", activation);

            atom.Value = 2;
            Assert.AreEqual("A", activation);

            tick(0);
            Assert.AreEqual("A", activation);

            autoRun.Dispose();
        });
Beispiel #8
0
        public void AutoUnsubscribed_MultiReaction() => TestZone.Run(tick =>
        {
            var source  = Atom.Value(1);
            var middle  = Atom.Computed(() => source.Value + 1);
            var target1 = Atom.Computed(() => middle.Value + 1);
            var target2 = Atom.Computed(() => middle.Value + 1);

            var run1 = Atom.AutoRun(() => target1.Get());
            var run2 = Atom.AutoRun(() => target2.Get());

            Assert.AreEqual(2, middle.SubscribersCount());

            run1.Dispose();
            tick(0);

            Assert.AreEqual(1, middle.SubscribersCount());

            run2.Dispose();
            tick(0);

            Assert.AreEqual(0, middle.SubscribersCount());
        });
Beispiel #9
0
        public void AutoRun() => TestZone.Run(tick =>
        {
            var source = Atom.Value(0);

            int runs     = 0;
            var disposer = Atom.AutoRun(() =>
            {
                source.Get();
                ++runs;
            });
            Assert.AreEqual(1, runs);

            source.Value++;
            tick(0);

            Assert.AreEqual(2, runs);

            disposer.Dispose();
            source.Value++;
            tick(0);
            Assert.AreEqual(2, runs);
        });
Beispiel #10
0
        public void Invalidate() => TestZone.Run(tick =>
        {
            //
            var source = Atom.Value(0);

            string actualization = "";

            var dispose = Atom.AutoRun(() =>
            {
                source.Get();
                actualization += "T";
            });

            tick(0);
            Assert.AreEqual("T", actualization);

            source.Invalidate();

            tick(0);
            Assert.AreEqual("TT", actualization);

            dispose.Dispose();
        });
Beispiel #11
0
        public void KeepAliveComputed() => TestZone.Run(tick =>
        {
            var source = Atom.Value(1);
            var middle = Atom.Computed(() => source.Value + 1, keepAlive: true);
            var target = Atom.Computed(() => middle.Value + source.Value);

            var run = Atom.AutoRun(() => target.Get());

            Assert.AreEqual(2, source.SubscribersCount());
            Assert.AreEqual(1, middle.SubscribersCount());
            Assert.AreEqual(1, target.SubscribersCount());

            run.Dispose();
            tick(0);

            Assert.AreEqual(1, source.SubscribersCount());
            Assert.AreEqual(0, middle.SubscribersCount());
            Assert.AreEqual(0, target.SubscribersCount());

            middle.Deactivate();
            tick(0);

            Assert.AreEqual(0, source.SubscribersCount());
        });