public override bool AddAction(IStateAction action)
        {
            if (action == null || this.actions.Contains(action))
            {
                return(false);
            }

            switch (action)
            {
            case IStateAction <TState, TTransition, TSignal> actionSTS:
                actionSTS.State = this;
                break;

            case IStateAction <TState> actionT:
                actionT.State = this;
                break;

            default:
                action.State = this;
                break;
            }

            this.actions.Add(action);
            return(true);
        }
Exemplo n.º 2
0
        public void Run(IStateAction action, bool?sendBack)
        {
            if (_middlewares.Any(m => !m.MayDispatchAction(action)))
            {
                return;
            }

            _middlewares.ForEach(m => m.BeforeDispatch(action));

            var sender  = ActorRefs.NoSender;
            var context = InternalCurrentActorCellKeeper.Current;

            if (context != null)
            {
                sender = context.Self;
            }

            var effects       = new EffectInvoker(_effects.Where(e => e.ShouldReactToAction(action)), action, this);
            var resultInvoker = new ResultInvoker(effects, _engine, sender, sendBack ?? _sendBackSetting, action);

            _middlewares.ForEach(m => m.BeforeDispatch(action));
            foreach (var dataMutation in _states.Select(sc => sc.TryDipatch(action, resultInvoker.AddResult, resultInvoker.WorkCompled)))
            {
                if (dataMutation == null)
                {
                    continue;
                }

                resultInvoker.PushWork();
                _engine.Mutate(dataMutation);
            }
        }
Exemplo n.º 3
0
        public Model Reduce(Model state, IStateAction action)
        {
            switch (action)
            {
            case UpdateNameAction a:
                return(state with {
                    Name = a.NewName, FieldsUpdated = state.FieldsUpdated + 1
                });

            case UpdateAgeAction a:
                return(state with {
                    Age = a.NewAge, FieldsUpdated = state.FieldsUpdated + 1
                });

            case ToggleShowNestedFormAction:
                return(state with {
                    ShowNestedForm = !state.ShowNestedForm, FieldsUpdated = state.FieldsUpdated + 1
                });

            default:
                return(state with {
                    Nested = _nestedReducer.Reduce(state.Nested, action)
                });
            }
        }
Exemplo n.º 4
0
 public ResultInvoker(EffectInvoker effectInvoker, MutatingEngine mutatingEngine, IActorRef sender, bool sendBack, IStateAction action)
 {
     _effectInvoker  = effectInvoker;
     _mutatingEngine = mutatingEngine;
     _sender         = sender;
     _sendBack       = sendBack;
     _action         = action;
 }
Exemplo n.º 5
0
        public override IDataMutation?TryDipatch(IStateAction action, Action <IReducerResult> sendResult, Action onCompled)
        {
            var type = action.GetType();

            return(!_map.TryGetValue(type, out var runner)
                ? null
                : new WorkspaceMutation(() => runner(_source, action), sendResult, onCompled, action.ActionName, action.ActionName));
        }
Exemplo n.º 6
0
        public void UpdateMainStateName(IStateAction action)
        {
            var stateName = $"{action.State.Name}";

            this.stateNameText.text = stateName;
            this.stateEnterTime     = Time.time;

            HideAll(this.mainStates, stateName);

            if (this.mainStateMap.TryGetValue(stateName, out var go))
            {
                go.SetActive(true);
            }
        }
Exemplo n.º 7
0
        public override IDataMutation?TryDipatch(IStateAction action, Action <IReducerResult> sendResult, Action onCompled)
        {
            var reducers = Reducers.Where(r => r.ShouldReduceStateForAction(action)).ToList();

            if (reducers.Count == 0)
            {
                return(null);
            }

            return(MutatingEngine.CreateMutate(action.ActionName, action.Query,
                                               async data =>
            {
                try
                {
                    var isFail = false;
                    foreach (var reducer in reducers)
                    {
                        var mayResult = await reducer.Reduce(data, action);

                        var tempData =
                            Collapse(from result in mayResult
                                     from _ in MayUse(() => sendResult(result))
                                     where result.IsOk
                                     select result.Data);

                        if (tempData.IsNothing())
                        {
                            isFail = true;
                        }

                        data = tempData.Or(data);
                    }

                    return isFail ? Maybe <MutatingContext <TData> > .Nothing : data;
                }
                finally
                {
                    onCompled();
                }
            }));
        }
