Skip to content
This repository was archived by the owner on Mar 19, 2018. It is now read-only.
This repository was archived by the owner on Mar 19, 2018. It is now read-only.

Is modifying the value of cancelable OK? #2

Closed

Description

@RByers

It's a little weird to say that 'Event.cancelable' changes value in the context of a particular handler. The alternative of an event being uncancelable only when ALL handlers have asked for that seems worse though. I guess we could describe this as the handler getting a mutated copy of the event? But that would be annoying (possibly expensive) to implement and probably impossible to polyfill.

Activity

  1. RByers commented on Jul 3, 2015

    @RByers
    MemberAuthor

    Olli Pettay has argued against this, saying the API should be just a hint - not affecting anything else.

    What do others think? Personally I don't think changing cancelable is that bad - not dissimilar to how currentTarget changes during dispatch.

  2. added a commit that references this issue on Jul 6, 2015
  3. smaug---- commented on Jul 8, 2015

    @smaug----
    Collaborator

    I could just repeat myself. Event.cancelable shouldn't change. The flag passed to addEventListener should be just a hint, and rest of the API should work consistently within every listener call.

  4. RByers commented on Jul 8, 2015

    @RByers
    MemberAuthor

    I could just repeat myself. Event.cancelable shouldn't change. The flag passed to addEventListener should be just a hint, and rest of the API should work consistently within every listener call.

    @smaug---- Thanks, I understand that opinion. But what about the arguments I list in the note for mayCancel:

    This property is valuable for stable composition of handler behavior. It ensures that a mayCancel=false handler which accidentally triggers code that invokes preventDefault behaves consistently regardless of what other handlers happen to be installed for the event. As a result, mayCancel can reliably be adopted incrementally. One component being upgraded to leverage mayCancel=false doesn't risk exposing bugs in other components not necessarily under control of the same author.

    Basically, I'm worried that if we treat this as just a "hint" it'll ultimately be impossible for large sites/framework to incrementally and reliably upgrade to this feature because there's no isolation between components.

  5. smaug---- commented on Jul 8, 2015

    @smaug----
    Collaborator

    Sounds like overengineering to me.

    If you really need to pass different kind of Event to certain listeners, then
    just clone the event, but set .cancelable to false, something hackish like:
    foo.addEventListener("foobarEvent", function (event) {
    var e = new Event(event.type, { ..., cancelable: false});
    realListener(e);
    }, { mayCancel: false} )

  6. RByers commented on Jul 8, 2015

    @RByers
    MemberAuthor

    But the composition issue can't be resolved by the one component looking to do the right thing. I.e. in order to convince the google ads team to set this bit on their touch handlers, I probably need to show why setting this bit isn't going to change behavior of the millions of pages their script is loaded into. If their change can effect the behavior of other handlers on the page (even if those are technically buggy), then they'll be much more resistant to make the change. It doesn't help if they add code like you suggest to their handler (they know their handler is following the rules).

    Also software engineering is all about enabling developers to reason locally. I worry about any API that requires global reasoning "if ALL handlers are ... then ..." as global reasoning is generally impossible in any sufficiently large real-world application that is a mashup of components from various places.

    Maybe I'm being overly paranoid though. @esprehn, @jacobrossi what's your opinion?

  7. smaug---- commented on Jul 8, 2015

    @smaug----
    Collaborator

    Now I'm lost what you want with this bug. You seem to want .cancelable to be per listener (which makes no sense, since .cancelable is a property of an Event, and we aren't changing Event API here), but then you say no, you don't want that after all since ... something about composition.

  8. RByers commented on Jul 8, 2015

    @RByers
    MemberAuthor

    Now I'm lost what you want with this bug. You seem to want .cancelable to be per listener (which makes no sense, since .cancelable is a property of an Event, and we aren't changing Event API here), but then you say no, you don't want that after all since ... something about composition.

    I do want cancelable to be set per listener. I.e. when a given listener promises not to cancel an event, then calls to preventDefault on the event will be completely ignored for the duration of that listener.

    I.e. they can't promise not to cancel and then trigger some behavior change by cancelling. If they could then whether or not their call to preventDefault has any effect would depend on what all the other (well-behaved) listeners for that event happened to do.

    Maybe another way to think about this is as the addition of 3 new phases to the event model. Before the CAPTURE phase there's a UNCANCELABLE-CAPTURE phase, before AT-TARGET there's UNCANCELABLE-AT-TARGET and before BUBBLING there UNCANCELABLE-BUBBLING. Then like the capture option, mayCancel determines what phase the listeners runs at. Listeners run during one of the UNCANCELABLE phases will get an event with cancelable=false. Is that any better? This has dispatch ordering differences from my current proposal, but that might be OK.

    And global reasoning should be totally fine. That is how preventDefault() works anyway.

    This is why scripts that are designed to be used on any site to passively monitor (but not change) behavior (like google analytics) need to be careful to avoid any global actions like preventDefault (they don't have their own UI so can't ever preventDefault an event without risking breaking something else on the page). There is a subset of things passive scripts should be safe to do without having to reason about all the other scripts running on the page. Adding a simple event listener (that doesn't cancel it's events) one of those things that should be completely passive - not changing the behavior of other code on the page.

  9. smaug---- commented on Jul 8, 2015

    @smaug----
    Collaborator

    So if you want per listener .cancelling, why not pass a new instance of Event to such listener? And that behavior can be implemented using script if actually needed.

  10. RByers commented on Jul 8, 2015

    @RByers
    MemberAuthor

    So if you want per listener .cancelling, why not pass a new instance of Event to such listener?

    Yes, we might be able to define it this way. It would have other observable side effects though. Eg. today a handler can stash additional properties onto an Event object and expect other handlers to see those properties. Or JS may create an event object with additional properties and expect all handlers to see those. These 'mayCancel=false' handlers would end up being pretty special in that they'd get a clone of the DOM Event instead of the actual Event. Maybe that would be ok? That design would fit with the idea that these are completely passive handlers which can't mutate the event in any way.

    And that behavior can be implemented using script if actually needed.

    You mean it wouldn't need to be part of the spec? How would one script implement this in a way that affects all scripts on the page? It's of course possible to do this by hooking addEventListener, but that's generally frowned upon (at least outside the context of polyfills) right?

  11. smaug---- commented on Jul 8, 2015

    @smaug----
    Collaborator

    yes, I mean it wouldn't need to be part of the spec. Replacing addEventListener should be totally fine
    (at least as fine as any polyfill) in case someone actually needs it.

    It is that .cancelable is a property of the Event. changing .cancelable per listener feels like a lie: "hey I'm actually cancelable, but not telling that to you"

    Also it becomes rather hairy if some listener stores a pointer to the event and .cancelling has value X at that point, and later, say some function call inside some other listener uses the same event and now the value of .canceling is Y.

    But I need to sleep on with this stuff. Perhaps I'm not so totally against making .canceling
    per listener as I thought.

  12. RByers commented on Jul 8, 2015

    @RByers
    MemberAuthor

    yes, I mean it wouldn't need to be part of the spec. Replacing addEventListener should be totally fine
    (at least as fine as any polyfill) in case someone actually needs it.

    But if it's part of the spec, then browser engines would implement the copying automatically, right? No need for any extra JS on browsers that support the spec. Of course we'd produce a polyfill developers could choose to use on browsers with no support for EventListenerOptions.

    It is that .cancelable is a property of the Event. changing .cancelable per listener feels like a lie: "hey I'm actually cancelable, but not telling that to you"

    But what's so bad about cancelability becoming a mutable property? Maybe we need to define an internal trulyCancelable variable that's immutable and redefine the exposed cancelable property in terms of "is currently cancelable" (implemented as trulyCancelable && currentListener.mayCancel).

    Also it becomes rather hairy if some listener stores a pointer to the event and .cancelling has value X at that point, and later, say some function call inside some other listener uses the same event and now the value of .canceling is Y.

    Yes, I agree. This is a good reason to prefer event cloning over a mutable cancelable. I was thinking this was unusual enough that it wasn't really worth worrying about. But maybe that's wrong.

    But I need to sleep on with this stuff. Perhaps I'm not so totally against making .canceling
    per listener as I thought.

    Ok, cool - thanks a ton for the debate here, it's very helpful! I'm personally OK with the 'send an uncancelable clone' option (although I need to figure out exactly what the implications of event cloning are for custom event types - how exactly do we specify the behavior of cloning?). I could possibly also be convinced that this is all unnecessary and your 'just a hint' approach is good enough (it's certainly simpler). I'd want to get some more input from other folks though (I'm hoping I can get some Google Analytics folks to weigh in here).

  13. dtapuska commented on Jul 10, 2015

    @dtapuska
    Collaborator

    I agree with @smaug---- that the event shouldn't mutate; but we should send a clone of it so if somebody holds a reference we are ok.

    Roughly...

    node.addEventListener('foo', function (event) {
    window.firstEvent = event;
    }, { mayCancel: true});
    node.addEventListener('foo', function (event) {
    // querying window.firstEvent should be the same as it was when first dispatched...
    }, { mayCancel: false});

  14. foolip commented on Jul 10, 2015

    @foolip
    Member

    After reading through this thread I'm seeing the new API as "please call this event listener after all of the normal ones, and at a time when canceling it has no effect". So perhaps one or three new event phases that all come after the existing ones, but not interleaved as in #2 (comment)

    Trying to call preventDefault() in could silently do nothing or throw an exception, and it doesn't really seem important what the value of cancelable is, or why does it matter?

    Does the "delay the event to a time when it can't do any damage" model make sense? We already have events that are synced with animation frames, so if it were possible to listen to events but ask for them to be collected and delivered as a batch at the next animation frame, that might have some of the properties we're looking for.

  15. 12 remaining items

  16. foolip commented on Jul 12, 2015

    @foolip
    Member

    To be clear, in #2 (comment) I'm trying to figure out what the changes to the processing model for { mayCancel: false } would be, never mind EventObserver for now.

    If the listeners aren't invoked after dispatchEvent() has returned, then what is it that makes preventDefault() have no effect, especially in cases where the different kinds of event listeners are interleaved?

    The only other thing that comes to mind is something around the last step of invoke, to restore the canceled flag to its value before running the listener.

  17. foolip commented on Jul 12, 2015

    @foolip
    Member

    I said:

    It seems to me that any solution where a new kind of never-canceling listener is still run in this same target.dispatchEvent(event) call cannot have solved the problem, because then you will have had to make the same problematic thread/process jump.

    I now realize this isn't true. The important thing is that one doesn't have to wait for the result of the event dispatch in the main thread, but this doesn't imply that the order of event listeners in the main thread has to change.

  18. annevk commented on Jul 12, 2015

    @annevk
    Collaborator

    I'm still having a hard time understanding what you're saying. This is how events work today:

    function dispatcher() {
      target.dispatchEvent(ev) // listeners run
      if(!ev.defaultPrevented) {
        defaultAction()
      }
    }

    I don't think we want to change that.

  19. foolip commented on Jul 12, 2015

    @foolip
    Member

    Sorry, I'm rambling, trying to understand what the processing model should actually be. We still need to change something about how events work today, after all, and I don't think cloning the event or changing cancelable during dispatch sounds very nice.

  20. annevk commented on Jul 12, 2015

    @annevk
    Collaborator

    What makes the most sense to me is that we'd change https://dom.spec.whatwg.org/#concept-event-listener-invoke by placing a guard (some kind of "global" flag) around step 6 if this "mayCancel" thing is set. While the guard is set, preventDefault() becomes a no-op. This does require all listeners to opt-in, but that is the only thing that makes sense I think.

  21. foolip commented on Jul 12, 2015

    @foolip
    Member

    Well, how global? If the non-canceling event listener itself fires a cancelable event (e.g. via click()), it'd be weird if that event couldn't be canceled.

  22. annevk commented on Jul 12, 2015

    @annevk
    Collaborator

    Ah yeah, that would be weird. So instead we'd set some flag on the event object and then unset it after invocation.

  23. foolip commented on Jul 12, 2015

    @foolip
    Member

    Yeah, something like the dispatch flag.

    (That'd be like restoring the canceled flag after calling the callback like I speculated above, except one could actually tell that preventDefault() didn't do anything, or it could throw an exception if we'd like that.)

  24. RByers commented on Jul 12, 2015

    @RByers
    MemberAuthor

    Yep, something like this sounds right to me. Thanks!

    @foolip, I think the main difference in our thinking here is that I'm only trying to avoid blocking scroll on JS when there are NO handlers that may block that scroll. Sounds like you'd like to also get scroll going after running the blocking handlers (but before the non-blocking ones), right?

    IMHO, once we have to block on any handler, we've taken most of the perf hit and there's not much we can do. The problems in practice tend not to be about slow handlers themselves, but other long running tasks on the main thread.

  25. foolip commented on Jul 12, 2015

    @foolip
    Member

    It doesn't make sense if unpacked, but I thought that the events would reach the main thread at different times depending on the type of handler, that the non-blocking case would be "asynchronous" and thus arrive "later", which would be a complication. But the only difference is that one doesn't need to wait for the result in one case, so it's all about providing that information.

  26. RByers commented on Jul 13, 2015

    @RByers
    MemberAuthor

    Exactly. I opened #6 to discuss making the implications (or lack thereof) on timing explicit.

  27. RByers commented on Jul 29, 2015

    @RByers
    MemberAuthor

    Let me attempt to summarize the conclusion from this long thread:

    • We won't modify cancelable (contrary to what my spec today suggests)
    • Instead, during invocation of a mayCancel=false handler` we'll set an internal flag on the Event indicating that cancelation should temporarily be ignored.

    I'll work on writing this up as part of #19

  28. RByers commented on Sep 22, 2015

    @RByers
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions