Throttle how often we call into the host side to check for cancellation.

This commit is contained in:
Cyrus Najmabadi
2015-07-06 14:04:42 -07:00
parent bcd8cce03d
commit af94f1c5d0
2 changed files with 25 additions and 2 deletions

View File

@@ -1618,7 +1618,6 @@ namespace ts {
export class OperationCanceledException { }
export class CancellationTokenObject {
public static None: CancellationTokenObject = new CancellationTokenObject(null)
constructor(private cancellationToken: CancellationToken) {

View File

@@ -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) {