YuzuJS/setImmediate

clearTimeout should clear timers setted with setImmediate

Yaffle opened this issue · 7 comments

it will be good, if clearTimeout will clear setImmediate timers

of course, replacing native clearTimeout required to implement that.

This would contradict the spec.

really?

currently clearTimeout clear timers, setted with setInterval
also all setTimeout, setImmediate and setInterval returns number and you can see, that this number is identifies timers or interval.

I'll suggest you to use instead of "handle" number, returned by setTimeout:
https://github.com/NobleJS/setImmediate/blob/master/setImmediate.js#L124
+handle = setTimeout(something, 1);
var args = Array.prototype.slice.call(arguments, 1);
var task = { handle: handle, handler: handler, args: args, thisObj: this };
immediates.push(task);
global.postMessage(MESSAGE_NAME, "*");
-return handle++;

Read the spec; there is no interaction between setImmediate and setTimeout. setImmediate is specified to return a number, which in the spec is called a "handle". There is no reason to get that number from a setTimeout call, though, instead of generating it ourselves.

test current browsers implementations and see yourself

spec is not so looking-forward

The only current browser that implements setImmediate is IE10, and in IE10 PP3 the following code:

clearTimeout(msSetImmediate(function () { console.log("hi"); }))

indeed outputs "hi" to the console; clearTimeout does not clear handles returned from msSetImmediate.

IE is not looking-forward too =)

try clearTimeout(setInterval(....)) in Firefox/Chrome/Opera

One of your implementation of setImmediate is based on setTimeout/clearTimeout, so
clearImmediate(id) will kill some timer, althought there are no immediate with this id
even if there are will be immediate
clearImmediate(id); clearImmediate(id) will kill timer too.
Different browser behaviors will make debugging more hard.
Developers mistakes will make debugging hard.

In my opinion current API is redundant (clearTimeout/clearInterval/clearImmediate)
all of set timer function (setTimeout/setInterval/setImmediate) returns "number", so we can't detect
with this number what it means? does it a timer identifer for timer setted by setTimeout or setInterval, or some other number?

You are correct that clearTimeout and clearInterval are interchangable, but this does not impact how clearImmediate should behave; we will be following the spec, since that is the whole point of this repository.

You are also correct that the fallback implementation uses clearTimeout and thus is interchangable; I would consider that a bug and will work to fix it shortly.

And yes, there is generally consensus in the web standards communities that having numbers be the return values from setTimeout and setInterval was a mistake, but unfortunately it's one that's not changable now, and the clearImmediate spec followed that lead.