Operation debouncer utility
- Support for return values
- Support for promises
- Support for cancelation
- Flexible API
- Full typescript support
Check out the API docs for more detailed information.
The debounce return value can be reached by waiting the promise.
const result = await debounce.exec(arg1, arg2);
if (result.type === 'ok') {
handleValue(result.value);
}
Async debounce functions are also supported.
const debounce = new Debouncer(async () => 'success', 1_000);
// after 1 second ...
const result = await debounce.exec();
assert.strictEqual(result, { ok: 'ok', value: 'success' });
Also supports all the async utilities.
const debounce = new Debouncer(async () => 'first', 1_500);
// after 1.5 seconds ...
const values = await Promise.all(debounce.exec(), Promise.resolve('second'));
assert.strictEqual(values, [{ type: 'ok', value: 'first' }, 'second']);
Cancelation can be triggered and handled safely.
const value = await debounce.exec(arg1, arg2);
// cancelExecution() ...
assert.strictEqual(result, { ok: 'abort', value: new DebounceAbortError() });
function cancelExecution() {
// Abort pending operation
debounce.abort();
}
In case the return value is already available, its possible to skip the debounce execution.
async function func() {
const value = await debounce.exec(arg1, arg2);
}
debounce.ready(value);
Instead of waiting the delay timeout, run the debounce immediately with flush
.
async function func() {
const value = await debounce.exec(arg1, arg2);
}
function resolveImmediate() {
debounce.flush();
}
Clone the git repo and install with pnpm.
To run the tests, use the package script test
or test --coverage
for code coverage.
See more options in the jest CLI options
To build js files, run build:js
and build:type
for typescript declaration files.
For a full build (.d.ts
, .js
), run the build script.