mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-26 00:36:29 -05:00
Throttle how often we call into the host side to check for cancellation.
This commit is contained in:
@@ -1618,7 +1618,6 @@ namespace ts {
|
||||
export class OperationCanceledException { }
|
||||
|
||||
export class CancellationTokenObject {
|
||||
|
||||
public static None: CancellationTokenObject = new CancellationTokenObject(null)
|
||||
|
||||
constructor(private cancellationToken: CancellationToken) {
|
||||
|
||||
@@ -327,7 +327,8 @@ namespace ts {
|
||||
}
|
||||
|
||||
public getCancellationToken(): CancellationToken {
|
||||
return this.shimHost.getCancellationToken();
|
||||
var hostCancellationToken = this.shimHost.getCancellationToken();
|
||||
return new ThrottledCancellationToken(hostCancellationToken);
|
||||
}
|
||||
|
||||
public getCurrentDirectory(): string {
|
||||
@@ -346,6 +347,29 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
/** A cancellation that throttles calls to the host */
|
||||
class ThrottledCancellationToken implements CancellationToken {
|
||||
// Store when we last tried to cancel. Checking cancellation can be expensive (as we have
|
||||
// to marshall over to the host layer). So we only bother actually checking once enough
|
||||
// time has passed.
|
||||
private lastCancellationCheckTime = 0;
|
||||
|
||||
constructor(private hostCancellationToken: CancellationToken) {
|
||||
}
|
||||
|
||||
public isCancellationRequested(): boolean {
|
||||
var time = Date.now();
|
||||
var duration = Math.abs(time - this.lastCancellationCheckTime);
|
||||
if (duration > 10) {
|
||||
// Check no more than once every 10 ms.
|
||||
this.lastCancellationCheckTime = time;
|
||||
return this.hostCancellationToken.isCancellationRequested();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class CoreServicesShimHostAdapter implements ParseConfigHost {
|
||||
|
||||
constructor(private shimHost: CoreServicesShimHost) {
|
||||
|
||||
Reference in New Issue
Block a user