YuzuJS/setImmediate

MessageChannel

Yaffle opened this issue · 6 comments

Chrome and Opera supports MessageChanel,
seems setImmediate can be implemented with it:

var m = new MessageChannel();
var setZTimeout = function (f) {

m.port1.onmessage = function (x) {
f();
};
m.port2.postMessage('');
}

MessageChannel works form Workers and SharedWorkers too, althout window.postMessage can't work form Workers

Hi Yaffle,

Is there anything that's better about this method than the current implementation, or is it just another alternative that doesn't work in as many browsers?

Hi, DomenicDenicola

  1. you can have many 'message' listeners on window object, so window.postMessage will call all of them.

  2. MessageChannel works form Workers and SharedWorkers.

As far as i know MessageChannel is supported in IE 10 PP 3, Opera, Chrome

  1. Why is this advantageous for implementing setImmediate?

  2. As far as I can see, so does postMessage, although I'd have to use the global postMessage instead of explicitly prefixing with window. I will test to see if that is actually true, but I still do not see any advantage to MessageChannel.

  1. window.postMessage and selft.postMessage (where self is workers global) is different things

Haha yeah, as I found out while implementing the web workers version. Check that out in my fork (linked from your other issue).

MessageChannel is now used when it is available, mainly because it works in a web workers context.