Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit faa3485

Browse files
committed
fix: Canceling already run task should not double decrement task counter
Closes #290
1 parent 9b3e779 commit faa3485

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

lib/browser/browser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ function patchTimer(
8686
var clearNative = patchMethod(window, cancelName, (delegate: Function) => function(self: any, args: any[]) {
8787
var task: Task = args[0];
8888
if (task && typeof task.type == 'string') {
89-
task.zone.cancelTask(task);
89+
if (task.cancelFn) {
90+
// Do not cancel already canceled functions
91+
task.zone.cancelTask(task);
92+
}
9093
} else {
9194
// cause an error by calling it directly.
9295
delegate.apply(window, args);

lib/zone.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,11 @@ var Zone: ZoneType = (function(global) {
581581
}
582582
}
583583
} finally {
584+
if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) {
585+
task.cancelFn = null;
586+
}
584587
_currentZone = oldZone;
585588
_currentTask = previousTask;
586-
if (task.type == 'microTask') {
587-
588-
}
589589
}
590590
}
591591

@@ -761,7 +761,6 @@ var Zone: ZoneType = (function(global) {
761761
var prev = counts[type];
762762
var next = counts[type] = prev + count;
763763
if (next < 0) {
764-
debugger;
765764
throw new Error('More tasks executed then were scheduled.');
766765
}
767766
if (prev == 0 || next == 0) {

test/browser/setTimeout.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ describe('setTimeout', function () {
4040
});
4141
});
4242

43+
it('should allow double cancelation of fns registered with setTimeout', function (done) {
44+
var spy = jasmine.createSpy('spy');
45+
var cancelId = setTimeout(spy, 0);
46+
setTimeout(function () {
47+
expect(spy).toHaveBeenCalled();
48+
setTimeout(function () {
49+
clearTimeout(cancelId);
50+
done();
51+
});
52+
});
53+
});
54+
4355
it('should pass invalid values through', function () {
4456
clearTimeout(null);
4557
clearTimeout(<any>{});

0 commit comments

Comments
 (0)