fix: Support clearing the timeouts with numeric IDs#462
fix: Support clearing the timeouts with numeric IDs#462platosha wants to merge 2 commits intoangular:masterfrom
Conversation
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
1 similar comment
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
|
I signed it! |
|
CLAs look good, thanks! |
1 similar comment
|
CLAs look good, thanks! |
|
| setName += nameSuffix; | ||
| cancelName += nameSuffix; | ||
|
|
||
| var tasksByHandleId: Array<Task> = []; |
There was a problem hiding this comment.
This array will grow forever. I think using a hash may be better here.
| if (task && typeof task === 'number') { | ||
| var handleId = <number>task; | ||
| task = tasksByHandleId[handleId]; | ||
| tasksByHandleId[handleId] = undefined; |
There was a problem hiding this comment.
also this also has to be cleared when the task expires
8726c08 to
9991b5c
Compare
|
@mhevery added the test. |
| setName += nameSuffix; | ||
| cancelName += nameSuffix; | ||
|
|
||
| var tasksByHandleId: Object = {}; |
| return; | ||
| } | ||
|
|
||
| var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' }); |
There was a problem hiding this comment.
const, const, const, let instead of var
9991b5c to
c23a1b2
Compare
| const task: Task = <any>setTimeout(spy, 0); | ||
| const cancelId: number = <any>task; | ||
| clearTimeout(0 + cancelId); | ||
| setTimeout(function () { |
There was a problem hiding this comment.
should you set a timeout of ie 10 here ?
I don't think there is any guarantee on the order for the same timeout value (even if most impl should work as expected)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
could you please also modify the other occurence ?
There was a problem hiding this comment.
Done, two places. Modified in a separate commit.
c23a1b2 to
02d0784
Compare
| var task: Task = args[0]; | ||
| if (task && typeof task === 'number') { | ||
| task = tasksByHandleId[<number>task]; | ||
| } |
There was a problem hiding this comment.
should be equivalent to:
if (typeof task === 'number') {
task = tasksByHandleId[task];
}
There was a problem hiding this comment.
Done.
Can’t drop the <number> type casting in the tasksByHandleId[<number>task] index expression, though, the TypeScript compiler gives error.
02d0784 to
5fcb1d0
Compare
| const data = <TimerOptions>task.data; | ||
| data.args[0] = task.invoke; | ||
| data.args[0] = function() { | ||
| task.invoke.apply(this, arguments); |
There was a problem hiding this comment.
should this be task here ?
or task.invoke(...arguments) ?
There was a problem hiding this comment.
No.
Before the change, task.invoke was used as a timer callback. Timer callbacks are executed with some other context: this is a Timeout instance in Node.js, window in browers, undefined in strict mode.
Within this change, we use another function as a timer callback, but in the function we still call task.invoke with the original timer callback context. As it was called before the change.
Note, that the invoke method of ZoneTask, 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:
setTimeout(function() {
console.log(this);
// should log `window` in browsers, not a `ZoneTask` instance or anything else
});This doesn’t fail any Zone.js tests, though. Seems like there is a lack of tests for the timer callbacks context.
| patchMethod(window, cancelName, (delegate: Function) => function(self: any, args: any[]) { | ||
| var task: Task = args[0]; | ||
| if (typeof task === 'number') { | ||
| task = tasksByHandleId[<number>task]; |
There was a problem hiding this comment.
remove <number> should be implied by the typeof check above
Some of the setTimeout tests use multiple timers and expect them to execute in a particular order. Use explicit delay in those places to reflect and guarantee the execution order.
5fcb1d0 to
3dd4f06
Compare
Fixes #461