The debounce delay in milliseconds, or a function that returns it. When debounce used as a method decorator, the function is passed the object instance as its first (only) parameter.
Optionalopts: DebounceOptions{
edge?: 'trailing' | 'leading' // default: 'trailing'
sequence?: 'serial' | 'concurrent' // default: 'serial'
}
'edge Specifies when the debounced function is invoked:
'leading' - immediately at the first call, using its parameters.'trailing' - after the debounce window timeout, using the parameters of the last call.sequence determines behavior if the function is still executing when a fresh call is made:
'serial' - the window is kept open until the function finishes executing; the new call returns the same Promise as the ongoing call.'concurrent' - allow overlapping execution; a new independent debounce window is opened, while the ongoing call continues to execute. import { debounce } from '@thingts/execution'
// Basic functional form:
const saveOnce = debounce(200)(async function saveData(...) { ... })
// With a debouncer object
const debouncer = debounce(200)
const onClickSave = debouncer(function handleSave(...) { ... })
const onClickLoad = debouncer(function handleLoad(...) { ... })
// As a method decorator
class SearchBox {
@debounce(300, { edge: 'leading' })
async onInputChange(...) { ... }
}
// With dynamic delay
class AutoSaver {
@debounce((self: AutoSaver) => self.getSaveDelay())
async autoSave(...) { ... }
}
Creates a debouncer that wraps a given function
fnto produce a debounced version. The debounced version ensures that when invoked multiple times in a burst close together in time, only a single call tofnis made.Debouncing works by creating a window that closes when no calls have been made for
delaymilliseconds. The original function is invoked only once during that window, either at the start or end of the window.The debounced function always returns a Promise. All calls made within the debounce window return the exact same Promise, that resolves (or rejects) with the value returned (or error thrown) by the original function when it is invoked and awaited.
In functional form, if the same debouncer is applied to multiple functions, each function is debounced independently.
In method decorator form, calls to the method on different instances are debounced independently.