示例#1
0
    /// <summary>
    /// Caller presents a decision to the Gateway and waits for the gateway to return an choice.
    /// </summary>
    public Task <T> Decision <T>(Select.TypedDecision <T> originalDecision) where T : class, IOption
    {
        if (originalDecision == null)
        {
            throw new ArgumentNullException(nameof(originalDecision));
        }

        if (activeDecisionMaker != null)
        {
            throw new InvalidOperationException($"Pending decision was not properly awaited. Current:[{activeDecisionMaker.Decision.Prompt}], Previous:[{originalDecision.Prompt}] ");
        }

        var promise       = new TaskCompletionSource <T>();
        var decisionMaker = new ActionHelper <T>(originalDecision, promise);
        var decision      = decisionMaker.Decision;

        if (decision.Options.Length == 0)
        {
            // Auto-Select NULL
            promise.TrySetResult(null);
        }

        else if (decision.Options.Length == 1 && decision.AllowAutoSelect)
        {
            // Auto-Select Single
            decisionMaker.Select(decision.Options[0]);
            Log(new DecisionLogEntry(decision.Options[0], decision, true));
        }
        else
        {
            activeDecisionMaker = decisionMaker;
            signal.Set();
            NewWaitingDecision?.Invoke(decision);
        }
        return(promise.Task);
    }
示例#2
0
 public Task <T> Decision <T>(Select.TypedDecision <T> originalDecision) where T : class, IOption => Self.Action.Decision(originalDecision);