Skip to content

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

ts
new DebounceController(opts: IDebounceOptions)

See IDebounceOptions.

Throws:

  • RangeError — if delayMs is not a positive safe integer.
  • RangeError — if maxDelayMs is less than delayMs.

Properties

PropertyTypeDescription
delayMsnumber (readonly)Quiet-period delay in milliseconds
maxDelayMsnumber (readonly)Maximum total delay; defaults to Number.MAX_SAFE_INTEGER

Methods

schedule()

ts
schedule(): void

Schedules (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()

ts
cancel(): void

Cancels any pending scheduled call. Has no effect if nothing is scheduled.


isScheduled()

ts
isScheduled(): boolean

Returns true if a call is currently scheduled.


callNow()

ts
callNow(): void

Immediately cancels any scheduled timer and calls the wrapped function synchronously.


DebounceController.wrap() (static)

ts
static wrap(opts: IDebounceOptions): IDebouncingFunction

Creates a standalone wrapper function. Calling the returned function is equivalent to calling schedule() on a DebounceController instance.

Returns: () => void


Events

See IDebounceControllerEvents.

EventDescription
'triggered'Emitted after the debounced function runs successfully
'error'Emitted when the debounced function throws

Example

ts
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,
});