Files
t3code/apps/web/src/hooks/threadUndo.ts
T

39 lines
1.1 KiB
TypeScript

// Shared across hook instances so sidebar, header and menu actions invalidate each other.
const currentActions = new Map<string, symbol>();
const listeners = new Set<() => void>();
export function subscribe(listener: () => void) {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
function notify() {
for (const listener of listeners) listener();
}
/** Claims one kind of thread action; a later claim of that kind expires its Undo. */
export function begin(kind: string, threadKey: string) {
const key = JSON.stringify([kind, threadKey]);
const token = Symbol();
currentActions.set(key, token);
notify();
const isCurrent = () => currentActions.get(key) === token;
return {
isCurrent,
finish: () => {
if (isCurrent()) {
currentActions.delete(key);
notify();
}
},
};
}
/** Expires only this action kind, leaving unrelated thread actions intact. */
export function invalidate(kind: string, threadKey: string) {
currentActions.delete(JSON.stringify([kind, threadKey]));
notify();
}