@chhsiao1981/use-thunk 9.0.5 → 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.
Files changed (52) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +59 -170
  3. package/dist/index.js +1723 -0
  4. package/dist/index.umd.cjs +50 -0
  5. package/package.json +2 -2
  6. package/src/ThunkContext.tsx +42 -0
  7. package/src/dispatchFuncMap.ts +48 -4
  8. package/src/index.ts +4 -18
  9. package/src/init.ts +12 -30
  10. package/src/reduceMap.ts +0 -14
  11. package/src/registerThunk.ts +35 -0
  12. package/src/remove.ts +6 -32
  13. package/src/setRoot.ts +4 -1
  14. package/src/stateTypes.ts +0 -29
  15. package/src/states.ts +4 -1
  16. package/src/thunk.ts +1 -1
  17. package/src/thunkContextMap.ts +22 -0
  18. package/src/thunkContextTypes.ts +8 -0
  19. package/src/thunkModuleFuncMap.ts +0 -11
  20. package/src/useThunk.ts +31 -55
  21. package/src/useThunkReducer.ts +19 -22
  22. package/types/ThunkContext.d.ts +7 -0
  23. package/types/dispatchFuncMap.d.ts +6 -5
  24. package/types/index.d.ts +3 -8
  25. package/types/init.d.ts +1 -1
  26. package/types/registerThunk.d.ts +4 -0
  27. package/types/remove.d.ts +2 -2
  28. package/types/stateTypes.d.ts +0 -19
  29. package/types/thunk.d.ts +1 -1
  30. package/types/thunkContextMap.d.ts +15 -0
  31. package/types/thunkContextTypes.d.ts +8 -0
  32. package/types/thunkModuleFuncMap.d.ts +1 -5
  33. package/types/useThunk.d.ts +6 -3
  34. package/types/useThunkReducer.d.ts +3 -4
  35. package/src/addChild.ts +0 -16
  36. package/src/addLink.ts +0 -27
  37. package/src/addRelation.ts +0 -32
  38. package/src/getRelation.ts +0 -43
  39. package/src/removeChild.ts +0 -46
  40. package/src/removeLink.ts +0 -47
  41. package/src/removeRelation.ts +0 -84
  42. package/types/addChild.d.ts +0 -5
  43. package/types/addLink.d.ts +0 -6
  44. package/types/addRelation.d.ts +0 -6
  45. package/types/getRelation.d.ts +0 -5
  46. package/types/reducerModuleFuncMap.d.ts +0 -10
  47. package/types/removeChild.d.ts +0 -9
  48. package/types/removeLink.d.ts +0 -9
  49. package/types/removeRelation.d.ts +0 -12
  50. package/types/thunk-reducer.d.ts +0 -16
  51. package/types/thunkReducer.d.ts +0 -18
  52. package/types/useReducer.d.ts +0 -8
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Chuan-Heng Hsiao
3
+ Copyright (c) 2026 Chuan-Heng Hsiao
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -6,7 +6,7 @@ A framework easily using `useThunk` to manage the data-state.
6
6
 
