Example #1
0
        private void Enter(Delegate callback = null)
        {
            // immediately call back if we have already transitioned, and exit
            if (_lifecycle.state == _finalState)
            {
                CallCallback(callback);
                return;
            }

            // queue this callback if we are mid transition, and exit
            if (_lifecycle.state == _transitionState)
            {
                if (callback != null)
                {
                    _callbacks.Add(callback);
                }
                return;
            }

            // report invalid transition, and exit
            if (InvalidTransition())
            {
                ReportError("Invalid transition", new List <Delegate> {
                    callback
                });
                return;
            }

            // store the initial lifecycle state in case we need to roll back
            LifecycleState initialState = _lifecycle.state;

            // queue the first callback
            if (callback != null)
            {
                _callbacks.Add(callback);
            }

            // put lifecycle into transition state
            SetState(_transitionState);

            // run before handlers
            _dispatcher.DispatchMessage(_name, delegate(object error)
            {
                // revert state, report error, and exit
                if (error != null)
                {
                    SetState(initialState);
                    ReportError(error, _callbacks);
                    return;
                }

                // dispatch pre transition and transition events
                if (preTransition != null)
                {
                    preTransition();
                }

                ProcessCallbacks(_whenCallbacks);

                if (transition != null)
                {
                    transition();
                }

                // put lifecycle into final state
                SetState(_finalState);

                // process callback queue (dup and trash for safety)
                CallCallbacks(_callbacks.ToArray());
                _callbacks.Clear();

                ProcessCallbacks(_postCallbacks);

                if (postTransition != null)
                {
                    postTransition();
                }
            }, _reverse);
        }
 public void dispatchMessage_runs()
 {
     dispatcher.DispatchMessage(message);
 }