Skip to content

Commit af94f1c

Browse files
Throttle how often we call into the host side to check for cancellation.
1 parent bcd8cce commit af94f1c

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/services/services.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,6 @@ namespace ts {
16181618
export class OperationCanceledException { }
16191619

16201620
export class CancellationTokenObject {
1621-
16221621
public static None: CancellationTokenObject = new CancellationTokenObject(null)
16231622

16241623
constructor(private cancellationToken: CancellationToken) {

src/services/shims.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ namespace ts {
327327
}
328328

329329
public getCancellationToken(): CancellationToken {
330-
return this.shimHost.getCancellationToken();
330+
var hostCancellationToken = this.shimHost.getCancellationToken();
331+
return new ThrottledCancellationToken(hostCancellationToken);
331332
}
332333

333334
public getCurrentDirectory(): string {
@@ -346,6 +347,29 @@ namespace ts {
346347
}
347348
}
348349

350+
/** A cancellation that throttles calls to the host */
351+
class ThrottledCancellationToken implements CancellationToken {
352+
// Store when we last tried to cancel. Checking cancellation can be expensive (as we have
353+
// to marshall over to the host layer). So we only bother actually checking once enough
354+
// time has passed.
355+
private lastCancellationCheckTime = 0;
356+
357+
constructor(private hostCancellationToken: CancellationToken) {
358+
}
359+
360+
public isCancellationRequested(): boolean {
361+
var time = Date.now();
362+
var duration = Math.abs(time - this.lastCancellationCheckTime);
363+
if (duration > 10) {
364+
// Check no more than once every 10 ms.
365+
this.lastCancellationCheckTime = time;
366+
return this.hostCancellationToken.isCancellationRequested();
367+
}
368+
369+
return false;
370+
}
371+
}
372+
349373
export class CoreServicesShimHostAdapter implements ParseConfigHost {
350374

351375
constructor(private shimHost: CoreServicesShimHost) {

0 commit comments

Comments
 (0)