예제 #1
0
        public async IAsyncEnumerable <TEvent> GetEventsAsync(object data = null,
                                                              [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            var stateModel = _model.States[Current];

            var events = _model.Events
                         .Where(x => !stateModel.Events.ContainsKey(x.Key))
                         .Concat(stateModel.Events);

            var args = new FsmTriggerArgs <TState, TEvent>
            {
                Fsm  = this,
                Data = data,
                CancellationToken = cancellationToken,
            };

            foreach (var kvp in events)
            {
                args.Event = kvp.Key;

                if (kvp.Value.Enable == null || await kvp.Value.Enable(args).ConfigureAwait(false))
                {
                    yield return(kvp.Key);
                }
            }
        }
예제 #2
0
 private Task OnComplete(FsmTriggerArgs <TState, TEvent> args, object result)
 {
     return(_model.OnComplete?.Invoke(new FsmCompleteArgs <TState, TEvent>
     {
         Fsm = this,
         Event = args.Event,
         Data = args.Data,
         CancellationToken = args.CancellationToken,
         Result = result,
     }) ?? FrameworkExt.CompletedTask);
 }
예제 #3
0
        public async Task <object> TriggerAsync(TEvent e, object data = null, CancellationToken cancellationToken = default)
        {
            var args = new FsmTriggerArgs <TState, TEvent>
            {
                Fsm               = this,
                Event             = e,
                Data              = data,
                CancellationToken = cancellationToken,
            };

            await OnTrigger(args).ConfigureAwait(false);

            var stateModel = _model.States[Current];
            FsmEventModel <TState, TEvent> eventModel;

            if (!stateModel.Events.TryGetValue(e, out eventModel) && !_model.Events.TryGetValue(e, out eventModel))
            {
                await OnError(args, _eventNotFound, e).ConfigureAwait(false);

                return(null);
            }

            if (eventModel.Enable != null && !await eventModel.Enable(args).ConfigureAwait(false))
            {
                await OnError(args, _eventDisabled, e).ConfigureAwait(false);

                return(null);
            }

            cancellationToken.ThrowIfCancellationRequested();

            await OnFire(args).ConfigureAwait(false);

            var result = eventModel.Execute == null ? null
                : await eventModel.Execute(args).ConfigureAwait(false);

            if (eventModel.JumpTo != null)
            {
                var next = await eventModel.JumpTo(args).ConfigureAwait(false);

                var done = await JumpToAsync(next, data).ConfigureAwait(false);

                if (eventModel.Execute == null)
                {
                    result = done;
                }
            }

            await OnComplete(args, result).ConfigureAwait(false);

            return(result);
        }
예제 #4
0
 private Task OnFire(FsmTriggerArgs <TState, TEvent> args)
 {
     return(_model.OnFire?.Invoke(args) ?? FrameworkExt.CompletedTask);
 }