async Task RaiseCurrentStateLeaveEvents(BehaviorContext <TInstance> context, State <TInstance> fromState)
        {
            BehaviorContext <TInstance> leaveContext = context.GetProxy(fromState.Leave);
            await fromState.Raise(leaveContext).ConfigureAwait(false);

            State <TInstance> superState = fromState.SuperState;

            while (superState != null && !superState.HasState(_toState))
            {
                BehaviorContext <TInstance> superStateLeaveContext = context.GetProxy(superState.Leave);
                await superState.Raise(superStateLeaveContext).ConfigureAwait(false);

                superState = superState.SuperState;
            }
        }
        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);
            }
        }
        async Task RaiseBeforeEnterEvents(BehaviorContext <TInstance> context, State <TInstance> currentState, State <TInstance> toState)
        {
            State <TInstance> superState = toState.SuperState;

            if (superState != null && (currentState == null || !superState.HasState(currentState)))
            {
                await RaiseBeforeEnterEvents(context, currentState, superState).ConfigureAwait(false);
            }

            if (currentState != null && toState.HasState(currentState))
            {
                return;
            }

            BehaviorContext <TInstance, State> beforeContext = context.GetProxy(toState.BeforeEnter, toState);
            await toState.Raise(beforeContext).ConfigureAwait(false);
        }
        async Task RaiseAfterLeaveEvents(BehaviorContext <TInstance> context, State <TInstance> fromState, State <TInstance> toState)
        {
            if (fromState.HasState(toState))
            {
                return;
            }

            BehaviorContext <TInstance, State> afterContext = context.GetProxy(fromState.AfterLeave, fromState);
            await fromState.Raise(afterContext).ConfigureAwait(false);

            State <TInstance> superState = fromState.SuperState;

            if (superState != null)
            {
                await RaiseAfterLeaveEvents(context, superState, toState).ConfigureAwait(false);
            }
        }
Example #5
0
        Task Behavior <TInstance> .Execute <T>(BehaviorContext <TInstance, T> context)
        {
            BehaviorContext <TInstance, TData> nextContext = context as BehaviorContext <TInstance, TData> ?? context.GetProxy(_event, _data);

            return(_next.Execute(nextContext));
        }
Example #6
0
 public BehaviorContext <TInstance> GetProxy(Event @event)
 {
     return(_context.GetProxy(@event));
 }
Example #7
0
        Task Behavior <TInstance> .Execute(BehaviorContext <TInstance> context)
        {
            var nextContext = context.GetProxy(_event, _data);

            return(_next.Execute(nextContext));
        }