|
1 | 1 | import { enableMapSet, immerable } from 'immer'
|
2 | 2 | import { create } from 'zustand'
|
3 |
| -import { persist, subscribeWithSelector } from 'zustand/middleware' |
| 3 | +import { subscribeWithSelector } from 'zustand/middleware' |
4 | 4 | import { immer } from 'zustand/middleware/immer'
|
5 | 5 |
|
6 | 6 | type Id = string
|
@@ -39,98 +39,99 @@ export const createCollection = <T extends { id: Id }, A extends object>(
|
39 | 39 | data[immerable] = true
|
40 | 40 |
|
41 | 41 | return create(
|
42 |
| - persist( |
43 |
| - immer( |
44 |
| - // @ts-ignore |
45 |
| - subscribeWithSelector<BaseStore<T> & A>((set: Setter<T, A>, get) => ({ |
46 |
| - data, |
47 |
| - |
48 |
| - ...(typeof actions === 'function' ? actions(set, get) : actions), |
49 |
| - |
50 |
| - softDelete(key) { |
51 |
| - const data = get().data.get(key) |
52 |
| - if (!data) { |
53 |
| - return false |
54 |
| - } |
55 |
| - |
56 |
| - set((state) => { |
57 |
| - const data = state.data.get(key) |
58 |
| - if (data) data.isDeleted = true |
59 |
| - }) |
60 |
| - |
61 |
| - return true |
62 |
| - }, |
63 |
| - add(...args: any[]) { |
64 |
| - const addFn = get().add |
65 |
| - |
66 |
| - const add = (id: string, data: T | T[]) => { |
67 |
| - if (Array.isArray(data)) { |
68 |
| - data.forEach((d) => { |
69 |
| - addFn(d) |
70 |
| - }) |
71 |
| - |
72 |
| - return |
73 |
| - } |
74 |
| - |
75 |
| - set((state) => { |
76 |
| - state.data.set(id, { ...data }) |
77 |
| - }) |
78 |
| - } |
| 42 | + // persist( |
| 43 | + immer( |
| 44 | + // @ts-ignore |
| 45 | + subscribeWithSelector<BaseStore<T> & A>((set: Setter<T, A>, get) => ({ |
| 46 | + data, |
| 47 | + |
| 48 | + ...(typeof actions === 'function' ? actions(set, get) : actions), |
| 49 | + |
| 50 | + softDelete(key) { |
| 51 | + const data = get().data.get(key) |
| 52 | + if (!data) { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + set((state) => { |
| 57 | + const data = state.data.get(key) |
| 58 | + if (data) data.isDeleted = true |
| 59 | + }) |
| 60 | + |
| 61 | + return true |
| 62 | + }, |
| 63 | + add(...args: any[]) { |
| 64 | + const addFn = get().add |
79 | 65 |
|
80 |
| - if (typeof args[0] === 'string') { |
81 |
| - const id = args[0] |
82 |
| - const data = args[1] |
83 |
| - add(id, data) |
84 |
| - } else { |
85 |
| - const data = args[0] |
86 |
| - add(data.id, data) |
87 |
| - } |
88 |
| - }, |
89 |
| - addAndPatch(data: T | T[]) { |
| 66 | + const add = (id: string, data: T | T[]) => { |
90 | 67 | if (Array.isArray(data)) {
|
91 |
| - const patch = get().addAndPatch |
92 | 68 | data.forEach((d) => {
|
93 |
| - patch(d) |
| 69 | + addFn(d) |
94 | 70 | })
|
| 71 | + |
95 | 72 | return
|
96 | 73 | }
|
| 74 | + |
97 | 75 | set((state) => {
|
98 |
| - const collection = state.data |
99 |
| - if (collection.has(data.id)) { |
100 |
| - const exist = collection.get(data.id) |
101 |
| - |
102 |
| - collection.set(data.id, { ...exist, ...data }) |
103 |
| - } else { |
104 |
| - collection.set(data.id, data) |
105 |
| - } |
| 76 | + state.data.set(id, { ...data }) |
106 | 77 | })
|
107 |
| - }, |
108 |
| - remove(id: Id) { |
109 |
| - set((state) => { |
110 |
| - state.data.delete(id) |
| 78 | + } |
| 79 | + |
| 80 | + if (typeof args[0] === 'string') { |
| 81 | + const id = args[0] |
| 82 | + const data = args[1] |
| 83 | + add(id, data) |
| 84 | + } else { |
| 85 | + const data = args[0] |
| 86 | + add(data.id, data) |
| 87 | + } |
| 88 | + }, |
| 89 | + addAndPatch(data: T | T[]) { |
| 90 | + if (Array.isArray(data)) { |
| 91 | + const patch = get().addAndPatch |
| 92 | + data.forEach((d) => { |
| 93 | + patch(d) |
111 | 94 | })
|
112 |
| - }, |
113 |
| - })), |
114 |
| - ), |
115 |
| - { |
116 |
| - name, |
117 |
| - // serialize: (data) => { |
118 |
| - // return JSON.stringify({ |
119 |
| - // ...data, |
120 |
| - // state: { |
121 |
| - // ...data.state, |
122 |
| - // data: Array.from(data.state.data as Set<unknown>), |
123 |
| - // }, |
124 |
| - // }) |
125 |
| - // }, |
126 |
| - deserialize: (value) => { |
127 |
| - const data = JSON.parse(value) |
128 |
| - |
129 |
| - data.state.data = new Map(Object.entries(data.state.data)) |
130 |
| - |
131 |
| - return data |
| 95 | + return |
| 96 | + } |
| 97 | + set((state) => { |
| 98 | + const collection = state.data |
| 99 | + if (collection.has(data.id)) { |
| 100 | + const exist = collection.get(data.id) |
| 101 | + |
| 102 | + collection.set(data.id, { ...exist, ...data }) |
| 103 | + } else { |
| 104 | + collection.set(data.id, data) |
| 105 | + } |
| 106 | + }) |
| 107 | + }, |
| 108 | + remove(id: Id) { |
| 109 | + set((state) => { |
| 110 | + state.data.delete(id) |
| 111 | + }) |
132 | 112 | },
|
133 |
| - }, |
| 113 | + })), |
134 | 114 | ),
|
| 115 | + // { |
| 116 | + // name, |
| 117 | + // storage: {} |
| 118 | + // serialize: (data) => { |
| 119 | + // return JSON.stringify({ |
| 120 | + // ...data, |
| 121 | + // state: { |
| 122 | + // ...data.state, |
| 123 | + // data: Array.from(data.state.data as Set<unknown>), |
| 124 | + // }, |
| 125 | + // }) |
| 126 | + // }, |
| 127 | + // deserialize: (value) => { |
| 128 | + // const data = JSON.parse(value) |
| 129 | + |
| 130 | + // data.state.data = new Map(Object.entries(data.state.data)) |
| 131 | + |
| 132 | + // return data |
| 133 | + // }, |
| 134 | + // }, |
| 135 | + // ), |
135 | 136 | )
|
136 | 137 | }
|
0 commit comments