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

Commit a6967ad

Browse files
frankwallismhevery
authored andcommitted
fix(setTimeout): fix for #290, allow clearTimeout to be called in setTimeout callback
Closes #301
1 parent e125173 commit a6967ad

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

lib/zone.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@ const Zone: ZoneType = (function(global) {
579579
const oldZone = _currentZone;
580580
_currentZone = this;
581581
try {
582+
if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) {
583+
task.cancelFn = null;
584+
}
582585
try {
583586
return this._zoneDelegate.invokeTask(this, task, applyThis, applyArgs);
584587
} catch (error) {
@@ -587,9 +590,6 @@ const Zone: ZoneType = (function(global) {
587590
}
588591
}
589592
} finally {
590-
if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) {
591-
task.cancelFn = null;
592-
}
593593
_currentZone = oldZone;
594594
_currentTask = previousTask;
595595
}

test/browser/setTimeout.spec.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,29 @@ describe('setTimeout', function () {
3131
});
3232

3333
it('should allow canceling of fns registered with setTimeout', function (done) {
34-
var spy = jasmine.createSpy('spy');
35-
var cancelId = setTimeout(spy, 0);
36-
clearTimeout(cancelId);
37-
setTimeout(function () {
38-
expect(spy).not.toHaveBeenCalled();
39-
done();
34+
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
35+
testZone.run(() => {
36+
var spy = jasmine.createSpy('spy');
37+
var cancelId = setTimeout(spy, 0);
38+
clearTimeout(cancelId);
39+
setTimeout(function () {
40+
expect(spy).not.toHaveBeenCalled();
41+
done();
42+
});
4043
});
4144
});
4245

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();
46+
it('should allow cancelation of fns registered with setTimeout after invocation', function (done) {
47+
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
48+
testZone.run(() => {
49+
var spy = jasmine.createSpy('spy');
50+
var cancelId = setTimeout(spy, 0);
4851
setTimeout(function () {
49-
clearTimeout(cancelId);
50-
done();
52+
expect(spy).toHaveBeenCalled();
53+
setTimeout(function () {
54+
clearTimeout(cancelId);
55+
done();
56+
});
5157
});
5258
});
5359
});
@@ -59,6 +65,16 @@ describe('setTimeout', function () {
5965
}, 0);
6066
});
6167

68+
it('should allow cancelation of fns registered with setTimeout during invocation', function (done) {
69+
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
70+
testZone.run(() => {
71+
var cancelId = setTimeout(function() {
72+
clearTimeout(cancelId);
73+
done();
74+
}, 0);
75+
});
76+
});
77+
6278
it('should pass invalid values through', function () {
6379
clearTimeout(null);
6480
clearTimeout(<any>{});

0 commit comments

Comments
 (0)