[whatwg] Limit on number of parallel Workers.

On Mon, Jun 22, 2009 at 3:38 PM, ben turner<bent.mozilla at gmail.com> wrote:
>> http://figushki.com/test/workers/workers.html
>
> I just wanted to point out that this example illustrates a bug in
> Safari/Chromium's implementation of workers (I think?). The example
> worker script ('fib.js') does this:
>
> ?function onmessage(evt) { ... }
> ?addEventListener("message", onmessage, true);
>
> In Firefox this has the effect of registering the message listener
> *twice* since 'onmessage' is an attribute on the worker global scope
> object and is handled independently of
> addEventListener/removeEventListener functions. This behavior matches
> that of normal web pages, as well. Given this snippet, for example,
> Firefox will alert twice on page load:
>
> ?function onload() { alert("load!"); }
> ?addEventListener("load", onload, false);
>
> Webkit seems to behave strangely here... The above code only alerts
> once, yet the following code alerts twice:
>
> ?onload = function(evt) { alert("load!"); }
> ?addEventListener("load", onload, false);
>
> Does that make sense? Firefox treats both cases the same and alerts
> twice always.
>
> Anyway, regardless of what happens here, the worker example linked
> above will run the fibonacci calculation twice per worker in Firefox.

So for what it's worth, it actually seems undefined what the right
thing to do here is. In ECMAScript 3 there is a difference between

onload = function (...) { ...}
and
function onload(...) { ...}

In that the latter replaces any existing setters, whereas the former
runs any existing setters. However, apparently in ECMAScript 5, the
two are the same in that both run any existing setter.

In any case, I'd recommend changing the example to simply use

onmessage = function (evt) { ... }

and remove the call to addEventListener. That seems like it
unambiguously should work, and results in less code.

/ Jonas

Received on Monday, 22 June 2009 15:52:08 UTC