void StateMachine <TInstance> .RaiseEvent(Composer composer, TInstance instance, Event @event)
        {
            composer.Execute(() =>
            {
                State <TInstance> state = _instanceStateAccessor.Get(instance);

                State <TInstance> instanceState = _stateCache[state.Name];

                return(composer.ComposeEvent(instance, instanceState, @event));
            });
        }
Example #2
0
        async Task <State <TInstance> > StateAccessor <TInstance> .Get(InstanceContext <TInstance> context)
        {
            var state = await _stateAccessor.Get(context).ConfigureAwait(false);

            if (state == null)
            {
                var behaviorContext = new EventBehaviorContext <TInstance>(context);

                await _initialBehavior.Execute(behaviorContext).ConfigureAwait(false);

                state = await _stateAccessor.Get(context).ConfigureAwait(false);
            }
            return(state);
        }
        State <TInstance> StateAccessor <TInstance> .Get(TInstance instance)
        {
            State <TInstance> state = _rawStateAccessor.Get(instance);

            if (state == null)
            {
                var composer = new TaskComposer <TInstance>(CancellationToken.None);
                _initialActivity.Execute(composer, instance);
                composer.Finish().Wait();

                state = _rawStateAccessor.Get(instance);
            }
            return(state);
        }
        public static Task <State <TInstance> > GetState <TInstance>(this StateAccessor <TInstance> accessor, TInstance instance)
            where TInstance : class
        {
            var context = new StateMachineInstanceContext <TInstance>(instance);

            return(accessor.Get(context));
        }
Example #5
0
        void Transition(Composer composer, TInstance instance)
        {
            composer.Execute(() =>
            {
                State <TInstance> currentState = _currentStateAccessor.Get(instance);
                if (_toState.Equals(currentState))
                {
                    return(composer.ComposeCompleted());
                }

                var taskComposer = new TaskComposer <TInstance>(composer.CancellationToken);

                if (currentState != null)
                {
                    currentState.Raise(taskComposer, instance, currentState.Leave);
                }

                _toState.Raise(taskComposer, instance, _toState.BeforeEnter, currentState);

                ((Composer)taskComposer).Execute(() => _currentStateAccessor.Set(instance, _toState));

                if (currentState != null)
                {
                    currentState.Raise(taskComposer, instance, currentState.AfterLeave, _toState);
                }

                _toState.Raise(taskComposer, instance, _toState.Enter);

                return(taskComposer.Finish());
            });
        }
Example #6
0
        public override void Execute(TInstance instance)
        {
            State <TInstance> currentState = _currentStateAccessor.Get(instance);

            if (currentState == _targetState)
            {
                return;
            }

            if (currentState != null)
            {
                currentState.RaiseEvent(instance, currentState.Exit);
            }

            _currentStateAccessor.Set(instance, _targetState);

            _targetState.RaiseEvent(instance, _targetState.Entry);
        }
        async Task Transition(BehaviorContext <TInstance> context)
        {
            State <TInstance> currentState = await _currentStateAccessor.Get(context).ConfigureAwait(false);

            if (_toState.Equals(currentState))
            {
                return; // Homey don't play re-entry, at least not yet.
            }
            if (currentState != null && !currentState.HasState(_toState))
            {
                await RaiseCurrentStateLeaveEvents(context, currentState).ConfigureAwait(false);
            }

            await RaiseBeforeEnterEvents(context, currentState, _toState).ConfigureAwait(false);

            await _currentStateAccessor.Set(context, _toState).ConfigureAwait(false);

            if (currentState != null)
            {
                await RaiseAfterLeaveEvents(context, currentState, _toState).ConfigureAwait(false);
            }

            if (currentState == null || !_toState.HasState(currentState))
            {
                State <TInstance> superState = _toState.SuperState;
                while (superState != null && (currentState == null || !superState.HasState(currentState)))
                {
                    BehaviorContext <TInstance> superStateEnterContext = context.GetProxy(superState.Enter);
                    await superState.Raise(superStateEnterContext).ConfigureAwait(false);

                    superState = superState.SuperState;
                }

                BehaviorContext <TInstance> enterContext = context.GetProxy(_toState.Enter);
                await _toState.Raise(enterContext).ConfigureAwait(false);
            }
        }