@figliolia/react-hooks
Version:
A small collection of simple React Hooks you're probably rewriting on a regular basis
34 lines (33 loc) • 811 B
JavaScript
export class FloatingTaskManager {
schedule;
tokens = new Set();
cancel;
constructor(schedule, cancel) {
this.schedule = schedule.bind(globalThis);
this.cancel = cancel.bind(globalThis);
}
execute(...args) {
const [fn, ...rest] = args;
const token = this.schedule(
// @ts-ignore
(...params) => {
fn(...params);
}, ...rest);
this.tokens.add(token);
return () => {
this.clear(token);
};
}
abortAll() {
for (const token of this.tokens) {
this.cancel(token);
}
this.tokens.clear();
}
clear(token) {
if (token && this.tokens.has(token)) {
this.cancel(token);
this.tokens.delete(token);
}
}
}