Exemplo n.º 8
0
        public NestedModel Reduce(NestedModel state, IStateAction action)
        {
            switch (action)
            {
            case UpdateAgeRangeAction a:
                return(state with {
                    AgeRange = a.NewAgeRange
                });

            case UpdateDateAction a:
                return(state with {
                    Date = a.NewDate
                });

            case UpdateRgbChoiceAction a:
                return(state with {
                    RgbChoice = a.Choice
                });

            case UpdateDescriptionAction a:
                return(state with {
                    Description = a.LongText
                });

            case UpdateSelectionAction a:
                return(state with {
                    Selection = a.Selection
                });

            case UpdateColorAction a:
                return(state with {
                    Color = a.Color
                });

            default:
                return(state);
            }
        }
Exemplo n.º 9
0
        public override object VisitStateDefer(PParser.StateDeferContext context)
        {
            if (CurrentMachine.IsSpec)
            {
                throw Handler.DeferredEventInMonitor(context, CurrentMachine);
            }

            // DEFER nonDefaultEventList
            var eventContexts = context.nonDefaultEventList()._events;
            var actions       = new IStateAction[eventContexts.Count];

            for (var i = 0; i < eventContexts.Count; i++)
            {
                var token = eventContexts[i];
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }
                actions[i] = new EventDefer(token, evt);
            }

            return(actions);
        }
Exemplo n.º 10
0
        public override object VisitStateDefer(PParser.StateDeferContext context)
        {
            // annotationSet? SEMI
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("event defer annotations");
            }

            // DEFER nonDefaultEventList
            var eventContexts = context.nonDefaultEventList()._events;
            var actions       = new IStateAction[eventContexts.Count];

            for (var i = 0; i < eventContexts.Count; i++)
            {
                var token = eventContexts[i];
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }
                actions[i] = new EventDefer(token, evt);
            }
            return(actions);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Add action that will be called on <see cref="UpdateState"/>
 /// </summary>
 /// <param name="action"></param>
 public void AddAction(IStateAction action)
 {
     actions.Add(action);
 }
Exemplo n.º 12
0
 public virtual Func <IObservable <MutatingContext <TData> >, IObservable <ReducerResult <TData> > > Reduce(
     IStateAction action)
 {
Exemplo n.º 13
0
 public void Run(IStateAction action, bool?sendBack = null)
 {
 }
Exemplo n.º 14
0
 public OrthogonalState <TState, TTransition, TSignal> On(
     IStateAction action)
 {
     this.State.On(action);
     return(this);
 }
Exemplo n.º 15
0
 public abstract bool AddAction(IStateAction action);
Exemplo n.º 16
0
 public bool RemoveAction(IStateAction toRemove)
 {
     return(this.actions.Remove(toRemove));
 }
Exemplo n.º 17
0
 public EffectInvoker(IEnumerable <IEffect> effects, IStateAction action, IActionInvoker invoker)
 {
     _effects = effects;
     _action  = action;
     _invoker = invoker;
 }
Exemplo n.º 18
0
 public void UpdateMainStateTime(IStateAction _)
 {
     this.stateTimeText.text = $"{Time.time - this.stateEnterTime}";
 }
 public State <TState, TTransition, TSignal> On(
     IStateAction action)
 {
     AddAction(action);
     return(this);
 }
Exemplo n.º 20
0
        public virtual async Task <Maybe <ReducerResult <TData> > > Reduce(Maybe <MutatingContext <TData> > state, IStateAction action)
        {
            try
            {
                var typedAction = (TAction)action;

                if (Validator == null)
                {
                    return(await Reduce(state, (TAction)action));
                }
                var result = await Validator.ValidateAsync(typedAction);

                return(!result.IsValid ? ReducerResult.Fail(state, result.Errors.Select(f => f.ErrorMessage)) : await Reduce(state, (TAction)action));
            }
            catch (Exception e)
            {
                return(ReducerResult.Fail(state, new[] { e.Message }));
            }
        }
Exemplo n.º 21
0
 public Exception DuplicateEventAction(ParserRuleContext location, IStateAction existingAction, State state)
 {
     return(IssueError(location,
                       $"event handler in state '{state.Name}' duplicates handler at {locationResolver.GetLocation(existingAction.SourceLocation)}"));
 }
Exemplo n.º 22
0
 public virtual bool ShouldReduceStateForAction(IStateAction action) => action is TAction;