-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathstore.ts
48 lines (42 loc) · 1.05 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { type OverlayControllerComponent } from './provider';
import { type OverlayReducerAction, overlayReducer } from './reducer';
type OverlayItemId = string;
export type OverlayItem = {
id: OverlayItemId;
isOpen: boolean;
controller: OverlayControllerComponent;
};
export type OverlayState = {
currentId: OverlayItemId | null;
orderIds: OverlayItemId[];
data: Record<OverlayItemId, OverlayItem>;
};
let overlays: OverlayState = {
currentId: null,
orderIds: [],
data: {},
};
let listeners: Array<() => void> = [];
function emitChangeListener() {
for (const listener of listeners) {
listener();
}
}
export function dispatchOverlay(action: OverlayReducerAction) {
overlays = overlayReducer(overlays, action);
emitChangeListener();
}
/**
* @description for useSyncExternalStorage
*/
export const registerOverlayStore = {
subscribe(listener: () => void) {
listeners = [...listeners, listener];
return () => {
listeners = listeners.filter((l) => l !== listener);
};
},
getSnapshot() {
return overlays;
},
};