-
Notifications
You must be signed in to change notification settings - Fork 0
/
clicker.ts
45 lines (39 loc) · 1.09 KB
/
clicker.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
import { create } from 'zustand'
import { ServiceApi } from '@dartfrog/puddle';
type ClickMap = Record<string, number>;
export interface ClickerStore {
clickMap: ClickMap;
setClickMap: (newClickMap: ClickMap) => void;
sendClick: (api: ServiceApi) => void;
handleUpdate: (update: any) => void;
get: () => ClickerStore;
set: (partial: ClickerStore | Partial<ClickerStore>) => void;
}
const useClickerStore = create<ClickerStore>((set, get) => ({
clickMap: {},
setClickMap: (newClickMap) => set({ clickMap: newClickMap }),
sendClick: (api: ServiceApi) => {
let req = {
"Clicker": "Click"
};
api.sendToService(req);
},
handleUpdate: (update) => {
if (update && typeof update === 'object') {
if ('ClickMap' in update) {
set({ clickMap: update.ClickMap as ClickMap });
} else if ('ClickMapNode' in update) {
const [ id, count ] = update.ClickMapNode;
set((state) => ({
clickMap: {
...state.clickMap,
[id]: count
}
}));
}
}
},
get,
set,
}));
export default useClickerStore;