Class: DebounceController
Package:
@litert/concurrentImport path:@litert/concurrentSource: packages/partials/concurrent/src/Classes/DebounceController.tsExtends:EventEmitter<IDebounceControllerEvents>
Wraps a zero-argument function and ensures it is called only once after a quiet period (delayMs) with no new schedule() calls. An optional maxDelayMs cap forces the call even if new schedules keep arriving.
Constructor
new DebounceController(opts: IDebounceOptions)See IDebounceOptions.
Throws:
RangeError— ifdelayMsis not a positive safe integer.RangeError— ifmaxDelayMsis less thandelayMs.
Properties
| Property | Type | Description |
|---|---|---|
delayMs | number (readonly) | Quiet-period delay in milliseconds |
maxDelayMs | number (readonly) | Maximum total delay; defaults to Number.MAX_SAFE_INTEGER |
Methods
schedule()
schedule(): voidSchedules (or re-schedules) the debounced function call. Resets the delayMs timer. If maxDelayMs has already elapsed since the first schedule(), the function is called immediately.
cancel()
cancel(): voidCancels any pending scheduled call. Has no effect if nothing is scheduled.
isScheduled()
isScheduled(): booleanReturns true if a call is currently scheduled.
callNow()
callNow(): voidImmediately cancels any scheduled timer and calls the wrapped function synchronously.
DebounceController.wrap() (static)
static wrap(opts: IDebounceOptions): IDebouncingFunctionCreates a standalone wrapper function. Calling the returned function is equivalent to calling schedule() on a DebounceController instance.
Returns: () => void
Events
See IDebounceControllerEvents.
| Event | Description |
|---|---|
'triggered' | Emitted after the debounced function runs successfully |
'error' | Emitted when the debounced function throws |
Example
import { DebounceController } from '@litert/concurrent';
const dc = new DebounceController({
function: () => console.log('Saved!'),
delayMs: 500,
maxDelayMs: 2000,
});
dc.on('triggered', () => console.log('Function ran'));
input.addEventListener('keyup', () => dc.schedule());
// Or use the static helper for a simpler API
const save = DebounceController.wrap({
function: () => saveDocument(),
delayMs: 300,
});