Skip to content

Convert PSRL OnIdle handler to take a CancellationToken #1587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool TryOverrideReadKey(Func<bool, ConsoleKeyInfo> readKeyFunc)
return true;
}

public bool TryOverrideIdleHandler(Action idleHandler)
public bool TryOverrideIdleHandler(Action<CancellationToken> idleHandler)
{
_psrlProxy.OverrideIdleHandler(idleHandler);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ internal interface IReadLine

bool TryOverrideReadKey(Func<bool, ConsoleKeyInfo> readKeyOverride);

bool TryOverrideIdleHandler(Action idleHandler);
bool TryOverrideIdleHandler(Action<CancellationToken> idleHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ internal void OverrideReadKey(Func<bool, ConsoleKeyInfo> readKeyFunc)
_readKeyOverrideField.SetValue(null, readKeyFunc);
}

internal void OverrideIdleHandler(Action idleAction)
internal void OverrideIdleHandler(Action<CancellationToken> idleAction)
{
_handleIdleOverrideField.SetValue(null, idleAction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
return pwsh;
}

public (PowerShell, EngineIntrinsics) CreateInitialPowerShell(
private (PowerShell, EngineIntrinsics) CreateInitialPowerShell(
HostStartupInfo hostStartupInfo,
ReadLineProvider readLineProvider)
{
Expand Down Expand Up @@ -669,7 +669,7 @@ private Runspace CreateInitialRunspace(InitialSessionState initialSessionState)
return runspace;
}

private void OnPowerShellIdle()
private void OnPowerShellIdle(CancellationToken idleCancellationToken)
{
IReadOnlyList<PSEventSubscriber> eventSubscribers = _mainRunspaceEngineIntrinsics.Events.Subscribers;

Expand All @@ -696,7 +696,7 @@ private void OnPowerShellIdle()
return;
}

using (CancellationScope cancellationScope = _cancellationContext.EnterScope(isIdleScope: true))
using (CancellationScope cancellationScope = _cancellationContext.EnterScope(isIdleScope: true, idleCancellationToken))
{
while (!cancellationScope.CancellationToken.IsCancellationRequested
&& _taskQueue.TryTake(out ISynchronousTask task))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ public CancellationContext()
_cancellationSourceStack = new ConcurrentStack<CancellationScope>();
}

public CancellationScope EnterScope(bool isIdleScope)
public CancellationScope EnterScope(bool isIdleScope, CancellationToken cancellationToken)
{
CancellationTokenSource newScopeCancellationSource = _cancellationSourceStack.TryPeek(out CancellationScope parentScope)
? CancellationTokenSource.CreateLinkedTokenSource(parentScope.CancellationToken)
: new CancellationTokenSource();
? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, parentScope.CancellationToken)
: CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

return EnterScope(isIdleScope, newScopeCancellationSource);
}

public CancellationScope EnterScope(bool isIdleScope) => EnterScope(isIdleScope, CancellationToken.None);

public void CancelCurrentTask()
{
if (_cancellationSourceStack.TryPeek(out CancellationScope currentCancellationSource))
Expand Down