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

Commit

Permalink
fix: Canceling already run task should not double decrement task counter
Browse files Browse the repository at this point in the history
Closes #290
  • Loading branch information
mhevery committed Mar 23, 2016
1 parent 9b3e779 commit faa3485
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ function patchTimer(
var clearNative = patchMethod(window, cancelName, (delegate: Function) => function(self: any, args: any[]) {
var task: Task = args[0];
if (task && typeof task.type == 'string') {
task.zone.cancelTask(task);
if (task.cancelFn) {
// Do not cancel already canceled functions
task.zone.cancelTask(task);
}
} else {
// cause an error by calling it directly.
delegate.apply(window, args);
Expand Down
7 changes: 3 additions & 4 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,11 @@ var Zone: ZoneType = (function(global) {
}
}
} finally {
if (task.type == 'macroTask' && task.data && !task.data.isPeriodic) {
task.cancelFn = null;
}
_currentZone = oldZone;
_currentTask = previousTask;
if (task.type == 'microTask') {

}
}
}

Expand Down Expand Up @@ -761,7 +761,6 @@ var Zone: ZoneType = (function(global) {
var prev = counts[type];
var next = counts[type] = prev + count;
if (next < 0) {
debugger;
throw new Error('More tasks executed then were scheduled.');
}
if (prev == 0 || next == 0) {
Expand Down
12 changes: 12 additions & 0 deletions test/browser/setTimeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ describe('setTimeout', function () {
});
});

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();
setTimeout(function () {
clearTimeout(cancelId);
done();
});
});
});

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

0 comments on commit faa3485

Please sign in to comment.