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); }
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); } }
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) }); } }
public ResultInvoker(EffectInvoker effectInvoker, MutatingEngine mutatingEngine, IActorRef sender, bool sendBack, IStateAction action) { _effectInvoker = effectInvoker; _mutatingEngine = mutatingEngine; _sender = sender; _sendBack = sendBack; _action = action; }
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)); }
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); } }
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(); } })); }
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); } }
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); }
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); }
/// <summary> /// Add action that will be called on <see cref="UpdateState"/> /// </summary> /// <param name="action"></param> public void AddAction(IStateAction action) { actions.Add(action); }
public virtual Func <IObservable <MutatingContext <TData> >, IObservable <ReducerResult <TData> > > Reduce( IStateAction action) {
public void Run(IStateAction action, bool?sendBack = null) { }
public OrthogonalState <TState, TTransition, TSignal> On( IStateAction action) { this.State.On(action); return(this); }
public abstract bool AddAction(IStateAction action);
public bool RemoveAction(IStateAction toRemove) { return(this.actions.Remove(toRemove)); }
public EffectInvoker(IEnumerable <IEffect> effects, IStateAction action, IActionInvoker invoker) { _effects = effects; _action = action; _invoker = invoker; }
public void UpdateMainStateTime(IStateAction _) { this.stateTimeText.text = $"{Time.time - this.stateEnterTime}"; }
public State <TState, TTransition, TSignal> On( IStateAction action) { AddAction(action); return(this); }
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 })); } }
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)}")); }
public virtual bool ShouldReduceStateForAction(IStateAction action) => action is TAction;