Пример #1
0
        // The dispatch method decides whether an action can be dispatched
        // based on SAFE's context
        public void Dispatch(string action, PresenterModel data, Action <string> next)
        {
            this._logger.Info("dispatcher received request");
            bool dispatch        = false;
            var  lastStepActions = this._lastStep.Actions;

            this._logger.Info("dispatcher received request" + JsHelpers.JSON.stringify(data));
            this._logger.Info("lastStepActions            " + JsHelpers.JSON.stringify(lastStepActions));


            if (lastStepActions.Count == 0)
            {
                // action validation is disabled
                dispatch = true;
            }
            else
            {
                foreach (var lastStep in lastStepActions)
                {
                    this._logger.Info(lastStep.ToString());
                    if (lastStep.Action == action)
                    {
                        dispatch = true;
                        // tag the action with the stepid
                        // we want to enforce one action per step
                        // if the step does not match we should not dispatch
                        data.__actionId = lastStep.UId;
                        this._logger.Info("tagging action with            " + lastStep.ToString());

                        this._lastStep.Dispatched = action;
                    }
                }
            }

            if (!dispatch)
            {
                this._errorHandler(new { action = action, error = "not allowed" }.ToString());
            }
            else
            {
                if (this._actions.ActionList.ContainsKey(action))
                {
                    // dispatch action
                    this._logger.Info("invoking action            " + data.ToString());
                    this._actions.ActionList[action](data, next);
                }
                else
                {
                    this._errorHandler(new { action = action, error = "not found" }.ToString());
                }
            }
        }
Пример #2
0
        public void Present(PresenterModel data, Action <string> next)
        {
            string actionId = data.__actionId ?? null;

            if (!this._blocked)
            {
                var lastStepActions = this._lastStep.Actions;
                this._logger.Info(lastStepActions.ToString());
                bool presentData = (lastStepActions.Count == 0);

                if (!presentData)
                {
                    // are we expecting that action?
                    foreach (var item in lastStepActions)
                    {
                        if (item.UId == actionId)
                        {
                            presentData = true;
                        }
                    }
                }
                if (presentData)
                {
                    Block();
                    if (!string.IsNullOrEmpty(data.__token))
                    {
                        this._model.__session = this._sessionManager.RehydrateSession(data.__token);
                        this._model.__token   = data.__token;
                    }
                    if (this._saveSnapshot != null)
                    {
                        // Store snapshot in TimeTravel
                        this._saveSnapshot(null, data.ToString());
                    }
                    this._model.Present(data, next);
                }
                else
                {
                    // unexpected actions
                    // possibly an action from previous step that needs to be blocked
                }
            }
            else
            {
                // ignore action's effect
                // this.logger({ blocked: true,data}) ; //todo
            }
        }