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

Commit

Permalink
fix(setTimeout): fix for #290, allow clearTimeout to be called in set…
Browse files Browse the repository at this point in the history
…Timeout callback

Closes #301
  • Loading branch information
frankwallis authored and mhevery committed Apr 8, 2016
1 parent e125173 commit a6967ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
6 changes: 3 additions & 3 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ const Zone: ZoneType = (function(global) {
const oldZone = _currentZone;
_currentZone = this;
try {
if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) {
task.cancelFn = null;
}
try {
return this._zoneDelegate.invokeTask(this, task, applyThis, applyArgs);
} catch (error) {
Expand All @@ -587,9 +590,6 @@ const Zone: ZoneType = (function(global) {
}
}
} finally {
if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) {
task.cancelFn = null;
}
_currentZone = oldZone;
_currentTask = previousTask;
}
Expand Down
42 changes: 29 additions & 13 deletions test/browser/setTimeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,29 @@ describe('setTimeout', function () {
});

it('should allow canceling of fns registered with setTimeout', function (done) {
var spy = jasmine.createSpy('spy');
var cancelId = setTimeout(spy, 0);
clearTimeout(cancelId);
setTimeout(function () {
expect(spy).not.toHaveBeenCalled();
done();
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
testZone.run(() => {
var spy = jasmine.createSpy('spy');
var cancelId = setTimeout(spy, 0);
clearTimeout(cancelId);
setTimeout(function () {
expect(spy).not.toHaveBeenCalled();
done();
});
});
});

it('should allow double cancelation of fns registered with setTimeout', function (done) {
var spy = jasmine.createSpy('spy');
var cancelId = setTimeout(spy, 0);
setTimeout(function () {
expect(spy).toHaveBeenCalled();
it('should allow cancelation of fns registered with setTimeout after invocation', function (done) {
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
testZone.run(() => {
var spy = jasmine.createSpy('spy');
var cancelId = setTimeout(spy, 0);
setTimeout(function () {
clearTimeout(cancelId);
done();
expect(spy).toHaveBeenCalled();
setTimeout(function () {
clearTimeout(cancelId);
done();
});
});
});
});
Expand All @@ -59,6 +65,16 @@ describe('setTimeout', function () {
}, 0);
});

it('should allow cancelation of fns registered with setTimeout during invocation', function (done) {
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' });
testZone.run(() => {
var cancelId = setTimeout(function() {
clearTimeout(cancelId);
done();
}, 0);
});
});

it('should pass invalid values through', function () {
clearTimeout(null);
clearTimeout(<any>{});
Expand Down

0 comments on commit a6967ad

Please sign in to comment.