Exemplo n.º 1
0
 internal AwaitableStateConfiguration(
     Dictionary <TState, AwaitableStateRepresentation <TState, TTrigger> > representations,
     TState currentState)
 {
     Representations            = representations;
     CurrentStateRepresentation = AwaitableStateConfigurationHelper.FindOrCreateStateRepresentation(
         currentState, representations);
 }
Exemplo n.º 2
0
        internal static AwaitableTriggerRepresentation <TTrigger> CreateTriggerRepresentation <TState, TTrigger>(
            TTrigger trigger,
            AwaitableStateRepresentation <TState, TTrigger> representation)
        {
            var rep = new AwaitableTriggerRepresentation <TTrigger>(trigger);

            representation.Triggers.Add(rep);
            return(rep);
        }
Exemplo n.º 3
0
        internal static AwaitableTriggerRepresentation <TTrigger> FindOrCreateTriggerRepresentation
        <TTrigger, TState>(
            TTrigger trigger,
            AwaitableStateRepresentation <TState, TTrigger> representation)
        {
            var rep = FindTriggerRepresentation(trigger, representation);

            return(rep ?? CreateTriggerRepresentation(trigger, representation));
        }
Exemplo n.º 4
0
        internal static AwaitableStateRepresentation <TState, TTrigger> CreateStateRepresentation <TState, TTrigger>(
            TState state,
            Dictionary <TState, AwaitableStateRepresentation <TState, TTrigger> > representations)
        {
            var rep = new AwaitableStateRepresentation <TState, TTrigger>(state);

            representations[state] = rep;
            return(rep);
        }
Exemplo n.º 5
0
        internal static AwaitableStateRepresentation <TState, TTrigger> FindOrCreateStateRepresentation <TState, TTrigger>
            (TState state,
            Dictionary <TState, AwaitableStateRepresentation <TState, TTrigger> > representations)
        {
            AwaitableStateRepresentation <TState, TTrigger> rep;

            if (representations.TryGetValue(state, out rep))
            {
                if (rep != null)
                {
                    return(rep);
                }
            }
            rep = new AwaitableStateRepresentation <TState, TTrigger>(state);

            representations[state] = rep;

            return(rep);
        }
        internal static async Task FireCoreAsync <TState, TTrigger>(TTrigger trigger,
                                                                    RawAwaitableStateMachineBase <TState, TTrigger> machine, bool raiseInvalidStateOrTrigger = true)
        {
            var currentStateRepresentation = machine.CurrentStateRepresentation;


            var triggerRep =
                await
                AwaitableDiagnosticsHelper.FindAndEvaluateTriggerRepresentationAsync(trigger, machine,
                                                                                     raiseInvalidStateOrTrigger);

            if (triggerRep == null)
            {
                return;
            }

            // Catch invalid paramters before execution.

            Action <Transition <TState, TTrigger> >    triggerAction = null;
            Func <Transition <TState, TTrigger>, Task> triggerFunc   = null;

            if (AwaitableStateConfigurationHelper.CheckFlag(triggerRep.AwaitableTransitionFlags,
                                                            AwaitableTransitionFlag.TriggerActionReturnsTask))
            {
                try
                {
                    triggerFunc = (Func <Transition <TState, TTrigger>, Task>)triggerRep.OnTriggerAction;
                }
                catch (InvalidCastException)
                {
                    if (raiseInvalidStateOrTrigger)
                    {
                        machine.RaiseInvalidTrigger(trigger);
                    }
                    return;
                }
            }
            else
            {
                try
                {
                    triggerAction = (Action <Transition <TState, TTrigger> >)triggerRep.OnTriggerAction;
                }
                catch (InvalidCastException)
                {
                    if (raiseInvalidStateOrTrigger)
                    {
                        machine.RaiseInvalidTrigger(trigger);
                    }
                    return;
                }
            }

            AwaitableStateRepresentation <TState, TTrigger> nextStateRep = null;

            if (AwaitableStateConfigurationHelper.CheckFlag(triggerRep.AwaitableTransitionFlags,
                                                            AwaitableTransitionFlag.DynamicState))
            {
                var dynamicState = await AwaitableDiagnosticsHelper.GetValidatedDynamicTransition <TState, TTrigger>(triggerRep);

                if (dynamicState == null)
                {
                    return;
                }

                var state = dynamicState.Value.ResultingState;

                nextStateRep =
                    AwaitableStateConfigurationHelper.FindStateRepresentation(state,
                                                                              machine.Representations);
                if (nextStateRep == null)
                {
                    if (raiseInvalidStateOrTrigger)
                    {
                        machine.RaiseInvalidState(state);
                    }
                    return;
                }
            }
            else
            {
                nextStateRep =
                    (AwaitableStateRepresentation <TState, TTrigger>)triggerRep.NextStateRepresentationWrapper;
            }


            var transition = new Transition <TState, TTrigger>(currentStateRepresentation.State, nextStateRep.State);

            machine.RaiseTransitionStarted(nextStateRep.State);

            // Current exit

            if (
                AwaitableStateConfigurationHelper.CheckFlag(
                    currentStateRepresentation.AwaitableTransitionFlags,
                    AwaitableTransitionFlag.ExitReturnsTask))
            {
                var exit = (Func <Transition <TState, TTrigger>, Task>)currentStateRepresentation.OnExitAction;
                if (exit != null)
                {
                    await exit(transition);
                }
            }
            else
            {
                var exit = (Action <Transition <TState, TTrigger> >)currentStateRepresentation.OnExitAction;
                exit?.Invoke(transition);
            }

            // Trigger entry

            if (triggerAction != null)
            {
                triggerAction(transition);
            }
            else if (triggerFunc != null)
            {
                await triggerFunc(transition);
            }

            // Next entry

            if (
                AwaitableStateConfigurationHelper.CheckFlag(
                    nextStateRep.AwaitableTransitionFlags, AwaitableTransitionFlag.EntryReturnsTask))
            {
                var entry = (Func <Transition <TState, TTrigger>, Task>)nextStateRep.OnEntryAction;
                if (entry != null)
                {
                    await entry(transition);
                }
            }
            else
            {
                var entry = (Action <Transition <TState, TTrigger> >)nextStateRep.OnEntryAction;
                entry?.Invoke(transition);
            }

            var pastState = machine.CurrentState;

            machine.CurrentStateRepresentation = nextStateRep;
            machine.RaiseTransitionExecuted(pastState);
        }
Exemplo n.º 7
0
 internal static AwaitableTriggerRepresentation <TTrigger> FindTriggerRepresentation <TState, TTrigger>(
     TTrigger trigger,
     AwaitableStateRepresentation <TState, TTrigger> representation)
 {
     return(representation.Triggers.Find(x => x.Trigger.Equals(trigger)));
 }