-
Notifications
You must be signed in to change notification settings - Fork 408
fix: Support clearing the timeouts with numeric IDs #462
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! |
|
@@ -16,6 +16,8 @@ export function patchTimer( | |||
setName += nameSuffix; | |||
cancelName += nameSuffix; | |||
|
|||
var tasksByHandleId: Array<Task> = []; |
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.
This array will grow forever. I think using a hash may be better here.
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.
Done.
if (task && typeof task === 'number') { | ||
var handleId = <number>task; | ||
task = tasksByHandleId[handleId]; | ||
tasksByHandleId[handleId] = undefined; |
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.
also this also has to be cleared when the task expires
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.
Done.
8726c08
to
9991b5c
Compare
@mhevery added the test. |
@@ -19,14 +19,21 @@ export function patchTimer(window: any, setName: string, cancelName: string, nam | |||
setName += nameSuffix; | |||
cancelName += nameSuffix; | |||
|
|||
var tasksByHandleId: Object = {}; |
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.
please use const
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.
Done.
return; | ||
} | ||
|
||
var testZone = Zone.current.fork(Zone['wtfZoneSpec']).fork({ name: 'TestZone' }); |
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.
const, const, const, let instead of var
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.
Done.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The 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
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.
could you please also modify the other occurence ?
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.
Done, two places. Modified in a separate commit.
c23a1b2
to
02d0784
Compare
@@ -60,6 +67,9 @@ export function patchTimer(window: any, setName: string, cancelName: string, nam | |||
clearNative = | |||
patchMethod(window, cancelName, (delegate: Function) => function(self: any, args: any[]) { | |||
var task: Task = args[0]; | |||
if (task && typeof task === 'number') { | |||
task = tasksByHandleId[<number>task]; | |||
} |
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 be equivalent to:
if (typeof task === 'number') {
task = tasksByHandleId[task];
}
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.
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
function scheduleTask(task: Task) { | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this
be task
here ?
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.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.
@@ -60,6 +67,9 @@ export function patchTimer(window: any, setName: string, cancelName: string, nam | |||
clearNative = | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove <number>
should be implied by the typeof
check above
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.
Done.
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