You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 19, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 19, 2018. It is now read-only.
Rather than overload the 3rd argument of addEventListener could we overload the 2nd? #11
This would separate capture from mayCancel but it makes some sense since mayCancel is describing a property of the EventListener. This would have the benefit of being backwards compatible (although we'd still want to encourage use of a polyfill to prevent accidentally depending on cancelable events). This would also avoid the need to change any of the language around the capture variable and what constitutes a redundant (and so ignored) event handler registration. However this seems like it might be an abuse of callback interfaces, is there precedent?
I know callback interfaces have fallen out of favor, but my reading of the WebIDL spec around this suggests that there's nothing really wrong with the above pattern. Maybe it's a reasonable pragmatic trade off to resolve the dispute in #12 in a way everyone can live with? We, of course, can still aim to replace addEventListener in the future to get rid of this legacy long-term. But is there some reason why this design would be really bad? It's certainly not too ugly from the web dev standpoint. Eg:
handleEvent will be part of the event listener paradigm until we replace aEL (note a similar behavior exists for handleChange in MediaQueryListener). So it's seems sensible to me to resolve #12 this way until that time.
So {handleEvent: function(){}} would behave as now, but there could be possibly also passive and capture boolean properties in object passed in?
Or was readonly attribute boolean cancel; on purpose? (I think you mean capture)
And to be concrete, I'm also proposing we'd change the existing boolean capture arg to optional and specify that when it's specified it takes precedence over the capture property here.
Alternately I'd be OK omitting a duplicate capture entirely if people would prefer that (though I know Tab and Anne among others would like some path for improving the syntax for capture).
IMO the ergonomics of using the EventListener callback interface for these options is a huge win. It makes for far more predictable behavior for older browsers. And yeah, as the examples in Rick's comment above shows, the code is far more readable.
The current boolean capture (3rd param) is already optional.
I'd prefer to not add capture to this 2nd param approach. There isn't really any need for that, and it just adds compat risk, similar to the 3rd param approach.
Without capture in 2nd param, I'm ok with this approach. Spec'ing it shouldn't be too hard.
The passive property would be read as if the passed object was a dictionary (so read only once when the addEventListener is called), but otherwise 2nd param would work as it works now.
I don't see how this could work. IDL has very precise semantics for dictionaries and very precise semantics for callback interfaces (which are deprecated). I don't see how this works. Dismissing how this is bound as @paulirish did also seems misguided. That would be an actual backwards incompatible change.
I thought about this a bit and I have these concerns:
This approach is actually backwards incompatible. It's totally legit to pass in your own object as second argument. We are now reducing the number of members you can use there safely. That'll be a continuous hazard going forward as we add more. Worse, we might break actual content, which I think is far worse than the theoretical risk of some site not working until the user updates their browser.
This approach is inconsistent with virtually every other event listener on the platform in that this will not point to the target, ever. That seems like a huge pain when writing reusable code.
This approach makes us store an extra object per listener. That seems suboptimal.
If we restrict this decision to just what to do about passive, this is something we could test concretely. I suspect the risk of existing callback interfaces having a member named passive is pretty low, but we could find out for sure.
But the argument about it not being a good pattern to build on in the future (the way EventListenerOptions could be) is a good one.
This approach is inconsistent with virtually every other event listener on the platform in that this will not point to the target, ever. That seems like a huge pain when writing reusable code.
Good point. So in practice people would probably want to write something ugly like this instead:
And I'm sure developers would often forget about the difference in this and so would have subtle bugs by forgetting to accommodate it when upgrading existing code to add passive.
This approach makes us store an extra object per listener.
Good point. It's common to have thousands of listeners in a frame, so the memory costs are potentially non-trivial. If this were the only potential deal breaker here I could do some work to quantify it a little better.
I've being using the handleEvent syntax quite a bit for animations (to keep state in the listener this and ease of removal), never had a "passive" property in the 2nd argument, but it seems to me that adding an arbitrary field to the object is not much forward looking: when usage spread people will have all sort of custom properties names and adding new features will be harder than is this time. It should then be instead an "handleEventOptions" object property able containing all possible future options. Or you may overload "handleEvent" to accept an object containing the handler and all the options as properties.
That said with everybody using event delegation won't the optimization be impossible as the not passive form of the listener would anyway be registered somewhere up in the DOM? Wouldn't instead be possible to have the passive behaviour be a property of the listener's target element, set via js or css like "-touch-action" is? Or have a completely new set of events like "touchmovePassive" carrying the behaviour?
If you are to change addEventListener itself, than to me a new 4th boolen seems the least weired way to go, when not present it will just be unoptimized, feature detection on something basic as addEventListener looks as frustrating as detecting attachEvent...
Thanks @RByers for pointing out that I was wrong about the WebIDL spec, it does indeed say things about callback interface attributes. Still, the current set of callback interfaces that may end up as widely implemented parts of the web platform are EventListener, NodeFilter, RTCIdentityProvider and XPathNSResolver, and none of these have attributes, yet.
No doubt this could be made to work, but a few things come to mind as not great:
Anyone who currently uses plain functions would have to keep track of the extra object if they later need to call removeEventListener. Some will likely try to use a new object literal instead.
It wouldn't be a proper dictionary, so if we ever come up with an API for easier feature detection of dictionary members, it wouldn't work here.
When it comes to backwards compat, to be fair the current spec also has risk, in that any code that already passes an object as the 3rd argument will see a change in behavior, from capturing to non-capturing. That's why the risk was measured and EventListenerOptions shipped without passive in M49.
Well, no, we'd only need to consider 3 or 4 arguments, since 2 clearly doesn't work. I think we've made it sufficiently clear why 4 arguments is not a good idea either, but we can revisit that again I suppose, in a separate thread.
"clearly doesn't work"? I must be missing such argument. It is perhaps suboptimal approach, sure, but so are others too.
(Currently I'm back in favoring 4th param approach. I was hoping some agreement could be achieved with 2nd param approach and I was ok with that, but looks like I was too optimistic.)
Between the this binding issue, and extra difficulty of removal, the 4th arg option seems strictly superior in terms of developer ergonomics than this 2nd arg option . So unless anyone has any other arguments (@jacobrossi?) I agree we should (again) close this and focus any further debate on just the 4th arg option in #12.
I'm ok with either approach. I prefer the 2nd arg approach as I think the mandated bool in the 3rd arg is more annoying to a dev than learning a different this (it works no differently than handleEvent already does and as @paulirish says, many are already rebinding this anyway) or removing the listener (you can either pass in the same object you gave aEL or just clear handleEvent). But I won't object to the 4th arg approach if there's a consensus that is better.
Based on all the discussion/concerns, it seems the 4th arg option generally better than any sort of 2nd arg option so I'm going to close this issue and continue the discussion #12.
Activity
RByers commentedon Jul 3, 2015
We couldn't do the above as it would be a breaking change due to the definition of single operation callback interfaces, plus we'd need some way to indicate the default value for myCancel. Maybe:
But this still doesn't scale nicely to support other optional options. Perhaps we could use a dictionary with a
handleEventcallback function member?AshleyScirra commentedon Jul 12, 2015
Why not go further and make all parameters passed through a dictionary?
This seems tantamount to a full API redesign though, and probably would deserve a new function name too..
annevk commentedon Jul 12, 2015
Going to close this. Callbacks should be simple functions, the interface callback thing is mostly about compatibility.
RByers commentedon Jul 12, 2015
SGTM, thanks
RByers commentedon Mar 17, 2016
Give the ongoing debate here let's hash this out a little further. Would it really be so bad to have this?
I know callback interfaces have fallen out of favor, but my reading of the WebIDL spec around this suggests that there's nothing really wrong with the above pattern. Maybe it's a reasonable pragmatic trade off to resolve the dispute in #12 in a way everyone can live with? We, of course, can still aim to replace addEventListener in the future to get rid of this legacy long-term. But is there some reason why this design would be really bad? It's certainly not too ugly from the web dev standpoint. Eg:
Seems nicer to me than:
@annevk @domenic @smaug---- @tabatkins @paulirish @jacobrossi @esprehn @grorg @foolip @phistuck @dtapuska
jacobrossi commentedon Mar 17, 2016
+1
handleEvent will be part of the event listener paradigm until we replace aEL (note a similar behavior exists for handleChange in MediaQueryListener). So it's seems sensible to me to resolve #12 this way until that time.
smaug---- commentedon Mar 17, 2016
So {handleEvent: function(){}} would behave as now, but there could be possibly also passive and capture boolean properties in object passed in?
Or was
readonly attribute boolean cancel;on purpose? (I think you mean capture)RByers commentedon Mar 17, 2016
Right
Ooops, thanks - fixed.
RByers commentedon Mar 17, 2016
And to be concrete, I'm also proposing we'd change the existing
boolean capturearg tooptionaland specify that when it's specified it takes precedence over thecaptureproperty here.Alternately I'd be OK omitting a duplicate
captureentirely if people would prefer that (though I know Tab and Anne among others would like some path for improving the syntax for capture).paulirish commentedon Mar 17, 2016
IMO the ergonomics of using the
EventListenercallback interface for these options is a huge win. It makes for far more predictable behavior for older browsers. And yeah, as the examples in Rick's comment above shows, the code is far more readable.smaug---- commentedon Mar 17, 2016
The current boolean capture (3rd param) is already optional.
I'd prefer to not add capture to this 2nd param approach. There isn't really any need for that, and it just adds compat risk, similar to the 3rd param approach.
Without capture in 2nd param, I'm ok with this approach. Spec'ing it shouldn't be too hard.
The passive property would be read as if the passed object was a dictionary (so read only once when the addEventListener is called), but otherwise 2nd param would work as it works now.
annevk commentedon Mar 18, 2016
I don't see how this could work. IDL has very precise semantics for dictionaries and very precise semantics for callback interfaces (which are deprecated). I don't see how this works. Dismissing how
thisis bound as @paulirish did also seems misguided. That would be an actual backwards incompatible change.8 remaining items
annevk commentedon Mar 18, 2016
I thought about this a bit and I have these concerns:
I don't think it's worth pursuing this anymore.
RByers commentedon Mar 18, 2016
If we restrict this decision to just what to do about
passive, this is something we could test concretely. I suspect the risk of existing callback interfaces having a member namedpassiveis pretty low, but we could find out for sure.But the argument about it not being a good pattern to build on in the future (the way EventListenerOptions could be) is a good one.
Good point. So in practice people would probably want to write something ugly like this instead:
And I'm sure developers would often forget about the difference in
thisand so would have subtle bugs by forgetting to accommodate it when upgrading existing code to addpassive.Good point. It's common to have thousands of listeners in a frame, so the memory costs are potentially non-trivial. If this were the only potential deal breaker here I could do some work to quantify it a little better.
paolocaminiti commentedon Mar 18, 2016
I've being using the handleEvent syntax quite a bit for animations (to keep state in the listener this and ease of removal), never had a "passive" property in the 2nd argument, but it seems to me that adding an arbitrary field to the object is not much forward looking: when usage spread people will have all sort of custom properties names and adding new features will be harder than is this time. It should then be instead an "handleEventOptions" object property able containing all possible future options. Or you may overload "handleEvent" to accept an object containing the handler and all the options as properties.
That said with everybody using event delegation won't the optimization be impossible as the not passive form of the listener would anyway be registered somewhere up in the DOM? Wouldn't instead be possible to have the passive behaviour be a property of the listener's target element, set via js or css like "-touch-action" is? Or have a completely new set of events like "touchmovePassive" carrying the behaviour?
If you are to change addEventListener itself, than to me a new 4th boolen seems the least weired way to go, when not present it will just be unoptimized, feature detection on something basic as addEventListener looks as frustrating as detecting attachEvent...
foolip commentedon Mar 21, 2016
Thanks @RByers for pointing out that I was wrong about the WebIDL spec, it does indeed say things about callback interface attributes. Still, the current set of callback interfaces that may end up as widely implemented parts of the web platform are
EventListener,NodeFilter,RTCIdentityProviderandXPathNSResolver, and none of these have attributes, yet.No doubt this could be made to work, but a few things come to mind as not great:
removeEventListener. Some will likely try to use a new object literal instead.When it comes to backwards compat, to be fair the current spec also has risk, in that any code that already passes an object as the 3rd argument will see a change in behavior, from capturing to non-capturing. That's why the risk was measured and EventListenerOptions shipped without passive in M49.
annevk commentedon Mar 21, 2016
Oh god, why was
RTCIdentityProviderintroduced? Bah, we really should have named this legacy callback interface.foolip commentedon Mar 21, 2016
I was also a bid sad to find it in Gecko's source code. File an issue at https://github.com/w3c/webrtc-pc/issues to see if it's too late?
annevk commentedon Mar 21, 2016
Filed w3c/webrtc-pc#559.
annevk commentedon Mar 21, 2016
So with @foolip's two additional points we now have five solid arguments against this approach. Time to close this issue?
smaug---- commentedon Mar 21, 2016
Well, 3rd param approach has clear arguments against it too.
So we're still in state "should we change handling of 2nd or 3rd parameter or add 4th".
annevk commentedon Mar 21, 2016
Well, no, we'd only need to consider 3 or 4 arguments, since 2 clearly doesn't work. I think we've made it sufficiently clear why 4 arguments is not a good idea either, but we can revisit that again I suppose, in a separate thread.
smaug---- commentedon Mar 21, 2016
"clearly doesn't work"? I must be missing such argument. It is perhaps suboptimal approach, sure, but so are others too.
(Currently I'm back in favoring 4th param approach. I was hoping some agreement could be achieved with 2nd param approach and I was ok with that, but looks like I was too optimistic.)
RByers commentedon Mar 21, 2016
Between the
thisbinding issue, and extra difficulty of removal, the 4th arg option seems strictly superior in terms of developer ergonomics than this 2nd arg option . So unless anyone has any other arguments (@jacobrossi?) I agree we should (again) close this and focus any further debate on just the 4th arg option in #12.jacobrossi commentedon Mar 22, 2016
I'm ok with either approach. I prefer the 2nd arg approach as I think the mandated bool in the 3rd arg is more annoying to a dev than learning a different
this(it works no differently thanhandleEventalready does and as @paulirish says, many are already rebindingthisanyway) or removing the listener (you can either pass in the same object you gave aEL or just clearhandleEvent). But I won't object to the 4th arg approach if there's a consensus that is better.RByers commentedon Apr 5, 2016
Based on all the discussion/concerns, it seems the 4th arg option generally better than any sort of 2nd arg option so I'm going to close this issue and continue the discussion #12.