@chhsiao1981/use-thunk 9.0.5 → 10.0.0

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 (51) hide show
  1. package/README.md +52 -55
  2. package/dist/index.js +1723 -0
  3. package/dist/index.umd.cjs +50 -0
  4. package/package.json +2 -2
  5. package/src/ThunkContext.tsx +34 -0
  6. package/src/dispatchFuncMap.ts +48 -4
  7. package/src/index.ts +4 -18
  8. package/src/init.ts +12 -30
  9. package/src/reduceMap.ts +0 -14
  10. package/src/registerThunk.ts +35 -0
  11. package/src/remove.ts +6 -32
  12. package/src/setRoot.ts +4 -1
  13. package/src/stateTypes.ts +0 -29
  14. package/src/states.ts +4 -1
  15. package/src/thunk.ts +1 -1
  16. package/src/thunkContextMap.ts +22 -0
  17. package/src/thunkContextTypes.ts +8 -0
  18. package/src/thunkModuleFuncMap.ts +0 -11
  19. package/src/useThunk.ts +31 -55
  20. package/src/useThunkReducer.ts +19 -22
  21. package/types/ThunkContext.d.ts +7 -0
  22. package/types/dispatchFuncMap.d.ts +6 -5
  23. package/types/index.d.ts +3 -8
  24. package/types/init.d.ts +1 -1
  25. package/types/registerThunk.d.ts +4 -0
  26. package/types/remove.d.ts +2 -2
  27. package/types/stateTypes.d.ts +0 -19
  28. package/types/thunk.d.ts +1 -1
  29. package/types/thunkContextMap.d.ts +15 -0
  30. package/types/thunkContextTypes.d.ts +8 -0
  31. package/types/thunkModuleFuncMap.d.ts +1 -5
  32. package/types/useThunk.d.ts +6 -3
  33. package/types/useThunkReducer.d.ts +3 -4
  34. package/src/addChild.ts +0 -16
  35. package/src/addLink.ts +0 -27
  36. package/src/addRelation.ts +0 -32
  37. package/src/getRelation.ts +0 -43
  38. package/src/removeChild.ts +0 -46
  39. package/src/removeLink.ts +0 -47
  40. package/src/removeRelation.ts +0 -84
  41. package/types/addChild.d.ts +0 -5
  42. package/types/addLink.d.ts +0 -6
  43. package/types/addRelation.d.ts +0 -6
  44. package/types/getRelation.d.ts +0 -5
  45. package/types/reducerModuleFuncMap.d.ts +0 -10
  46. package/types/removeChild.d.ts +0 -9
  47. package/types/removeLink.d.ts +0 -9
  48. package/types/removeRelation.d.ts +0 -12
  49. package/types/thunk-reducer.d.ts +0 -16
  50. package/types/thunkReducer.d.ts +0 -18
  51. package/types/useReducer.d.ts +0 -8
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,6 +153,23 @@ 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)
@@ -368,10 +405,6 @@ return: `[ClassState<S>, DispatchedAction<S>]`
368
405
 
369
406
  initializing the react-object.
370
407
 
371
- ##### `genUUID(myuuidv4?: () => string): string`
372
-
373
- generate uuid for react-object.
374
-
375
408
  ##### `setData(myID, data)`
376
409
 
377
410
  set the data to myID.
@@ -380,6 +413,10 @@ set the data to myID.
380
413
 
381
414
  remove the react-object.
382
415
 
416
+ ##### `genUUID(myuuidv4?: () => string): string`
417
+
418
+ generate uuid for react-object.
419
+
383
420
  ### State
384
421
 
385
422
  ##### `getState(state: ClassState, myID?: string): State`
@@ -392,46 +429,6 @@ get the root id.
392
429
 
393
430
  ### NodeState
394
431
 
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)`
432
+ ##### `getNode(state: ClassState, myID?: string): NodeState`
436
433
 
437
- remove the link of `myID` (and remove the link from linkID).
434
+ Get the node of `myID`. Get the node of `rootID` if `myID` is not present.