@chhsiao1981/use-thunk 10.0.0 → 10.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +7 -115
- package/package.json +1 -1
- package/src/ThunkContext.tsx +10 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -176,23 +176,13 @@ The general concept of normalized state can be found in [Normalizing State Shape
|
|
|
176
176
|
with the following features:
|
|
177
177
|
|
|
178
178
|
1. ClassState: the state of the class, including the nodes and the root of the class.
|
|
179
|
-
2. NodeState: the state of a node, including the id
|
|
179
|
+
2. NodeState: the state of a node, including the id of the node and the content (state) of the node.
|
|
180
180
|
3. State: the content of the node, represented as a state.
|
|
181
|
-
4. The concept of "parent" and "children" and "links" is embedded in the NodeState.
|
|
182
|
-
* remove (me):
|
|
183
|
-
- initiate "remove" for all the children.
|
|
184
|
-
- remove from the parent.
|
|
185
|
-
- remove from all the links.
|
|
186
|
-
* remove child:
|
|
187
|
-
- the child initiate "remove".
|
|
188
|
-
* remove link:
|
|
189
|
-
- the link initiate "remove link" on me.
|
|
190
|
-
4. To avoid complication, currently there is only 1 parent.
|
|
191
181
|
|
|
192
182
|
For example, the example [in the redux link](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape) is represented as:
|
|
193
183
|
|
|
194
184
|
```ts
|
|
195
|
-
|
|
185
|
+
classStatePost = {
|
|
196
186
|
myClass: 'post',
|
|
197
187
|
doMe: (DispatchedAction<Post>),
|
|
198
188
|
nodes: {
|
|
@@ -201,33 +191,15 @@ statePost = {
|
|
|
201
191
|
state: {
|
|
202
192
|
author : uuid-user1,
|
|
203
193
|
body : "......",
|
|
194
|
+
comments: [uuid-comment1, uuid-comment2]
|
|
204
195
|
},
|
|
205
|
-
_parent: {
|
|
206
|
-
id: uuid-user1,
|
|
207
|
-
do: doUser
|
|
208
|
-
},
|
|
209
|
-
_links: {
|
|
210
|
-
comment : {
|
|
211
|
-
list: [uuid-comment1, uuid-comment2],
|
|
212
|
-
do: doComment
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
196
|
},
|
|
216
197
|
[uuid-post2] : {
|
|
217
198
|
id : uuid-post2,
|
|
218
199
|
state: {
|
|
219
200
|
author : uuid-user2,
|
|
220
201
|
body : "......",
|
|
221
|
-
|
|
222
|
-
_parent: {
|
|
223
|
-
id: uuid-user2,
|
|
224
|
-
do: doUser
|
|
225
|
-
},
|
|
226
|
-
_links: {
|
|
227
|
-
comment : {
|
|
228
|
-
list: [uuid-comment3, uuid-comment4, uuid-comment5],
|
|
229
|
-
do: doComment
|
|
230
|
-
}
|
|
202
|
+
comments: [uuid-comment3, uuid-comment4, uuid-comment5]
|
|
231
203
|
}
|
|
232
204
|
}
|
|
233
205
|
}
|
|
@@ -237,7 +209,7 @@ statePost = {
|
|
|
237
209
|
and:
|
|
238
210
|
|
|
239
211
|
```ts
|
|
240
|
-
|
|
212
|
+
classStateComment = {
|
|
241
213
|
myClass: 'comment',
|
|
242
214
|
doMe: (DispatchedAction<Comment>),
|
|
243
215
|
nodes: {
|
|
@@ -246,16 +218,6 @@ stateComment = {
|
|
|
246
218
|
state: {
|
|
247
219
|
author : uuid-user2,
|
|
248
220
|
comment : ".....",
|
|
249
|
-
},
|
|
250
|
-
_parent: {
|
|
251
|
-
id: uuid-user2,
|
|
252
|
-
do: doUser
|
|
253
|
-
},
|
|
254
|
-
_links: {
|
|
255
|
-
post: {
|
|
256
|
-
list: [uuid-post1],
|
|
257
|
-
do: doPost
|
|
258
|
-
}
|
|
259
221
|
}
|
|
260
222
|
},
|
|
261
223
|
[uuid-comment2] : {
|
|
@@ -263,16 +225,6 @@ stateComment = {
|
|
|
263
225
|
state: {
|
|
264
226
|
author : uuid-user3,
|
|
265
227
|
comment : ".....",
|
|
266
|
-
},
|
|
267
|
-
_parent: {
|
|
268
|
-
id: uuid-user3,
|
|
269
|
-
do: doUser
|
|
270
|
-
},
|
|
271
|
-
_links: {
|
|
272
|
-
post: {
|
|
273
|
-
list: [uuid-post1],
|
|
274
|
-
do: doPost
|
|
275
|
-
}
|
|
276
228
|
}
|
|
277
229
|
},
|
|
278
230
|
[uuid-comment3] : {
|
|
@@ -280,16 +232,6 @@ stateComment = {
|
|
|
280
232
|
state: {
|
|
281
233
|
author : uuid-user3,
|
|
282
234
|
comment : ".....",
|
|
283
|
-
},
|
|
284
|
-
_parent: {
|
|
285
|
-
id: uuid-user3,
|
|
286
|
-
do: doUser
|
|
287
|
-
},
|
|
288
|
-
_links: {
|
|
289
|
-
post: {
|
|
290
|
-
list: [uuid-post2],
|
|
291
|
-
do: doPost
|
|
292
|
-
}
|
|
293
235
|
}
|
|
294
236
|
},
|
|
295
237
|
[uuid-comment4] : {
|
|
@@ -297,16 +239,6 @@ stateComment = {
|
|
|
297
239
|
state: {
|
|
298
240
|
author : uuid-user1,
|
|
299
241
|
comment : ".....",
|
|
300
|
-
},
|
|
301
|
-
_parent: {
|
|
302
|
-
id: uuid-user1,
|
|
303
|
-
do: doUser
|
|
304
|
-
},
|
|
305
|
-
_links: {
|
|
306
|
-
post: {
|
|
307
|
-
list: [uuid-post2],
|
|
308
|
-
do: doPost
|
|
309
|
-
}
|
|
310
242
|
}
|
|
311
243
|
},
|
|
312
244
|
[uuid-comment5] : {
|
|
@@ -314,25 +246,15 @@ stateComment = {
|
|
|
314
246
|
state: {
|
|
315
247
|
author : uuid-user3,
|
|
316
248
|
comment : ".....",
|
|
317
|
-
},
|
|
318
|
-
_parent: {
|
|
319
|
-
id: uuid-user3,
|
|
320
|
-
do: doUser
|
|
321
|
-
},
|
|
322
|
-
_links: {
|
|
323
|
-
post: {
|
|
324
|
-
list: [uuid-post2],
|
|
325
|
-
do: doPost
|
|
326
|
-
}
|
|
327
249
|
}
|
|
328
|
-
}
|
|
250
|
+
}
|
|
329
251
|
}
|
|
330
252
|
}
|
|
331
253
|
```
|
|
332
254
|
|
|
333
255
|
and:
|
|
334
256
|
```ts
|
|
335
|
-
|
|
257
|
+
classStateUser = {
|
|
336
258
|
myClass: 'user',
|
|
337
259
|
doMe: (DispatchedAction<User>),
|
|
338
260
|
nodes: {
|
|
@@ -341,16 +263,6 @@ stateUser = {
|
|
|
341
263
|
state: {
|
|
342
264
|
username : "user1",
|
|
343
265
|
name : "User 1",
|
|
344
|
-
},
|
|
345
|
-
_children: {
|
|
346
|
-
post: {
|
|
347
|
-
list: [uuid-post1],
|
|
348
|
-
do: doPost,
|
|
349
|
-
},
|
|
350
|
-
comment: {
|
|
351
|
-
list: [uuid-comment4],
|
|
352
|
-
do: doComment,
|
|
353
|
-
}
|
|
354
266
|
}
|
|
355
267
|
},
|
|
356
268
|
[uuid-user2] : {
|
|
@@ -358,16 +270,6 @@ stateUser = {
|
|
|
358
270
|
state: {
|
|
359
271
|
username : "user2",
|
|
360
272
|
name : "User 2",
|
|
361
|
-
},
|
|
362
|
-
_children: {
|
|
363
|
-
post: {
|
|
364
|
-
list: [uuid-post2],
|
|
365
|
-
do: doPost,
|
|
366
|
-
},
|
|
367
|
-
comment: {
|
|
368
|
-
list: [uuid-comment1],
|
|
369
|
-
do: doComment,
|
|
370
|
-
}
|
|
371
273
|
}
|
|
372
274
|
},
|
|
373
275
|
[uuid-user3] : {
|
|
@@ -375,16 +277,6 @@ stateUser = {
|
|
|
375
277
|
state: {
|
|
376
278
|
username : "user3",
|
|
377
279
|
name : "User 3",
|
|
378
|
-
},
|
|
379
|
-
_children: {
|
|
380
|
-
post: {
|
|
381
|
-
list: [uuid-post1],
|
|
382
|
-
do: doPost,
|
|
383
|
-
},
|
|
384
|
-
comment: {
|
|
385
|
-
list: [uuid-comment2, uuid-comment3, uuid-comment5],
|
|
386
|
-
do: doComment,
|
|
387
|
-
}
|
|
388
280
|
}
|
|
389
281
|
}
|
|
390
282
|
}
|
package/package.json
CHANGED
package/src/ThunkContext.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ReactNode, useState } from 'react'
|
|
1
|
+
import { type ReactNode, useMemo, useState } from 'react'
|
|
2
2
|
import type { ClassState } from './stateTypes'
|
|
3
3
|
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
4
4
|
|
|
@@ -22,8 +22,16 @@ const ThunkContext = (props: Props) => {
|
|
|
22
22
|
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
23
23
|
// biome-ignore lint/suspicious/noExplicitAny: This generalized state can be any type.
|
|
24
24
|
const [classState, setClassState] = useState<ClassState<any>>({ myClass: theClass, nodes: {} })
|
|
25
|
+
|
|
25
26
|
refClassState.current = classState
|
|
26
|
-
|
|
27
|
+
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
28
|
+
const value = useMemo(
|
|
29
|
+
() => ({
|
|
30
|
+
refClassState,
|
|
31
|
+
setClassState,
|
|
32
|
+
}),
|
|
33
|
+
[classState],
|
|
34
|
+
)
|
|
27
35
|
|
|
28
36
|
const theChildren =
|
|
29
37
|
classes.length === 1 ? children : ThunkContext({ classes: classes.slice(1), children })
|