@akala/core
Version:
48 lines • 1.23 kB
JavaScript
import { Event } from "../events/shared.js";
import { debounce } from "../observables/shared.js";
/**
* Class representing a Debounce.
* @template T
* @implements {ReversibleFormatter<T, Promise<T>>}
* @implements {Disposable}
*/
export class Debounce {
delay;
/**
* Create a Debounce.
* @param {number} delay - The delay in milliseconds.
*/
constructor(delay) {
this.delay = delay;
}
/**
* Dispose of the Debounce.
*/
[Symbol.dispose]() {
if (this.timeout)
clearTimeout(this.timeout);
this.event[Symbol.dispose]();
}
timeout;
event = new Event(100);
/**
* Unformat a value.
* @param {T} value - The value to unformat.
* @returns {Promise<T>} A promise that resolves to the unformatted value.
*/
unformat(value) {
return new Promise(resolve => {
debounce(this.event, this.delay).addListener(resolve, { once: true });
this.event.emit(value);
});
}
/**
* Format a value.
* @param {T} value - The value to format.
* @returns {T} The formatted value.
*/
format(value) {
return value;
}
}
//# sourceMappingURL=debounce.js.map