Module: alfresco/util/functionUtils

alfresco/util/functionUtils

Utility object for function-related utilities. Note that this is not a Class, and so does not need to be instantiated before use.
Author:
  • Martin Doyle
License:
Source:

Members

defaultDebounceMs :number

The default timeout for debounce calls
Type:
  • number
Default Value:
  • 250
Source:

defaultThrottleMs :number

The default timeout for throttle calls
Type:
  • number
Default Value:
  • 250
Source:

Methods

addRepeatingFunction(func, period) → {object}

Register a new repeating function.
Parameters:
Name Type Description
func function The function to be added
period string One of "SHORT" (100ms), "MEDIUM" (1000ms) or "LONG" (10000ms), to determine how often the function is called
Source:
Returns:
An object with a remove method on it, which will de-register this function
Type
object

debounce(args) → {Object}

Debounce the supplied function.

This means that repeated calls to execute a function will be batched up, with only the last submitted function call being executed, and then only after the debounce period has elapsed. A common usage for this is for debouncing keypress events (in a text box) that will ultimately trigger an XHR request.

Parameters:
Name Type Description
args Object The arguments for this function
Properties
Name Type Argument Default Description
name string Debounce calls under different names will not be limited by each other, so this specifies the name for the group of functions to debounce
func function The function to be called after the debounce expires
execFirst bool <optional>
false By default, the last-provided function will execute after a period of inactivity. Setting this to true will execute the first-provided function immediately and then discard any others that occur during the timeout period, with each subsequent function received within that period extending it by the debounce timeout.
timeoutMs int <optional>
The length of the debounce, if different from the default
filter * <optional>
An optional parameter which is used in conjunction with the name to give further context to the debounce: i.e. will debounce only when name AND filter match. The filter can be any value, and will use a strict-equality check to make the comparison.
Source:
Returns:
An object containing a remove() function which will clear any outstanding timeout
Type
Object

throttle(args) → {Object}

Throttle the supplied function.

This means that repeated calls to execute a function will be controlled such that the first call will execute and the last call before the next throttle period will execute, but all others will be discarded.

Example: If the throttle period were 250ms and the function was called at 0ms, 60ms, 120ms, 280ms and 400ms then the first, third and fifth functions would execute at 0ms, 250ms and 500ms respectively, and the second and fourth function calls would be discarded.

Parameters:
Name Type Description
args Object The arguments for this function
Properties
Name Type Argument Default Description
name string Throttle calls under different names will not be limited by each other, so this specifies the name for the group of functions to throttle
func function The function to be called after the throttle expires
ignoreFirst bool <optional>
false If set to true, then do not fire the first-received function call, so in the above example, only the third and fifth functions would be executed.
timeoutMs int <optional>
The length of the throttle, if different from the default
filter * <optional>
An optional parameter which is used in conjunction with the name to give further context to the throttle: i.e. will throttle only when name AND filter match. The filter can be any value, and will use a strict-equality check to make the comparison.
Source:
Returns:
An object containing a remove() function which will clear any outstanding timeout
Type
Object