@chhsiao1981/use-thunk 9.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.
- package/LICENSE +21 -0
- package/README.md +437 -0
- package/package.json +63 -0
- package/src/action.ts +20 -0
- package/src/addChild.ts +13 -0
- package/src/addLink.ts +27 -0
- package/src/addRelation.ts +32 -0
- package/src/createReducer.ts +24 -0
- package/src/dispatch.ts +6 -0
- package/src/dispatchFuncMap.ts +22 -0
- package/src/genUUID.ts +38 -0
- package/src/getRelation.ts +35 -0
- package/src/index.ts +62 -0
- package/src/init.ts +82 -0
- package/src/reduceMap.ts +37 -0
- package/src/reducer.ts +9 -0
- package/src/remove.ts +76 -0
- package/src/removeChild.ts +46 -0
- package/src/removeLink.ts +47 -0
- package/src/removeRelation.ts +84 -0
- package/src/setData.ts +23 -0
- package/src/setRoot.ts +14 -0
- package/src/stateTypes.ts +62 -0
- package/src/states.ts +45 -0
- package/src/thunk.ts +16 -0
- package/src/thunkModuleFuncMap.ts +22 -0
- package/src/thunkReducer.ts +73 -0
- package/src/useThunk.ts +93 -0
- package/types/action.d.ts +11 -0
- package/types/addChild.d.ts +5 -0
- package/types/addLink.d.ts +6 -0
- package/types/addRelation.d.ts +6 -0
- package/types/createReducer.d.ts +4 -0
- package/types/dispatch.d.ts +4 -0
- package/types/dispatchFuncMap.d.ts +17 -0
- package/types/genUUID.d.ts +1 -0
- package/types/getRelation.d.ts +5 -0
- package/types/index.d.ts +19 -0
- package/types/init.d.ts +19 -0
- package/types/reduceMap.d.ts +6 -0
- package/types/reducer.d.ts +5 -0
- package/types/reducerModuleFuncMap.d.ts +10 -0
- package/types/remove.d.ts +5 -0
- package/types/removeChild.d.ts +9 -0
- package/types/removeLink.d.ts +9 -0
- package/types/removeRelation.d.ts +12 -0
- package/types/setData.d.ts +5 -0
- package/types/setRoot.d.ts +5 -0
- package/types/stateTypes.d.ts +42 -0
- package/types/states.d.ts +6 -0
- package/types/thunk-reducer.d.ts +16 -0
- package/types/thunk.d.ts +12 -0
- package/types/thunkModuleFuncMap.d.ts +10 -0
- package/types/thunkReducer.d.ts +18 -0
- package/types/useReducer.d.ts +8 -0
- package/types/useThunk.d.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Chuan-Heng Hsiao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
# use-thunk
|
|
2
|
+
|
|
3
|
+
[](https://codecov.io/gh/chhsiao1981/use-thunk)
|
|
4
|
+
|
|
5
|
+
A framework easily using `useThunk` to manage the data-state.
|
|
6
|
+
|
|
7
|
+
Adopted concept of [redux-thunk](https://redux.js.org/usage/writing-logic-thunks) and [redux-duck](https://github.com/PlatziDev/redux-duck)
|
|
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).
|
|
10
|
+
|
|
11
|
+
`use-thunk` is with the following additional features:
|
|
12
|
+
|
|
13
|
+
1. The development of the thunk modules follows the concept of `redux-duck`.
|
|
14
|
+
2. Instead of action / reducer, we focus only on `thunk`, and have the primitive actions/reducers.
|
|
15
|
+
|
|
16
|
+
Please check [docs/00-introduction.md](docs/00-introduction.md) for more information.
|
|
17
|
+
|
|
18
|
+
### Breaking Changes
|
|
19
|
+
|
|
20
|
+
* Starting from `9.0.0`: npm package is renamed as [@chhsiao1981/use-thunk](https://www.npmjs.com/package/%40chhsiao1981/use-thunk)
|
|
21
|
+
* Starting from `8.0.0`: [Totally renamed as `useThunk`](https://github.com/chhsiao1981/use-thunk/issues/105).
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
npm install --save @chhsiao1981/use-thunk
|
|
26
|
+
|
|
27
|
+
## Example
|
|
28
|
+
|
|
29
|
+
Thunk module able to do increment (reducers/increment.ts):
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { init as _init, setData, createReducer, Thunk, getState, type State as rState, genUUID } from '@chhsiao1981/use-thunk'
|
|
33
|
+
|
|
34
|
+
export const myClass = 'demo/Increment'
|
|
35
|
+
|
|
36
|
+
export interface State extends rState {
|
|
37
|
+
count: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const defaultState: State = {
|
|
41
|
+
count: 0
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const init = (): Thunk<State> => {
|
|
45
|
+
const myID = genUUID()
|
|
46
|
+
return async (dispatch, getClassState) => {
|
|
47
|
+
dispatch(_init({myID, state: defaultState}))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const increment = (myID: string): Thunk<State> => {
|
|
52
|
+
return async (dispatch, getClassState) => {
|
|
53
|
+
let classState = getClassState()
|
|
54
|
+
let me = getState(classState, myID)
|
|
55
|
+
if(!me) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
dispatch(setData(myID, { count: me.count + 1 }))
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
App.tsx:
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
import { type ThunkModuleToFunc, useThunk, getRootID, getState } from '@chhsiao1981/use-thunk'
|
|
68
|
+
import * as DoIncrement from './reducers/increment'
|
|
69
|
+
|
|
70
|
+
type TDoIncrement = ThunkModuleToFunc(typeof DoIncrement)
|
|
71
|
+
|
|
72
|
+
type Props = {
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default (props: Props) => {
|
|
76
|
+
const [stateIncrement, doIncrement] = useThunk<DoIncrement.State, TDoIncrement>(DoIncrement, StateType.LOCAL)
|
|
77
|
+
|
|
78
|
+
//init
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
doIncrement.init()
|
|
81
|
+
}, [])
|
|
82
|
+
|
|
83
|
+
// to render
|
|
84
|
+
const incrementID = getRootID(stateIncrement)
|
|
85
|
+
const increment = getState(stateIncrement)
|
|
86
|
+
if(!increment) {
|
|
87
|
+
return (<div styles={{display: 'none'}}></div>)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div>
|
|
92
|
+
<p>count: {increment.count}</p>
|
|
93
|
+
<button onClick={() => doIncrement.increment(incrementID)}>increase</button>
|
|
94
|
+
</div>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Must Included in a Thunk Module
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import type { State as rState } from '@chhsiao1981/use-thunk'
|
|
103
|
+
|
|
104
|
+
// reducer class name.
|
|
105
|
+
export const myClass = ""
|
|
106
|
+
|
|
107
|
+
// state definition of the reducer.
|
|
108
|
+
export interface State extends rState {
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.
|
|
112
|
+
.
|
|
113
|
+
.
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Must Included in a Top-level Component
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { type ThunkModuleToFunc, useThunk, getRootID, getState } from '@chhsiao1981/use-thunk'
|
|
120
|
+
import * as DoModule from '../reducers/module'
|
|
121
|
+
|
|
122
|
+
type TDoModule = ThunkModuleToFunc<typeof DoModule>
|
|
123
|
+
|
|
124
|
+
const Component = () => {
|
|
125
|
+
const [stateModule, doModule] = useThunk<DoModule.State, TDoModule>(DoModule)
|
|
126
|
+
|
|
127
|
+
const moduleID = getRootID(stateModule)
|
|
128
|
+
const theModule = getState(stateModule)
|
|
129
|
+
|
|
130
|
+
.
|
|
131
|
+
.
|
|
132
|
+
.
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Normalized State
|
|
137
|
+
|
|
138
|
+
The general concept of normalized state can be found in [Normalizing State Shape](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape)
|
|
139
|
+
with the following features:
|
|
140
|
+
|
|
141
|
+
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.
|
|
143
|
+
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
|
+
|
|
155
|
+
For example, the example [in the redux link](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape) is represented as:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
statePost = {
|
|
159
|
+
myClass: 'post',
|
|
160
|
+
doMe: (DispatchedAction<Post>),
|
|
161
|
+
nodes: {
|
|
162
|
+
[uuid-post1] : {
|
|
163
|
+
id: uuid-post1,
|
|
164
|
+
state: {
|
|
165
|
+
author : uuid-user1,
|
|
166
|
+
body : "......",
|
|
167
|
+
},
|
|
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
|
+
},
|
|
179
|
+
[uuid-post2] : {
|
|
180
|
+
id : uuid-post2,
|
|
181
|
+
state: {
|
|
182
|
+
author : uuid-user2,
|
|
183
|
+
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
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
and:
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
stateComment = {
|
|
204
|
+
myClass: 'comment',
|
|
205
|
+
doMe: (DispatchedAction<Comment>),
|
|
206
|
+
nodes: {
|
|
207
|
+
[uuid-comment1] : {
|
|
208
|
+
id: uuid-comment1,
|
|
209
|
+
state: {
|
|
210
|
+
author : uuid-user2,
|
|
211
|
+
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
|
+
}
|
|
223
|
+
},
|
|
224
|
+
[uuid-comment2] : {
|
|
225
|
+
id : uuid-comment2,
|
|
226
|
+
state: {
|
|
227
|
+
author : uuid-user3,
|
|
228
|
+
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
|
+
}
|
|
240
|
+
},
|
|
241
|
+
[uuid-comment3] : {
|
|
242
|
+
id : uuid-comment3,
|
|
243
|
+
state: {
|
|
244
|
+
author : uuid-user3,
|
|
245
|
+
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
|
+
}
|
|
257
|
+
},
|
|
258
|
+
[uuid-comment4] : {
|
|
259
|
+
id : uuid-comment4,
|
|
260
|
+
state: {
|
|
261
|
+
author : uuid-user1,
|
|
262
|
+
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
|
+
}
|
|
274
|
+
},
|
|
275
|
+
[uuid-comment5] : {
|
|
276
|
+
id : uuid-comment5,
|
|
277
|
+
state: {
|
|
278
|
+
author : uuid-user3,
|
|
279
|
+
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
|
+
}
|
|
291
|
+
},
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
and:
|
|
297
|
+
```ts
|
|
298
|
+
stateUser = {
|
|
299
|
+
myClass: 'user',
|
|
300
|
+
doMe: (DispatchedAction<User>),
|
|
301
|
+
nodes: {
|
|
302
|
+
[uuid-user1] : {
|
|
303
|
+
id: uuid-user1,
|
|
304
|
+
state: {
|
|
305
|
+
username : "user1",
|
|
306
|
+
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
|
+
}
|
|
318
|
+
},
|
|
319
|
+
[uuid-user2] : {
|
|
320
|
+
id: uuid-user2,
|
|
321
|
+
state: {
|
|
322
|
+
username : "user2",
|
|
323
|
+
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
|
+
}
|
|
335
|
+
},
|
|
336
|
+
[uuid-user3] : {
|
|
337
|
+
id: uuid-user3,
|
|
338
|
+
state: {
|
|
339
|
+
username : "user3",
|
|
340
|
+
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
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
## [APIs](https://github.com/chhsiao1981/use-thunk/blob/main/src/index.d.ts)
|
|
358
|
+
|
|
359
|
+
### Basic
|
|
360
|
+
|
|
361
|
+
##### `useThunk(theDo: ThunkModuleFunc): [ClassState, DispatchedAction]`
|
|
362
|
+
|
|
363
|
+
Similar to `React.useReducer`, but we use `useThunk`, and we also bind the actions with dispatch (similar concept as `mapDispatchToProps`).s
|
|
364
|
+
|
|
365
|
+
return: `[ClassState<S>, DispatchedAction<S>]`
|
|
366
|
+
|
|
367
|
+
##### `init({myID, parentID, doParent, state}, myuuidv4?)`
|
|
368
|
+
|
|
369
|
+
initializing the react-object.
|
|
370
|
+
|
|
371
|
+
##### `genUUID(myuuidv4?: () => string): string`
|
|
372
|
+
|
|
373
|
+
generate uuid for react-object.
|
|
374
|
+
|
|
375
|
+
##### `setData(myID, data)`
|
|
376
|
+
|
|
377
|
+
set the data to myID.
|
|
378
|
+
|
|
379
|
+
##### `remove(myID, isFromParent=false)`
|
|
380
|
+
|
|
381
|
+
remove the react-object.
|
|
382
|
+
|
|
383
|
+
### State
|
|
384
|
+
|
|
385
|
+
##### `getState(state: ClassState, myID?: string): State`
|
|
386
|
+
|
|
387
|
+
Get the state of `myID`. Get the state of `rootID` if `myID` is not present.
|
|
388
|
+
|
|
389
|
+
##### `getRootID(state: ClassState): string`
|
|
390
|
+
|
|
391
|
+
get the root id.
|
|
392
|
+
|
|
393
|
+
### NodeState
|
|
394
|
+
|
|
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)`
|
|
436
|
+
|
|
437
|
+
remove the link of `myID` (and remove the link from linkID).
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@chhsiao1981/use-thunk",
|
|
3
|
+
"version": "9.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A framework easily using useThunk to manage the data-state.",
|
|
6
|
+
"homepage": "https://github.com/chhsiao1981/use-thunk",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/chhsiao1981/use-thunk"
|
|
10
|
+
},
|
|
11
|
+
"main": "src/index.ts",
|
|
12
|
+
"types": "types/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"dist",
|
|
16
|
+
"types"
|
|
17
|
+
],
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Chuan-Heng Hsiao",
|
|
20
|
+
"email": "hsiao.chuanheng@gmail.com"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"react",
|
|
24
|
+
"useThunk"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean:lib": "rimraf ./dist/* ./types/*",
|
|
28
|
+
"compile:lib": "vite build",
|
|
29
|
+
"build:lib": "npm run clean:lib && npm run types && npm run compile:lib",
|
|
30
|
+
"build": "npm run build:lib",
|
|
31
|
+
"lint": "biome lint --error-on-warnings",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"types": "tsc -b",
|
|
34
|
+
"coverage": "vitest run --coverage --reporter=junit --outputFile=test-report.junit.xml",
|
|
35
|
+
"tsd": "npx -p typescript tsc src/*.ts --declaration --allowJs --emitDeclarationOnly --strict --outDir types"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@codecov/vite-plugin": "^1.9.1",
|
|
40
|
+
"@eslint/js": "^9.33.0",
|
|
41
|
+
"@types/node": "^24.3.0",
|
|
42
|
+
"@types/react": ">=18.3.1",
|
|
43
|
+
"@types/react-dom": ">=18.3.1",
|
|
44
|
+
"@vitejs/plugin-react-swc": "^4.0.0",
|
|
45
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
46
|
+
"eslint": "^9.33.0",
|
|
47
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
48
|
+
"eslint-plugin-react-refresh": "^0.4.20",
|
|
49
|
+
"globals": "^16.3.0",
|
|
50
|
+
"happy-dom": "^20.0.0",
|
|
51
|
+
"jiti": "^2.5.1",
|
|
52
|
+
"react-dom": ">=18.3.1",
|
|
53
|
+
"rimraf": "^6.0.1",
|
|
54
|
+
"typescript": "~5.9.3",
|
|
55
|
+
"typescript-eslint": "^8.46.0",
|
|
56
|
+
"vite": "^6.3.6",
|
|
57
|
+
"vitest": "^3.2.4"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"react": ">=18.3.1",
|
|
61
|
+
"uuid": "^11.1.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/action.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ClassState, State } from './stateTypes'
|
|
2
|
+
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './thunkReducer'
|
|
3
|
+
|
|
4
|
+
// BaseAction contains only object-based actions, no thunk-based actions.
|
|
5
|
+
export interface BaseAction {
|
|
6
|
+
myID: string
|
|
7
|
+
type: string
|
|
8
|
+
[key: string]: unknown
|
|
9
|
+
}
|
|
10
|
+
// Thunk
|
|
11
|
+
export type Thunk<S extends State> = rThunk<ClassState<S>, BaseAction>
|
|
12
|
+
|
|
13
|
+
export type ActionOrThunk<S extends State> = rActionOrThunk<ClassState<S>, BaseAction>
|
|
14
|
+
|
|
15
|
+
// ActionFunc
|
|
16
|
+
// biome-ignore lint/suspicious/noExplicitAny: unknown requires same type in list. use any for possible different types.
|
|
17
|
+
export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>
|
|
18
|
+
|
|
19
|
+
// GetClassState
|
|
20
|
+
export type GetClassState<S extends State> = () => ClassState<S>
|
package/src/addChild.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type AddRelationAction, reduceAddRelation } from './addRelation'
|
|
2
|
+
import { type ClassState, type NodeMeta, Relation, type State } from './stateTypes'
|
|
3
|
+
|
|
4
|
+
export const ADD_CHILD = '@chhsiao1981/use-thunk/ADD_CHILD'
|
|
5
|
+
export const addChild = (myID: string, child: NodeMeta): AddRelationAction => ({
|
|
6
|
+
myID,
|
|
7
|
+
type: ADD_CHILD,
|
|
8
|
+
relaton: child,
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
export const reduceAddChild = <S extends State>(state: ClassState<S>, action: AddRelationAction): ClassState<S> => {
|
|
12
|
+
return reduceAddRelation(state, action, Relation.CHILDREN)
|
|
13
|
+
}
|
package/src/addLink.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Thunk } from './action'
|
|
2
|
+
import { type AddRelationAction, reduceAddRelation } from './addRelation'
|
|
3
|
+
import { type ClassState, type NodeMeta, Relation, type State } from './stateTypes'
|
|
4
|
+
|
|
5
|
+
export const addLink = <S extends State>(myID: string, link: NodeMeta, isFromLink = false): Thunk<S> => {
|
|
6
|
+
return (dispatch, getClassState) => {
|
|
7
|
+
dispatch(addLinkCore(myID, link))
|
|
8
|
+
|
|
9
|
+
if (!isFromLink) {
|
|
10
|
+
// I connect to the other, would like the other to connect to me as well.
|
|
11
|
+
// @ts-expect-error XXX doMe is a hidden variable for links.
|
|
12
|
+
const { doMe, myClass } = getClassState()
|
|
13
|
+
link.do.addLink(link.id, { id: myID, theClass: myClass, do: doMe }, true)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const ADD_LINK = '@chhsiao1981/use-thunk/ADD_LINK'
|
|
19
|
+
const addLinkCore = (myID: string, link: NodeMeta): AddRelationAction => ({
|
|
20
|
+
myID,
|
|
21
|
+
type: ADD_LINK,
|
|
22
|
+
relaton: link,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export const reduceAddLink = <S extends State>(state: ClassState<S>, action: AddRelationAction): ClassState<S> => {
|
|
26
|
+
return reduceAddRelation(state, action, Relation.LINKS)
|
|
27
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { BaseAction } from './action'
|
|
2
|
+
import type { ClassState, NodeMeta, Relation, State } from './stateTypes'
|
|
3
|
+
|
|
4
|
+
export interface AddRelationAction extends BaseAction {
|
|
5
|
+
relaton: NodeMeta
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const reduceAddRelation = <S extends State>(
|
|
9
|
+
state: ClassState<S>,
|
|
10
|
+
action: AddRelationAction,
|
|
11
|
+
relationName: Relation,
|
|
12
|
+
): ClassState<S> => {
|
|
13
|
+
const { myID, relaton: relation } = action
|
|
14
|
+
const me = state.nodes[myID]
|
|
15
|
+
if (!me) {
|
|
16
|
+
return state
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { theClass, id: theID, do: theDo } = relation
|
|
20
|
+
|
|
21
|
+
const relations = me[relationName] ?? {}
|
|
22
|
+
const relationsByClass = relations[theClass] ?? { list: [] }
|
|
23
|
+
const relationIDs = relationsByClass.list ?? []
|
|
24
|
+
const newIDs = theID ? relationIDs.concat([theID]) : relationIDs
|
|
25
|
+
|
|
26
|
+
const newRelations = Object.assign({}, me[relationName], { [theClass]: { list: newIDs, do: theDo } })
|
|
27
|
+
const newMe = Object.assign({}, me, { [relationName]: newRelations })
|
|
28
|
+
const newNodes = Object.assign({}, state.nodes, { [myID]: newMe })
|
|
29
|
+
const newState = Object.assign({}, state, { nodes: newNodes })
|
|
30
|
+
|
|
31
|
+
return newState
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { BaseAction } from './action'
|
|
2
|
+
import { DEFAULT_REDUCE_MAP, type ReduceMap } from './reduceMap'
|
|
3
|
+
import type { Reducer } from './reducer'
|
|
4
|
+
|
|
5
|
+
import type { ClassState, State } from './stateTypes'
|
|
6
|
+
|
|
7
|
+
export const createReducer = <S extends State>(reduceMap?: ReduceMap<S>): Reducer<S> => {
|
|
8
|
+
return (state: ClassState<S>, action: BaseAction): ClassState<S> => {
|
|
9
|
+
if (!action) {
|
|
10
|
+
return state
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (reduceMap?.[action.type]) {
|
|
14
|
+
return reduceMap[action.type](state, action)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const defaultReduceMap = DEFAULT_REDUCE_MAP<S>()
|
|
18
|
+
if (defaultReduceMap?.[action.type]) {
|
|
19
|
+
return defaultReduceMap[action.type](state, action)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return state
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/dispatch.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { State } from './stateTypes'
|
|
2
|
+
import type { ThunkModuleFunc } from './thunk'
|
|
3
|
+
import type { DefaultThunkModuleFuncMap } from './thunkModuleFuncMap'
|
|
4
|
+
|
|
5
|
+
// biome-ignore lint/suspicious/noExplicitAny: unknown requires same type in list, use any for possible different types.
|
|
6
|
+
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void
|
|
7
|
+
|
|
8
|
+
export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
9
|
+
[action in keyof T]: VoidReturnType<T[action]>
|
|
10
|
+
} & Omit<DefaultDispatchFuncMap, keyof T>
|
|
11
|
+
|
|
12
|
+
export type DefaultDispatchFuncMap = {
|
|
13
|
+
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
17
|
+
[className: string]: DispatchFuncMap<S, T>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface RefDispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
21
|
+
current: DispatchFuncMapByClassMap<S, T>
|
|
22
|
+
}
|