-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstore.ts
More file actions
157 lines (140 loc) · 3.71 KB
/
store.ts
File metadata and controls
157 lines (140 loc) · 3.71 KB
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
157
import { State, Toast, Action, ToastOptions } from '../types';
import { createStore, produce as p } from 'solid-js/store';
const [initOptions, setInitOptions] = createStore<{
options: ToastOptions[];
}>({
options: [],
});
const [store, setStore] = createStore<State>({
toasts: [],
pausedAt: undefined,
});
export const createTimers = () => {
const { pausedAt, toasts } = store;
if (pausedAt) return;
const now = Date.now();
const timers = toasts.map((toast) => {
if (toast.duration === Infinity) return;
const durationLeft = (toast.duration || 0) + toast.pauseDuration - (now - toast.createdAt);
if (durationLeft <= 0) {
if (toast.visible) {
dispatch({
type: 'dismiss',
toastId: toast.id,
});
}
return;
}
return setTimeout(() => {
dispatch({
type: 'dismiss',
toastId: toast.id,
});
}, durationLeft);
});
return timers;
};
const removalQueue = new Map<Toast['id'], ReturnType<typeof setTimeout>>();
const scheduleRemoval = (toastId: string, unmountDelay: number) => {
if (removalQueue.has(toastId)) return;
const timeout = setTimeout(() => {
removalQueue.delete(toastId);
dispatch({
type: 'remove',
toastId,
});
}, unmountDelay);
removalQueue.set(toastId, timeout);
};
const unscheduleRemoval = (toastId: string) => {
const timeout = removalQueue.get(toastId);
removalQueue.delete(toastId);
if (timeout) clearTimeout(timeout);
};
export const dispatch = (action: Action) => {
switch (action.type) {
case 'add':
setStore('toasts', (t) => {
const toasts = t as Toast[];
return [action.toast, ...toasts];
});
break;
case 'dismiss':
const { toastId } = action;
const toasts = store.toasts;
if (toastId) {
const toastToRemove = toasts.find((t) => t.id === toastId);
if (toastToRemove) scheduleRemoval(toastId, toastToRemove.unmountDelay);
setStore(
'toasts',
(t) => t.id === toastId,
p((t) => (t.visible = false))
);
} else {
toasts.forEach((t) => {
scheduleRemoval(t.id, t.unmountDelay);
});
setStore(
'toasts',
(t) => t.id !== undefined,
p((t) => (t.visible = false))
);
}
break;
case 'remove':
if (!action.toastId) {
setStore('toasts', []);
break;
}
setStore('toasts', (t) => {
const toasts = t as Toast[];
return toasts.filter((t) => t.id !== action.toastId);
});
break;
case 'update':
if (action.toast.id) {
unscheduleRemoval(action.toast.id);
}
setStore(
'toasts',
(t) => t.id === action.toast.id,
(t) => {
const toast = t as Toast;
return {
...toast,
...action.toast,
};
}
);
break;
case 'upsert':
store.toasts.find((t) => t.id === action.toast.id)
? dispatch({ type: 'update', toast: action.toast })
: dispatch({ type: 'add', toast: action.toast });
break;
case 'start_pause':
setStore(
p((s) => {
s.pausedAt = Date.now();
s.toasts.forEach((t) => {
t.paused = true;
});
})
);
break;
case 'end_pause':
const pauseInterval = action.time - (store.pausedAt || 0);
setStore(
p((s) => {
s.pausedAt = undefined;
s.toasts.forEach((t) => {
t.pauseDuration += pauseInterval;
t.paused = false;
});
})
);
break;
}
};
export const initOptionsDispatch = (action: Action) => {};
export { store, initOptions };