Skip to content

feature request: custom event modifier #3193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skyboyer opened this issue Jul 7, 2019 · 4 comments
Closed

feature request: custom event modifier #3193

skyboyer opened this issue Jul 7, 2019 · 4 comments

Comments

@skyboyer
Copy link

skyboyer commented Jul 7, 2019

By now I have only one example where it could be useful: throttling and debouncing.

<input on:change.throttle200={loadSuggestions}
<input on:change:debounce1000={runSomeHeavyValidation}

Also there is interesting idea on checking keycode in declarative way mentioned in #1088

Currently we can keep throttling/branching into handler but the same as .stopPropagaion it's not part of business logic rather details on how it's integrated. And with declarative way utilizing event modifier it looks more clear and easier to compose.

Does it make sense?

@Rich-Harris
Copy link
Member

This sort of thing can be achieved like so: https://svelte.dev/repl/a2431204cba841a0967900fb15c23e2c?version=3.6.4

<div on:mousemove={throttle(handleMousemove, 200)}>

@skyboyer
Copy link
Author

skyboyer commented Jul 7, 2019

I see. missed that handler may be an expression too.

@qurafi
Copy link
Contributor

qurafi commented Jan 7, 2022

This sort of thing can be achieved like so: https://svelte.dev/repl/a2431204cba841a0967900fb15c23e2c?version=3.6.4

<div on:mousemove={throttle(handleMousemove, 200)}>

Although we can already achieve this but I will still prefer to support passing custom functions. This will allow us to share common logic between multiple handlers without focusing too much into it and also in other situation if we want to support another new official modifier it will not necessarily require a new compiler change.

For example let say you want to use keyboard shortcuts instead of doing ugly parenthesis key.ctrl(key.b(event)) or adding checks in every handler we do instead on:keyup|key.ctrl|key.b. This will give us a lot of flexibility and will produce a cleaner code.

@PierBover
Copy link

PierBover commented Jan 20, 2022

But wouldn't using a function produce an error if the timeout triggers after the component has been unmounted?

In the example Rich posted:

<div on:mousemove={throttle(handleMousemove, 200)}>

What if the component holding this div is unmounted?

If the debouncing was integrated into the Svelte core, it could clear the timeout when the unmount happens.

Another solution is using an action:

export default function (node, {eventName, handler, time}) {
  let timeout;

  function onEvent (event) {
    clearTimeout(timeout);

    timeout = setTimeout(() => {
      handler(event);
    }, time);
  }

  node.addEventListener(eventName, onEvent);

  return {
    destroy () {
      clearTimeout(timeout);
      node.removeEventListener(eventName, onEvent);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants