Skip to content

Proposal: declaring rate-limited event handlers (debounce, throttle) #1773

@jelbourn

Description

@jelbourn

It's very common to need to rate-limit DOM events that fire too quickly, such as scroll or mousemove for performance reasons. In the ES5 world, it is very easy to create this kind of handler. For example, if you were using underscore:

<widget (movemove)="checkHoverPosition($event)">
Widget.prototype.checkHoverPosition = _.debounce(function(event) {
  // ...
}, 300);

However, in ES6 / TypeScript, the class syntax does not have a way to define a method based on the output of some function like this. You can still use the ES5 syntax, but that totally kills the hip vibe. Not to mention that this doesn't work for Dart at all.

We could solve this by introducing rate-limiters (or any of kind of similar function) for event handlers into Angular itself with a syntax something like:

<widget (movemove)="checkHoverPosition($event) | debounce:300">
<widget (movemove)="checkHoverPosition($event) | throttle:300">

These rate-limiters are needed by the material components, so it's only a question of whether Angular will support them directly or if the components have to roll our own rate limiting.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions