Exemplo n.º 1
0
        async Task <string> IRCallbacks.ReadConsole(IReadOnlyList <IRContext> contexts, string prompt, int len, bool addToHistory, CancellationToken ct)
        {
            await TaskUtilities.SwitchToBackgroundThread();

            if (!_initializedTcs.Task.IsCompleted)
            {
                await AfterHostStarted(_startupInfo);
            }

            var callback = _callback;

            if (!addToHistory && callback != null)
            {
                return(await callback.ReadUserInput(prompt, len, ct));
            }

            var currentRequest = Interlocked.Exchange(ref _currentRequestSource, null);

            _contexts = contexts;
            Prompt    = GetDefaultPrompt(prompt);
            MaxLength = len;

            var requestEventArgs = new RBeforeRequestEventArgs(contexts, Prompt, len, addToHistory);

            BeforeRequest?.Invoke(this, requestEventArgs);

            OnMutated();

            currentRequest?.CompleteResponse();

            string consoleInput = null;

            do
            {
                ct.ThrowIfCancellationRequested();
                try {
                    consoleInput = await ReadNextRequest(Prompt, len, ct);
                } catch (OperationCanceledException ex) when(!(ex is CancelAllException))
                {
                    // If request was canceled through means other than our token, it indicates the refusal of
                    // that requestor to respond to that particular prompt, so move on to the next requestor.
                    // If it was canceled through the token, then host itself is shutting down, and cancellation
                    // will be propagated on the entry to next iteration of this loop.
                    //
                    // If request was canceled due to CancelAllAsync, then we should not continue to process this
                    // ReadConsole call at all. Under normal conditions, ct will also be marked as canceled; but
                    // there is a potential for a race condition where we get a cancellation exception here, but
                    // ct is not marked as canceled yet. Explicitly checking for CancelAllException handles this.
                }
            } while (consoleInput == null);


            // We only want to fire 'directory changed' events when it is initiated by the user
            _processingChangeDirectoryCommand = consoleInput.StartsWithOrdinal("setwd");

            consoleInput = consoleInput.EnsureLineBreak();
            AfterRequest?.Invoke(this, new RAfterRequestEventArgs(contexts, Prompt, consoleInput, addToHistory, currentRequest?.IsVisible ?? false));

            return(consoleInput);
        }
Exemplo n.º 2
0
 public Task CancelAllAsync(CancellationToken сancellationToken = default(CancellationToken))
 {
     if (_inter != null)
     {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(_inter.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         _inter = null;
     }
     return(Task.CompletedTask);
 }
Exemplo n.º 3
0
 private ITraktModule RegisterModule(ITraktModule module)
 {
     module.BeforeRequest += (sender, e) => {
         BeforeRequest?.Invoke(sender, e);
     };
     module.AfterRequest += (sender, e) => {
         AfterRequest?.Invoke(sender, e);
     };
     return(module);
 }
Exemplo n.º 4
0
        async Task <string> IRCallbacks.ReadConsole(IReadOnlyList <IRContext> contexts, string prompt, int len, bool addToHistory, bool isEvaluationAllowed, CancellationToken ct)
        {
            await TaskUtilities.SwitchToBackgroundThread();

            var currentRequest = Interlocked.Exchange(ref _currentRequestSource, null);

            _contexts = contexts;
            Prompt    = prompt;
            MaxLength = len;

            var requestEventArgs = new RRequestEventArgs(contexts, prompt, len, addToHistory);

            BeforeRequest?.Invoke(this, requestEventArgs);

            CancellationTokenSource evaluationCts;
            Task evaluationTask;

            if (isEvaluationAllowed)
            {
                evaluationCts  = new CancellationTokenSource();
                evaluationTask = EvaluateUntilCancelled(contexts, evaluationCts.Token, ct); // will raise Mutate
            }
            else
            {
                evaluationCts  = null;
                evaluationTask = Task.CompletedTask;
                OnMutated();
            }

            currentRequest?.CompleteResponse();

            string consoleInput = null;

            do
            {
                ct.ThrowIfCancellationRequested();
                try {
                    consoleInput = await ReadNextRequest(prompt, len, ct);
                } catch (OperationCanceledException) {
                    // If request was canceled through means other than our token, it indicates the refusal of
                    // that requestor to respond to that particular prompt, so move on to the next requestor.
                    // If it was canceled through the token, then host itself is shutting down, and cancellation
                    // will be propagated on the entry to next iteration of this loop.
                }
            } while (consoleInput == null);

            // If evaluation was allowed, cancel evaluation processing but await evaluation that is in progress
            evaluationCts?.Cancel();
            await evaluationTask;

            AfterRequest?.Invoke(this, requestEventArgs);

            return(consoleInput);
        }
Exemplo n.º 5
0
 public Task CancelAllAsync()
 {
     if (_eval != null)
     {
         AfterRequest?.Invoke(this, new RRequestEventArgs(_eval.Contexts, Prompt, 4096, true));
         _eval = null;
     }
     else if (_inter != null)
     {
         AfterRequest?.Invoke(this, new RRequestEventArgs(_inter.Contexts, Prompt, 4096, true));
         _inter = null;
     }
     return(Task.CompletedTask);
 }
Exemplo n.º 6
0
 public Task CancelAllAsync()
 {
     if (Evaluation != null)
     {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(Evaluation.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         Evaluation = null;
     }
     else if (_inter != null)
     {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(_inter.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         _inter = null;
     }
     return(Task.CompletedTask);
 }
Exemplo n.º 7
0
 private void OnAfterRequest(IHttpRequest request, IHttpResponse response)
 {
     AfterRequest.Raise(this, new AfterRequestEventArgs(request, response));
     Logger.AfterRequest(request, response);
 }