-
-
Notifications
You must be signed in to change notification settings - Fork 674
/
store.ts
156 lines (131 loc) Β· 3.58 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
Id,
NotValidatedToastProps,
OnChangeCallback,
ToastContainerProps,
ToastContent,
ToastItem,
ToastOptions
} from '../types';
import { Default, canBeRendered, isId } from '../utils';
import {
ContainerObserver,
createContainerObserver
} from './containerObserver';
interface EnqueuedToast {
content: ToastContent<any>;
options: NotValidatedToastProps;
}
interface ClearWaitingQueueParams {
containerId?: Id;
}
interface RemoveParams {
id?: Id;
containerId: Id;
}
const containers = new Map<Id, ContainerObserver>();
let renderQueue: EnqueuedToast[] = [];
const listeners = new Set<OnChangeCallback>();
const dispatchChanges = (data: ToastItem) => listeners.forEach(cb => cb(data));
const hasContainers = () => containers.size > 0;
function flushRenderQueue() {
renderQueue.forEach(v => pushToast(v.content, v.options));
renderQueue = [];
}
export const getToast = (id: Id, { containerId }: ToastOptions) =>
containers.get(containerId || Default.CONTAINER_ID)?.toasts.get(id);
export function isToastActive(id: Id, containerId?: Id) {
if (containerId) return !!containers.get(containerId)?.isToastActive(id);
let isActive = false;
containers.forEach(c => {
if (c.isToastActive(id)) isActive = true;
});
return isActive;
}
export function removeToast(params?: Id | RemoveParams) {
if (!hasContainers()) {
renderQueue = renderQueue.filter(
v => params != null && v.options.toastId !== params
);
return;
}
if (params == null || isId(params)) {
containers.forEach(c => {
c.removeToast(params as Id);
});
} else if (params && ('containerId' in params || 'id' in params)) {
const container = containers.get(params.containerId);
container
? container.removeToast(params.id)
: containers.forEach(c => {
c.removeToast(params.id);
});
}
}
export function clearWaitingQueue(p: ClearWaitingQueueParams = {}) {
containers.forEach(c => {
if (c.props.limit && (!p.containerId || c.id === p.containerId)) {
c.clearQueue();
}
});
}
export function pushToast<TData>(
content: ToastContent<TData>,
options: NotValidatedToastProps
) {
if (!canBeRendered(content)) return;
if (!hasContainers()) renderQueue.push({ content, options });
containers.forEach(c => {
c.buildToast(content, options);
});
}
interface ToggleToastParams {
id?: Id;
containerId?: Id;
}
type RegisterToggleOpts = {
id: Id;
containerId?: Id;
fn: (v: boolean) => void;
};
export function registerToggle(opts: RegisterToggleOpts) {
containers
.get(opts.containerId || Default.CONTAINER_ID)
?.setToggle(opts.id, opts.fn);
}
export function toggleToast(v: boolean, opt?: ToggleToastParams) {
containers.forEach(c => {
if (opt == null || !opt?.containerId) {
c.toggle(v, opt?.id);
} else if (opt?.containerId === c.id) {
c.toggle(v, opt?.id);
}
});
}
export function registerContainer(props: ToastContainerProps) {
const id = props.containerId || Default.CONTAINER_ID;
return {
subscribe(notify: () => void) {
const container = createContainerObserver(id, props, dispatchChanges);
containers.set(id, container);
const unobserve = container.observe(notify);
flushRenderQueue();
return () => {
unobserve();
containers.delete(id);
};
},
setProps(p: ToastContainerProps) {
containers.get(id)?.setProps(p);
},
getSnapshot() {
return containers.get(id)?.getSnapshot();
}
};
}
export function onChange(cb: OnChangeCallback) {
listeners.add(cb);
return () => {
listeners.delete(cb);
};
}