internal static AwaitableStateConfiguration <TState, TTrigger> Permit <TState, TTrigger>(
            AwaitableStateConfiguration <TState, TTrigger> config, object predicate, TTrigger trigger,
            TState resultingState, object onTriggerAction, AwaitableTransitionFlag flags)
        {
            Contract.NotNull(trigger != null, nameof(trigger));
            Contract.NotNull(resultingState != null, nameof(resultingState));

            if (
                AwaitableStateConfigurationHelper.FindTriggerRepresentation(trigger,
                                                                            config.CurrentStateRepresentation) != null)
            {
                ExceptionHelper.ThrowExclusiveOperation();
            }

            var rep = AwaitableStateConfigurationHelper.CreateTriggerRepresentation(trigger,
                                                                                    config.CurrentStateRepresentation);

            rep.NextStateRepresentationWrapper =
                AwaitableStateConfigurationHelper.FindOrCreateStateRepresentation(resultingState,
                                                                                  config.Representations);
            rep.OnTriggerAction             = onTriggerAction;
            rep.ConditionalTriggerPredicate = predicate;
            rep.AwaitableTransitionFlags   |= flags;

            return(config);
        }
Example #2
0
        FindAndEvaluateTriggerRepresentationAsync
        <TState, TTrigger>(TTrigger trigger, RawAwaitableStateMachineBase <TState, TTrigger> machine,
                           bool raiseInvalidTriggers = true)
        {
            var triggerRep = AwaitableStateConfigurationHelper.FindTriggerRepresentation(trigger,
                                                                                         machine.CurrentStateRepresentation);

            if (triggerRep == null)
            {
                if (raiseInvalidTriggers)
                {
                    machine.RaiseInvalidTrigger(trigger);
                }
                return(null);
            }

            if (AwaitableStateConfigurationHelper.CheckFlag(triggerRep.AwaitableTransitionFlags,
                                                            AwaitableTransitionFlag.TriggerPredicateReturnsTask))
            {
                var predicate = (Func <Task <bool> >)triggerRep.ConditionalTriggerPredicate;
                if (predicate != null)
                {
                    if (!await predicate())
                    {
                        if (raiseInvalidTriggers)
                        {
                            machine.RaiseInvalidTrigger(trigger);
                        }
                        return(null);
                    }
                }
            }
            else
            {
                var predicate = (Func <bool>)triggerRep.ConditionalTriggerPredicate;
                if (predicate != null)
                {
                    if (!predicate())
                    {
                        if (raiseInvalidTriggers)
                        {
                            machine.RaiseInvalidTrigger(trigger);
                        }
                        return(null);
                    }
                }
            }

            // Handle ignored trigger

            if (triggerRep.NextStateRepresentationWrapper == null)
            {
                return(null);
            }

            return(triggerRep);
        }
        internal static AwaitableStateConfiguration <TState, TTrigger> Ignore <TState, TTrigger>(
            AwaitableStateConfiguration <TState, TTrigger> config, object predicate, TTrigger trigger,
            AwaitableTransitionFlag flags)
        {
            Contract.NotNull(trigger != null, nameof(trigger));

            if (
                AwaitableStateConfigurationHelper.FindTriggerRepresentation(trigger, config.CurrentStateRepresentation) !=
                null)
            {
                ExceptionHelper.ThrowExclusiveOperation();
            }

            var rep = AwaitableStateConfigurationHelper.CreateTriggerRepresentation(trigger,
                                                                                    config.CurrentStateRepresentation);

            rep.NextStateRepresentationWrapper = null;
            rep.ConditionalTriggerPredicate    = predicate;
            rep.AwaitableTransitionFlags      |= flags;

            return(config);
        }
        internal static AwaitableStateConfiguration <TState, TTrigger> PermitDynamic <TState, TTrigger>(
            AwaitableStateConfiguration <TState, TTrigger> config, TTrigger trigger,
            object targetStateFunc,
            object onTriggerAction, AwaitableTransitionFlag flags)
        {
            Contract.NotNull(trigger != null, nameof(trigger));
            Contract.NotNull(targetStateFunc != null, nameof(targetStateFunc));

            if (
                AwaitableStateConfigurationHelper.FindTriggerRepresentation(trigger, config.CurrentStateRepresentation) !=
                null)
            {
                ExceptionHelper.ThrowExclusiveOperation();
            }
            var rep = AwaitableStateConfigurationHelper.CreateTriggerRepresentation(trigger,
                                                                                    config.CurrentStateRepresentation);

            rep.NextStateRepresentationWrapper = targetStateFunc;
            rep.OnTriggerAction             = onTriggerAction;
            rep.ConditionalTriggerPredicate = null;
            rep.AwaitableTransitionFlags   |= flags;

            return(config);
        }