@buzuli/throttle
v2.0.2
Published
Report function throttler for enforcing delays between reports.
Downloads
3,659
Readme
throttle
Controls the frequency at which a report function is called.
Installation
npm install @buzuli/throttle
Usage
const throttle = require('@buzuli/throttle')
const notify = throttle(options)
API
The throttle package exports the function (aliased throttle
). This function accepts an object (aliased options
) and returns a notifier function (aliased notify
).
options
reportFunc
- the report function which will be run at themaxDelay
interval- type is
function
- default is
undefined
- if
undefined
ornull
, it is a no-op not an error - may be replaced at any time using the
notify
function
- type is
minDelay
- reports will never run more frequently than this- type is
number
- units is milliseconds
- default is
1000
- if
undefined
ornull
, the default value is used
- type is
maxDelay
- reports will be forced at this frequency- type is
number
- units is milliseconds
- default is
5000
- if
undefined
, the default value is used - if
null
or<= 0
, then reports will only run when triggered by a notifier event - if
< minDelay
, the value ofminDelay
will be used in its stead
- type is
notify({reportFunc, force, halt})
The notifier function causes the internal notification count to increase, and will cause the report function to be run if minDelay
has been met.
If maxDelay
is > 0
, reports will be forced when it has been >= maxDelay
milliseconds since the last report.
If maxDelay
is null
or <= 0
and the notify function has NOT been called, then reports will not be scheduled.
If maxDelay
is null
or <= 0
and the notify function HAS been called, then a report will either be run (if time since last reports > minDelay
) or will be schedule to run in reportDelay = minDelay - (now - lastReport)
.
If reportFunc
is a function, it will replace the report function for this and all further reports.
If force
is true, the report will be run, even if the time elapsed since the last run is less than minDelay
.
If halt
is true, the next notification will not be scheduled, permitting the process to halt.