Skip to content

Commit 2725ec6

Browse files
committed
Convert PSRL OnIdle handler to take a CancellationToken
1 parent ccc2fc2 commit 2725ec6

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

src/PowerShellEditorServices/Services/PowerShell/Console/ConsoleReadLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public bool TryOverrideReadKey(Func<bool, ConsoleKeyInfo> readKeyFunc)
5252
return true;
5353
}
5454

55-
public bool TryOverrideIdleHandler(Action idleHandler)
55+
public bool TryOverrideIdleHandler(Action<CancellationToken> idleHandler)
5656
{
5757
_psrlProxy.OverrideIdleHandler(idleHandler);
5858
return true;

src/PowerShellEditorServices/Services/PowerShell/Console/IReadLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ internal interface IReadLine
1515

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

18-
bool TryOverrideIdleHandler(Action idleHandler);
18+
bool TryOverrideIdleHandler(Action<CancellationToken> idleHandler);
1919
}
2020
}

src/PowerShellEditorServices/Services/PowerShell/Console/PSReadLineProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ internal void OverrideReadKey(Func<bool, ConsoleKeyInfo> readKeyFunc)
171171
_readKeyOverrideField.SetValue(null, readKeyFunc);
172172
}
173173

174-
internal void OverrideIdleHandler(Action idleAction)
174+
internal void OverrideIdleHandler(Action<CancellationToken> idleAction)
175175
{
176176
_handleIdleOverrideField.SetValue(null, idleAction);
177177
}

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
611611
return pwsh;
612612
}
613613

614-
public PowerShell CreateInitialPowerShell(
614+
private PowerShell CreateInitialPowerShell(
615615
HostStartupInfo hostStartupInfo,
616616
ReadLineProvider readLineProvider)
617617
{
@@ -664,14 +664,14 @@ private Runspace CreateInitialRunspace(InitialSessionState initialSessionState)
664664
return runspace;
665665
}
666666

667-
private void OnPowerShellIdle()
667+
private void OnPowerShellIdle(CancellationToken idleCancellationToken)
668668
{
669669
if (_taskQueue.Count == 0)
670670
{
671671
return;
672672
}
673673

674-
using (CancellationScope cancellationScope = _cancellationContext.EnterScope(isIdleScope: true))
674+
using (CancellationScope cancellationScope = _cancellationContext.EnterScope(isIdleScope: true, idleCancellationToken))
675675
{
676676
while (!cancellationScope.CancellationToken.IsCancellationRequested
677677
&& _taskQueue.TryTake(out ISynchronousTask task))

src/PowerShellEditorServices/Services/PowerShell/Utility/CancellationContext.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ public CancellationContext()
3030
_cancellationSourceStack = new ConcurrentStack<CancellationScope>();
3131
}
3232

33-
public CancellationScope EnterScope(bool isIdleScope)
33+
public CancellationScope EnterScope(bool isIdleScope, CancellationToken cancellationToken)
3434
{
3535
CancellationTokenSource newScopeCancellationSource = _cancellationSourceStack.TryPeek(out CancellationScope parentScope)
36-
? CancellationTokenSource.CreateLinkedTokenSource(parentScope.CancellationToken)
37-
: new CancellationTokenSource();
36+
? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, parentScope.CancellationToken)
37+
: CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
3838

3939
return EnterScope(isIdleScope, newScopeCancellationSource);
4040
}
4141

42+
public CancellationScope EnterScope(bool isIdleScope) => EnterScope(isIdleScope, CancellationToken.None);
43+
4244
public void CancelCurrentTask()
4345
{
4446
if (_cancellationSourceStack.TryPeek(out CancellationScope currentCancellationSource))

0 commit comments

Comments
 (0)