Exemplo n.º 1
0
        public IDisposable Initialize()
        {
            var groups = _allNodes.GroupBy(item => item.Value, item => item.Key).ToArray();

            foreach (var group in groups)
            {
                switch (group.Key)
                {
                case NodeType.IfTrue:
                    _ifTrueNodes = group.ToList().AsReadOnly();
                    break;

                case NodeType.IfFalse:
                    _ifFalseNodes = group.ToList().AsReadOnly();
                    break;

                case NodeType.Test:
                    group.ForEach(item => item.Changed += OnChanged);
                    break;
                }
            }

            var ifTrueChildDisposable  = IfTrueChild?.Initialize();
            var ifFalseChildDisposable = IfFalseChild?.Initialize();

            Update(true);

            return(Disposable.Create(() =>
            {
                Update(false);

                _ifTrueNodes = null;
                _ifFalseNodes = null;
                groups.FirstOrDefault(item => item.Key == NodeType.Test)?
                .ForEach(item => item.Changed -= OnChanged);

                ifTrueChildDisposable?.Dispose();
                ifFalseChildDisposable?.Dispose();
            }));
        }
Exemplo n.º 2
0
        private void Update(bool activate)
        {
            IsActivated = activate;

            if (IsActivated)
            {
                var testResult = _testGetter.TryGet(out var exception);

                if (exception == null)
                {
                    UpdateInternal(IfTrueChild, _ifTrueNodes, testResult);
                    UpdateInternal(IfFalseChild, _ifFalseNodes, !testResult);
                }
            }
            else
            {
                // Disable all nodes and child-conditional-expression recursively.
                _allNodes.Keys.ForEach(item => item.IsActivated = false);
                // The following two line only used to conditional expressions
                // that are larger than three levels of nesting.
                IfTrueChild?.Update(false);
                IfFalseChild?.Update(false);
            }
        }