# Immediate event handlers - need some clarification

**URL:** <https://discourse.elm-lang.org/t/immediate-event-handlers-need-some-clarification/6656>\
**Category:** Learn\
**Created:** [December 21, 2020, 11:47am UTC](https://discourse.elm-lang.org/t/immediate-event-handlers-need-some-clarification/6656 "2020-12-21T11:47:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [December 21, 2020, 11:47am UTC](https://discourse.elm-lang.org/t/immediate-event-handlers-need-some-clarification/6656/1 "2020-12-21T11:47:23Z")

</div>

Hi,

I am trying to do custom handling of ‘copy’ events. This will require calling back into a port, in order to run `document.execCommand('copy')` on the JS side. This must be done immediately in the event handler, in order to know that it occured as the result of user input, and not some malicious code running in the background.

I knew there was stuff added in 0.19, to better support this kind of immediate handling. The documentation for it can be found here:

[https://package.elm-lang.org/packages/elm/virtual-dom/latest/VirtualDom#Handler](https://package.elm-lang.org/packages/elm/virtual-dom/latest/VirtualDom#Handler)

Can someone provide some clarification on Note 2:

> **Note 2:** Some actions, like uploading and downloading files, are only allowed when the JavaScript event loop is running because of user input. This is for security! So when an event occurs, we call `update` and send any `port` messages immediately, all within the same tick of the event loop. This makes it possible to handle user-instigated events in ports.

Does this apply to all event handlers in Elm? Or is it only some of the kinds of handler defined by the `Handler` type? I think `MayPreventDefault` is what I will be using, or possibly `Custom` if I need to.

===

Stackoverflow answer here gives some good detail on custom handling of copy to the clipboard:

> <https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript>

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [December 21, 2020, 3:30pm UTC](https://discourse.elm-lang.org/t/immediate-event-handlers-need-some-clarification/6656/2 "2020-12-21T15:30:56Z")

</div>

There is also the doc comment on `Html.Event.custom` adding to the confusion:

[https://package.elm-lang.org/packages/elm/html/latest/Html-Events#custom](https://package.elm-lang.org/packages/elm/html/latest/Html-Events#custom)

It says:

> **Note:** If you need something even more custom (like capture phase) check out the lower-level event API in `elm/virtual-dom` .

My guess is that all the different ways of attaching event handlers all go to the `VirtualDom.on` function, that all event handling processing is handled synchronously, and that includes port `Cmd`s coming out of the event handling `update`.

---

<div class="post-metadata">

**Author:** ![jreusch](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jreusch](https://discourse.elm-lang.org/u/jreusch)\
**Post date:** [December 21, 2020, 3:38pm UTC](https://discourse.elm-lang.org/t/immediate-event-handlers-need-some-clarification/6656/3 "2020-12-21T15:38:18Z")

</div>

From the Elm side, this applies to all event handlers. Note 3 specifically talks about the `view`, which may happen only after an animation frame. `update` and synchronous commands are always called immediately though:

The [elm-internal event callback](https://github.com/elm/virtual-dom/blob/master/src/Elm/Kernel/VirtualDom.js#L655) calls `currentEventNode` here (an _interesting_ name choice IMO), where `eventNode` is a linked list of all `VirtualDom.map` calls + the final `Process.sendToApp`. sendToApp gets injected by [Platform.initialize](https://github.com/elm/core/blob/master/src/Elm/Kernel/Platform.js#L49).This first calls the `update` function, and then immediately calls `enqueueEffects` on all returned commands, which immediately starts dispatching effects if it isn’t doing that already.The [effect manager for ports](https://github.com/elm/core/blob/master/src/Elm/Kernel/Platform.js#L373) then calls all registered callbacks synchronously.

Elm Kernel code is always “fun” to decipher 😃

I’m not quite sure how all of this interacts with passive event listeners, though. This would be a browser issue then - whether or not passive listeners are still considered to run “after user interaction”.

**Edit** : `elm/html` does not contain any Kernel Code, it just provides some wrapper functions on top of `elm/virtual-dom`, like common nodes, attributes and events. So everything goes through the functions in virtual-dom eventually.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [December 31, 2020, 3:38pm UTC](https://discourse.elm-lang.org/t/immediate-event-handlers-need-some-clarification/6656/4 "2020-12-31T15:38:18Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
