Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadUndo.ts

21 lines770 Bytes

1// Shared across hook instances so sidebar, header and menu actions invalidate each other.
2const currentActions = new Map<string, symbol>();
3​
4/** Claims one kind of thread action; a later claim of that kind expires its Undo. */
5export function begin(kind: string, threadKey: string) {
6 const key = JSON.stringify([kind, threadKey]);
7 const token = Symbol();
8 currentActions.set(key, token);
9 const isCurrent = () => currentActions.get(key) === token;
10 return {
11 isCurrent,
12 finish: () => {
13 if (isCurrent()) currentActions.delete(key);
14 },
15 };
17​
18/** Expires only this action kind, leaving unrelated thread actions intact. */
19export function invalidate(kind: string, threadKey: string) {
20 currentActions.delete(JSON.stringify([kind, threadKey]));