7
7
  Adopted concept of [redux-thunk](https://redux.js.org/usage/writing-logic-thunks) and [redux-duck](https://github.com/PlatziDev/redux-duck)
8
8
 
9
- [src/thunk-reducer.ts](src/thunkReducer.ts) is adopted from [nathanbuchar/react-hook-thunk-reducer](https://github.com/nathanbuchar/react-hook-thunk-reducer/blob/master/src/thunk-reducer.js).
9
+ [src/useThunkReducer.ts](src/useThunkReducer.ts) is adopted from [nathanbuchar/react-hook-thunk-reducer](https://github.com/nathanbuchar/react-hook-thunk-reducer/blob/master/src/thunk-reducer.js).
10
10
 
11
11
  `use-thunk` is with the following additional features:
12
12
 
@@ -15,8 +15,11 @@ Adopted concept of [redux-thunk](https://redux.js.org/usage/writing-logic-thunks
15
15
 
16
16
  Please check [docs/00-introduction.md](docs/00-introduction.md) for more information.
17
17
 
18
+ Please check [demo-use-thunk](https://github.com/chhsiao1981/demo-use-thunk) for a demo to use Thunk.
19
+
18
20
  ### Breaking Changes
19
21
 
22
+ * Starting from `10.0.0`: The ClassState is shared globally, with `registerThunk` and `ThunkContext`.
20
23
  * Starting from `9.0.0`: npm package is renamed as [@chhsiao1981/use-thunk](https://www.npmjs.com/package/%40chhsiao1981/use-thunk)
21
24
  * Starting from `8.0.0`: [Totally renamed as `useThunk`](https://github.com/chhsiao1981/use-thunk/issues/105).
22
25
 
@@ -29,7 +32,7 @@ Please check [docs/00-introduction.md](docs/00-introduction.md) for more informa
29
32
  Thunk module able to do increment (reducers/increment.ts):
30
33
 
31
34
  ```ts
32
- import { init as _init, setData, createReducer, Thunk, getState, type State as rState, genUUID } from '@chhsiao1981/use-thunk'
35
+ import { init as _init, setData, Thunk, getState, type State as rState, genUUID } from '@chhsiao1981/use-thunk'
33
36
 
34
37
  export const myClass = 'demo/Increment'
35
38
 
@@ -73,20 +76,18 @@ type Props = {
73
76
  }
74
77
 
75
78
  export default (props: Props) => {
76
- const [stateIncrement, doIncrement] = useThunk<DoIncrement.State, TDoIncrement>(DoIncrement, StateType.LOCAL)
79
+ const [classStateIncrement, doIncrement] = useThunk<DoIncrement.State, TDoIncrement>(DoIncrement, StateType.LOCAL)
77
80
 
78
81
  //init
79
82
  useEffect(() => {
80
83
  doIncrement.init()
81
84
  }, [])
82
85
 
83
- // to render
84
- const incrementID = getRootID(stateIncrement)
85
- const increment = getState(stateIncrement)
86
- if(!increment) {
87
- return (<div styles={{display: 'none'}}></div>)
88
- }
86
+ // states
87
+ const incrementID = getRootID(classStateIncrement)
88
+ const increment = getState(classStateIncrement) || DoIncrement.defaultState
89
89
 
90
+ // to render
90
91
  return (
91
92
  <div>
92
93
  <p>count: {increment.count}</p>
@@ -96,6 +97,25 @@ export default (props: Props) => {
96
97
  }
97
98
  ```
98
99
 
100
+ main.tsx:
101
+ ```tsx
102
+ import { registerThunk, ThunkContext } from "@chhsiao1981/use-thunk";
103
+ import { StrictMode } from "react";
104
+ import { createRoot } from "react-dom/client";
105
+ import * as DoIncrement from './reducers/increment'
106
+ import App from "./App.tsx";
107
+
108
+ registerThunk(DoIncrement)
109
+
110
+ createRoot(document.getElementById("root")!).render(
111
+ <StrictMode>
112
+ <ThunkContext>
113
+ <App />
114
+ </ThunkContext>
115
+ </StrictMode>,
116
+ )
117
+ ```
118
+
99
119
  ### Must Included in a Thunk Module
100
120
 
101
121
  ```ts
@@ -133,29 +153,36 @@ const Component = () => {
133
153
  }
134
154
  ```
135
155
 
156
+ ### Must Included in main.tsx
157
+
158
+ ```tsx
159
+ registerThunk(...)
160
+ .
161
+ .
162
+ .
163
+
164
+ createRoot(document.getElementById("root")!).render(
165
+ <StrictMode>
166
+ <ThunkContext>
167
+ <App />
168
+ </ThunkContext>
169
+ </StrictMode>,
170
+ )
171
+ ```
172
+
136
173
  ## Normalized State
137
174
 
138
175
  The general concept of normalized state can be found in [Normalizing State Shape](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape)
139
176
  with the following features:
140
177
 
141
178
  1. ClassState: the state of the class, including the nodes and the root of the class.
142
- 2. NodeState: the state of a node, including the id, children, parent, links of the node, and the content (state) of the node.
179
+ 2. NodeState: the state of a node, including the id of the node and the content (state) of the node.
143
180
  3. State: the content of the node, represented as a state.
144
- 4. The concept of "parent" and "children" and "links" is embedded in the NodeState.
145
- * remove (me):
146
- - initiate "remove" for all the children.
147
- - remove from the parent.
148
- - remove from all the links.
149
- * remove child:
150
- - the child initiate "remove".
151
- * remove link:
152
- - the link initiate "remove link" on me.
153
- 4. To avoid complication, currently there is only 1 parent.
154
181
 
155
182
  For example, the example [in the redux link](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape) is represented as:
156
183
 
157
184
  ```ts
158
- statePost = {
185
+ classStatePost = {
159
186
  myClass: 'post',
160
187
  doMe: (DispatchedAction<Post>),
161
188
  nodes: {
@@ -164,33 +191,15 @@ statePost = {
164
191
  state: {
165
192
  author : uuid-user1,
166
193
  body : "......",
194
+ comments: [uuid-comment1, uuid-comment2]
167
195
  },
168
- _parent: {
169
- id: uuid-user1,
170
- do: doUser
171
- },
172
- _links: {
173
- comment : {
174
- list: [uuid-comment1, uuid-comment2],
175
- do: doComment
176
- }
177
- }
178
196
  },
179
197
  [uuid-post2] : {
180
198
  id : uuid-post2,
181
199
  state: {
182
200
  author : uuid-user2,
183
201
  body : "......",
184
- },
185
- _parent: {
186
- id: uuid-user2,
187
- do: doUser
188
- },
189
- _links: {
190
- comment : {
191
- list: [uuid-comment3, uuid-comment4, uuid-comment5],
192
- do: doComment
193
- }
202
+ comments: [uuid-comment3, uuid-comment4, uuid-comment5]
194
203
  }
195
204
  }
196
205
  }
@@ -200,7 +209,7 @@ statePost = {
200
209
  and:
201
210
 
202
211
  ```ts
203
- stateComment = {
212
+ classStateComment = {
204
213
  myClass: 'comment',
205
214
  doMe: (DispatchedAction<Comment>),
206
215
  nodes: {
@@ -209,16 +218,6 @@ stateComment = {
209
218
  state: {
210
219
  author : uuid-user2,
211
220
  comment : ".....",
212
- },
213
- _parent: {
214
- id: uuid-user2,
215
- do: doUser
216
- },
217
- _links: {
218
- post: {
219
- list: [uuid-post1],
220
- do: doPost
221
- }
222
221
  }
223
222
  },
224
223
  [uuid-comment2] : {
@@ -226,16 +225,6 @@ stateComment = {
226
225
  state: {
227
226
  author : uuid-user3,
228
227
  comment : ".....",
229
- },
230
- _parent: {
231
- id: uuid-user3,
232
- do: doUser
233
- },
234
- _links: {
235
- post: {
236
- list: [uuid-post1],
237
- do: doPost
238
- }
239
228
  }
240
229
  },
241
230
  [uuid-comment3] : {
@@ -243,16 +232,6 @@ stateComment = {
243
232
  state: {
244
233
  author : uuid-user3,
245
234
  comment : ".....",
246
- },
247
- _parent: {
248
- id: uuid-user3,
249
- do: doUser
250
- },
251
- _links: {
252
- post: {
253
- list: [uuid-post2],
254
- do: doPost
255
- }
256
235
  }
257
236
  },
258
237
  [uuid-comment4] : {
@@ -260,16 +239,6 @@ stateComment = {
260
239
  state: {
261
240
  author : uuid-user1,
262
241
  comment : ".....",
263
- },
264
- _parent: {
265
- id: uuid-user1,
266
- do: doUser
267
- },
268
- _links: {
269
- post: {
270
- list: [uuid-post2],
271
- do: doPost
272
- }
273
242
  }
274
243
  },
275
244
  [uuid-comment5] : {
@@ -277,25 +246,15 @@ stateComment = {
277
246
  state: {
278
247
  author : uuid-user3,
279
248
  comment : ".....",
280
- },
281
- _parent: {
282
- id: uuid-user3,
283
- do: doUser
284
- },
285
- _links: {
286
- post: {
287
- list: [uuid-post2],
288
- do: doPost
289
- }
290
249
  }
291
- },
250
+ }
292
251
  }
293
252
  }
294
253
  ```
295
254
 
296
255
  and:
297
256
  ```ts
298
- stateUser = {
257
+ classStateUser = {
299
258
  myClass: 'user',
300
259
  doMe: (DispatchedAction<User>),
301
260
  nodes: {
@@ -304,16 +263,6 @@ stateUser = {
304
263
  state: {
305
264
  username : "user1",
306
265
  name : "User 1",
307
- },
308
- _children: {
309
- post: {
310
- list: [uuid-post1],
311
- do: doPost,
312
- },
313
- comment: {
314
- list: [uuid-comment4],
315
- do: doComment,
316
- }
317
266
  }
318
267
  },
319
268
  [uuid-user2] : {
@@ -321,16 +270,6 @@ stateUser = {
321
270
  state: {
322
271
  username : "user2",
323
272
  name : "User 2",
324
- },
325
- _children: {
326
- post: {
327
- list: [uuid-post2],
328
- do: doPost,
329
- },
330
- comment: {
331
- list: [uuid-comment1],
332
- do: doComment,
333
- }
334
273
  }
335
274
  },
336
275
  [uuid-user3] : {
@@ -338,16 +277,6 @@ stateUser = {
338
277
  state: {
339
278
  username : "user3",
340
279
  name : "User 3",
341
- },
342
- _children: {
343
- post: {
344
- list: [uuid-post1],
345
- do: doPost,
346
- },
347
- comment: {
348
- list: [uuid-comment2, uuid-comment3, uuid-comment5],
349
- do: doComment,
350
- }
351
280
  }
352
281
  }
353
282
  }
@@ -368,10 +297,6 @@ return: `[ClassState<S>, DispatchedAction<S>]`
368
297
 
369
298
  initializing the react-object.
370
299
 
371
- ##### `genUUID(myuuidv4?: () => string): string`
372
-
373
- generate uuid for react-object.
374
-
375
300
  ##### `setData(myID, data)`
376
301
 
377
302
  set the data to myID.
@@ -380,6 +305,10 @@ set the data to myID.
380
305
 
381
306
  remove the react-object.
382
307
 
308
+ ##### `genUUID(myuuidv4?: () => string): string`
309
+
310
+ generate uuid for react-object.
311
+
383
312
  ### State
384
313
 
385
314
  ##### `getState(state: ClassState, myID?: string): State`
@@ -392,46 +321,6 @@ get the root id.
392
321
 
393
322
  ### NodeState
394
323
 
395
- ##### `getNode(state: ClassState, myID: string): NodeState`
396
-
397
- Get the node of `myID`. Get the node of `rootID` if `myID` is not present.s
398
-
399
- ##### `getChildIDs(me: NodeState, childClass: string): string[]`
400
-
401
- get the child-ids of the childClass.
402
-
403
- ##### `getChildID(me: NodeState, childClass: string): string`
404
-
405
- get the only child-id (`childIDs[0]`) of the childClass.
406
-
407
-
408
- ##### `getLinkIDs(me: NodeState, linkClass string): string[]`
409
-
410
- get the link-ids of the linkClass.
411
-
412
-
413
- ##### `getLinkID(me: NodeState, linkClass): string`
414
-
415
- get the only link-id (`linkIDs[0]`) of the linkClass.
416
-
417
- ### Children
418
-
419
- ##### `addChild(myID, child)`
420
-
421
- params:
422
- * child: `{id, theClass, do}`
423
-
424
- ##### `removeChild(myID, childID, childClass, isFromChild=false)`
425
-
426
- remove the child (and delete the child) of `myID`.
427
-
428
- ### Link
429
-
430
- ##### `addLink(myID, link, isFromLink=false)`
431
-
432
- params:
433
- * link: `{id, theClass, do}`
434
-
435
- ##### `removeLink(myID, linkID, linkClass, isFromLink=false)`
324
+ ##### `getNode(state: ClassState, myID?: string): NodeState`
436
325
 
437
- remove the link of `myID` (and remove the link from linkID).
326
+ Get the node of `myID`. Get the node of `rootID` if `myID` is not present.