-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
42 lines (38 loc) · 782 Bytes
/
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
import logger from '@coaction/logger';
import { create, type Slices, AsyncStore, SliceState } from 'coaction';
export type Counter = Slices<
{
counter: {
count: number;
increment: () => void;
};
},
'counter'
>;
const worker = globalThis.SharedWorker
? new SharedWorker(new URL('./store.ts', import.meta.url), {
type: 'module'
})
: undefined;
const store = create<{
counter: Counter;
}>(
{
counter: (set) => ({
count: 0,
increment() {
set((draft) => {
draft.counter.count += 1;
});
}
})
},
{
...(worker ? { worker } : {}),
middlewares: [logger({ collapsed: false })]
}
);
export const useWorkerStore = store as AsyncStore<
SliceState<{ counter: Counter }>,
true
>;