Re: addEventListener and side-effects

On Tue, 6 Sep 2011, Robin Berjon wrote:

> On Sep 6, 2011, at 14:59 , James Graham wrote:
>> On Tue, 6 Sep 2011, Robin Berjon wrote:
>>> as most of you are probably aware, there have been recent threads on both the Geo list[0] and the DAP list[1][2] about calls to addEventListener producing side-effects (or purported side-effects).
>>
>> I am very much against addEventListener having side effects. Where we 
>> have nice simple invariants in the platform it is good to keep them.
>
> Right, but part of the question is whether triggering an event upon 
> registration is indeed a side-effect. Presumably, there is necessarily 
> an effect of event registration (certainly for this type of event), 
> which causes some code to be initialised with a specific intent 
> (dispatching events to a given window about orientation sensors or the 
> level of a battery).

Not at all. Consider the case where one registers multiple event handlers 
for a single event type. Even if the first has some implementation-detail 
side effect like turning on the GPS, later event registations don't. 
Having extra events every time some unrelated piece of code from a 
different library adds an event listener seems wholly undesirable. It is 
much better to keep the simple model where events are fired regardless of 
whether there is a handler listening for them, and let anything else be a 
UA optimisation.

Received on Tuesday, 6 September 2011 16:04:27 UTC