-
Notifications
You must be signed in to change notification settings - Fork 405
fix: Support clearing the timeouts with numeric IDs #462
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,7 @@ describe('setTimeout', function() { | |
| setTimeout(function() { | ||
| expect(spy).not.toHaveBeenCalled(); | ||
| done(); | ||
| }); | ||
| }, 1); | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -72,7 +72,7 @@ describe('setTimeout', function() { | |
| clearTimeout(cancelId); | ||
| done(); | ||
| }); | ||
| }); | ||
| }, 1); | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -102,6 +102,27 @@ describe('setTimeout', function() { | |
| expect(typeof(cancelId.toString())).toBe('number'); | ||
| }); | ||
|
|
||
| it('should allow cancelation by numeric timeout Id', function (done) { | ||
| // Node returns complex object from setTimeout, ignore this test. | ||
| if (isNode) { | ||
| done(); | ||
| return; | ||
| } | ||
|
|
||
| const testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' }); | ||
| testZone.run(() => { | ||
| const spy = jasmine.createSpy('spy'); | ||
| const task: Task = <any>setTimeout(spy, 0); | ||
| const cancelId: number = <any>task; | ||
| clearTimeout(0 + cancelId); | ||
| setTimeout(function () { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should you set a timeout of ie 10 here ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. Done. Set the second timeout of 1. Please note that the same point also applies to another test in this file as well, see https://github.com/platosha/zone.js/blob/02d0784bb9e9cfb44d2eaa32c12be538370b0a0b/test/common/setTimeout.spec.ts#L57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please also modify the other occurence ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, two places. Modified in a separate commit. |
||
| expect(spy).not.toHaveBeenCalled(); | ||
| expect(task.runCount).toEqual(-1); | ||
| done(); | ||
| }, 1); | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass invalid values through', function() { | ||
| clearTimeout(null); | ||
| clearTimeout(<any>{}); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should
thisbetaskhere ?or
task.invoke(...arguments)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No.
Before the change,
task.invokewas used as a timer callback. Timer callbacks are executed with some other context:thisis aTimeoutinstance in Node.js,windowin browers,undefinedin strict mode.Within this change, we use another function as a timer callback, but in the function we still call
task.invokewith the original timer callback context. As it was called before the change.Note, that the
invokemethod ofZoneTask, which is called here, also passes the context further, so it ends as the original user-provided task callback context. Changing it is not good. Consider:This doesn’t fail any Zone.js tests, though. Seems like there is a lack of tests for the timer callbacks context.