Open History to see the last commit
1// Shared across hook instances so sidebar, header and menu actions invalidate each other.
2const currentActions = new Map<string, symbol>();
3const listeners = new Set<() => void>();
4
5export function subscribe(listener: () => void) {
6 listeners.add(listener);
7 return () => {
8 listeners.delete(listener);
9 };
10}
11
12function notify() {
13 for (const listener of listeners) listener();
14}
15
16export function begin(kind: string, threadKey: string) {
17 const key = JSON.stringify([kind, threadKey]);
18 const token = Symbol();
19 currentActions.set(key, token);
20 notify();
21 const isCurrent = () => currentActions.get(key) === token;
22 return {
23 isCurrent,
24 finish: () => {
25 if (isCurrent()) {
26 currentActions.delete(key);
27 notify();
28 }
29 },
30 };
31}
32
33export function invalidate(kind: string, threadKey: string) {
34 currentActions.delete(JSON.stringify([kind, threadKey]));
35 notify();
36}