Пример #1
0
        public virtual async ValueTask <ActionEvent> ExecuteAsync(CancellationToken token)
        {
            var results   = new object[_queue.Count];
            var actions   = new IAction[_queue.Count];
            var lastEvent = ActionEvent.EmptyOkEvent;

            for (var i = 0; i < _queue.Count; i++)
            {
                if (token.IsCancellationRequested)
                {
                    return(ActionEvent.Cancel(this));
                }

                actions[i] = actions[i] ?? _queue[i](results); // action只生成一次
                var action = actions[i];
                if (action == null)
                {
                    continue;
                }

                var result = await action.ExecuteAutoAsync(token).DonotCapture();

                results[i] = result.Target;
                switch (result.Type)
                {
                case ActionEventType.EvtError:
                case ActionEventType.EvtCanceled:
                    return(result);

                case ActionEventType.EvtRetry:
                case ActionEventType.EvtRepeat:
                    --i;     // 回退,即重复执行
                    break;

                default:
                    lastEvent = result;
                    break;
                }
            }
            return(lastEvent);
        }