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

fix: Support clearing the timeouts with numeric IDs #462

Closed
wants to merge 2 commits into from

Conversation

platosha
Copy link
Contributor

Fixes #461

@googlebot
Copy link

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!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

1 similar comment
@googlebot
Copy link

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!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@platosha
Copy link
Contributor Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

1 similar comment
@googlebot
Copy link

CLAs look good, thanks!

@mhevery
Copy link
Contributor

mhevery commented Oct 1, 2016

  • You need a test to demonstrate the issue.

@@ -16,6 +16,8 @@ export function patchTimer(
setName += nameSuffix;
cancelName += nameSuffix;

var tasksByHandleId: Array<Task> = [];
Copy link
Contributor

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@platosha
Copy link
Contributor Author

platosha commented Oct 4, 2016

@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 = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use const

Copy link
Contributor Author

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' });
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

const task: Task = <any>setTimeout(spy, 0);
const cancelId: number = <any>task;
clearTimeout(0 + cancelId);
setTimeout(function () {
Copy link
Contributor

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)

Copy link
Contributor Author

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

Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

@@ -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];
}
Copy link
Contributor

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];
        }

Copy link
Contributor Author

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.

function scheduleTask(task: Task) {
const data = <TimerOptions>task.data;
data.args[0] = task.invoke;
data.args[0] = function() {
task.invoke.apply(this, arguments);
Copy link
Contributor

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) ?

Copy link
Contributor Author

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];
Copy link
Contributor

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

Copy link
Contributor Author

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.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants