Module Relude_Debounce

type debounced = {
cancel: unit => unit,
flush: unit => unit,
isScheduled: unit => bool,
f: unit => unit,
};

The data returned form the debounce function

let debounce: delayMS:int => ?⁠leading:bool => (unit => unit) => debounced;

debounce takes a unit => unit function, and returns a new unit => unit function which when called, will only invoke the given input function after a period of inactivity.

The leading flag can be used to force one invocation of the function the first time the debounced function is called. If no other calls to the function are made, the function will be invoked again after the delay. TODO: not sure if this is desirable - maybe it should not be invoked again if it was only called once and invoked on the leading edge.

This is useful when waiting for a stream of events to "settle" before invoking some final processing function. Some examples of debouncing might include:

  1. Waiting for a stream of keystrokes to stop before updating a view model, or making an async call.
  2. Waiting for window resize events to settle before using the new screen size to make some expensive computation you don't want to run for every incremental change.

The value returned is a record with the following fields:

  • cancel - a function that can be used to cancel any current-scheduled debounced function invocations
  • flush - a function that can be used to cancel any scheduled invocations and immediately invoke the function
  • isScheduled - a function to check if the debounced function is currently scheduled to run in the future
  • f - the debounced function to be used by the caller