@chhsiao1981/use-thunk 10.1.2 → 10.3.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/README.md +21 -13
- package/package.json +2 -2
- package/src/ThunkContext.tsx +1 -3
- package/src/index.ts +4 -4
- package/src/registerThunk.ts +2 -1
- package/src/stateTypes.ts +1 -15
- package/src/states.ts +17 -5
- package/types/index.d.ts +3 -3
- package/types/stateTypes.d.ts +1 -8
- package/types/states.d.ts +5 -1
package/README.md
CHANGED
|
@@ -184,7 +184,6 @@ For example, the example [in the redux link](https://redux.js.org/recipes/struct
|
|
|
184
184
|
```ts
|
|
185
185
|
classStatePost = {
|
|
186
186
|
myClass: 'post',
|
|
187
|
-
doMe: (DispatchedAction<Post>),
|
|
188
187
|
nodes: {
|
|
189
188
|
[uuid-post1] : {
|
|
190
189
|
id: uuid-post1,
|
|
@@ -202,7 +201,9 @@ classStatePost = {
|
|
|
202
201
|
comments: [uuid-comment3, uuid-comment4, uuid-comment5]
|
|
203
202
|
}
|
|
204
203
|
}
|
|
205
|
-
}
|
|
204
|
+
},
|
|
205
|
+
defaultID,
|
|
206
|
+
defaultState,
|
|
206
207
|
}
|
|
207
208
|
```
|
|
208
209
|
|
|
@@ -211,7 +212,6 @@ and:
|
|
|
211
212
|
```ts
|
|
212
213
|
classStateComment = {
|
|
213
214
|
myClass: 'comment',
|
|
214
|
-
doMe: (DispatchedAction<Comment>),
|
|
215
215
|
nodes: {
|
|
216
216
|
[uuid-comment1] : {
|
|
217
217
|
id: uuid-comment1,
|
|
@@ -249,6 +249,8 @@ classStateComment = {
|
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
|
+
defaultID,
|
|
253
|
+
defaultState,
|
|
252
254
|
}
|
|
253
255
|
```
|
|
254
256
|
|
|
@@ -256,7 +258,6 @@ and:
|
|
|
256
258
|
```ts
|
|
257
259
|
classStateUser = {
|
|
258
260
|
myClass: 'user',
|
|
259
|
-
doMe: (DispatchedAction<User>),
|
|
260
261
|
nodes: {
|
|
261
262
|
[uuid-user1] : {
|
|
262
263
|
id: uuid-user1,
|
|
@@ -280,6 +281,8 @@ classStateUser = {
|
|
|
280
281
|
}
|
|
281
282
|
}
|
|
282
283
|
}
|
|
284
|
+
defaultID,
|
|
285
|
+
defaultState,
|
|
283
286
|
}
|
|
284
287
|
```
|
|
285
288
|
|
|
@@ -287,11 +290,11 @@ classStateUser = {
|
|
|
287
290
|
|
|
288
291
|
### Basic
|
|
289
292
|
|
|
290
|
-
##### `useThunk(theDo: ThunkModuleFunc): [ClassState,
|
|
293
|
+
##### `useThunk(theDo: ThunkModuleFunc): [ClassState, DispatchedFuncMap]`
|
|
291
294
|
|
|
292
295
|
Similar to `React.useReducer`, but we use `useThunk`, and we also bind the actions with dispatch (similar concept as `mapDispatchToProps`).s
|
|
293
296
|
|
|
294
|
-
return: `[ClassState<S>,
|
|
297
|
+
return: `[ClassState<S>, DispatchedFuncMap<S, R>]`
|
|
295
298
|
|
|
296
299
|
##### `init({myID, parentID, doParent, state}, myuuidv4?)`
|
|
297
300
|
|
|
@@ -311,18 +314,23 @@ generate uuid for react-object.
|
|
|
311
314
|
|
|
312
315
|
### State
|
|
313
316
|
|
|
314
|
-
##### `getState(state: ClassState, myID?: string): State`
|
|
317
|
+
##### `getState(state: ClassState, myID?: string): State | null`
|
|
315
318
|
|
|
316
|
-
Get the state of `myID`. Get the state of `defaultID` if `myID` is not present.
|
|
319
|
+
Get the state of `myID`. Get the state of `defaultID` if `myID` is not present. Return `null` if not available.
|
|
317
320
|
|
|
318
|
-
##### `
|
|
321
|
+
##### `mustGetState(state: ClassState, myID?: string): State`
|
|
319
322
|
|
|
320
|
-
|
|
323
|
+
Get the state of `myID`. Get the state of `defaultID` if `myID` is not present. Return `defaultState` if not available.
|
|
324
|
+
|
|
325
|
+
##### `mustGetStateByThunk(theUseThunk: UseThunk, myID?: string): [State, DispatchedActionMap, theID]`
|
|
321
326
|
|
|
322
|
-
|
|
327
|
+
Get the state of `myID` by `UseThunk`. Get the state of `defaultID` if `myID` is not present. Return `defaultState` if not available.
|
|
323
328
|
|
|
324
|
-
|
|
325
|
-
|
|
329
|
+
return: `[S, DispatchedFuncMap<S, R>, theID]`
|
|
330
|
+
|
|
331
|
+
##### `getDefaultID(state: ClassState): string`
|
|
332
|
+
|
|
333
|
+
get the default id.
|
|
326
334
|
|
|
327
335
|
### NodeState
|
|
328
336
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chhsiao1981/use-thunk",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A framework easily using useThunk to manage the data-state.",
|
|
6
6
|
"homepage": "https://github.com/chhsiao1981/use-thunk",
|
|
@@ -58,6 +58,6 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"react": ">=18.3.1",
|
|
61
|
-
"uuid": "^
|
|
61
|
+
"uuid": "^14.0.0"
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/ThunkContext.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type JSX, useMemo, useState } from 'react'
|
|
2
|
-
import type { ClassState } from './stateTypes'
|
|
3
2
|
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
4
3
|
|
|
5
4
|
type Props = {
|
|
@@ -21,8 +20,7 @@ const ThunkContext = (props: Props): JSX.Element => {
|
|
|
21
20
|
const { context: Context_m, refClassState } = THUNK_CONTEXT_MAP.theMap[theClass]
|
|
22
21
|
|
|
23
22
|
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
24
|
-
|
|
25
|
-
const [classState, setClassState] = useState<ClassState<any>>({ myClass: theClass, nodes: {} })
|
|
23
|
+
const [classState, setClassState] = useState(refClassState.current)
|
|
26
24
|
|
|
27
25
|
refClassState.current = classState
|
|
28
26
|
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
package/src/index.ts
CHANGED
|
@@ -7,8 +7,8 @@ import registerThunk from './registerThunk'
|
|
|
7
7
|
import { remove } from './remove'
|
|
8
8
|
import { setData } from './setData'
|
|
9
9
|
import { setDefaultID } from './setDefaultID'
|
|
10
|
-
import { getDefaultID, getNode,
|
|
11
|
-
import type { ClassState,
|
|
10
|
+
import { getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk } from './states'
|
|
11
|
+
import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes'
|
|
12
12
|
import ThunkContext from './ThunkContext'
|
|
13
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunk'
|
|
14
14
|
import useThunk, { type UseThunk } from './useThunk'
|
|
@@ -20,7 +20,6 @@ export {
|
|
|
20
20
|
type UseThunk,
|
|
21
21
|
type State,
|
|
22
22
|
type NodeState,
|
|
23
|
-
type NodeMeta,
|
|
24
23
|
type NodeStateMap,
|
|
25
24
|
// type NodeStateMapByClass, // XXX for global state
|
|
26
25
|
type ClassState,
|
|
@@ -37,9 +36,10 @@ export {
|
|
|
37
36
|
type DispatchFuncMap,
|
|
38
37
|
// type DefaultDispatchFuncMap, // XXX deemphasize default
|
|
39
38
|
getDefaultID,
|
|
40
|
-
getRootID,
|
|
41
39
|
getNode,
|
|
42
40
|
getState,
|
|
41
|
+
mustGetState,
|
|
42
|
+
mustGetStateByThunk,
|
|
43
43
|
init,
|
|
44
44
|
type InitParams,
|
|
45
45
|
setData,
|
package/src/registerThunk.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { ThunkModule, ThunkModuleFunc } from './thunk'
|
|
|
4
4
|
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
5
5
|
|
|
6
6
|
export default <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S, R>) => {
|
|
7
|
-
const { myClass } = theDo
|
|
7
|
+
const { myClass, defaultState } = theDo
|
|
8
8
|
|
|
9
9
|
if (THUNK_CONTEXT_MAP.theMap[myClass]) {
|
|
10
10
|
// already init
|
|
@@ -15,6 +15,7 @@ export default <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModul
|
|
|
15
15
|
const classState: ClassState<S> = {
|
|
16
16
|
myClass,
|
|
17
17
|
nodes: {},
|
|
18
|
+
defaultState,
|
|
18
19
|
}
|
|
19
20
|
const setClassState: Dispatch<SetStateAction<ClassState<S>>> = () => {}
|
|
20
21
|
const refClassState = { current: classState }
|
package/src/stateTypes.ts
CHANGED
|
@@ -13,24 +13,10 @@ export type NodeStateMap<S extends State> = {
|
|
|
13
13
|
[key: string]: NodeState<S>
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export type NodeStateMapByClass<S extends State> = {
|
|
17
|
-
[className: string]: NodeStateMap<S>
|
|
18
|
-
}
|
|
19
|
-
|
|
20
16
|
// ClassState
|
|
21
17
|
export type ClassState<S extends State> = {
|
|
22
18
|
myClass: string
|
|
23
19
|
defaultID?: string | null
|
|
24
|
-
// XXX doMe is a hidden variable for ClassState
|
|
25
|
-
// used only for parents / children / links.
|
|
26
|
-
// doMe: DispatchFuncMap
|
|
27
20
|
nodes: NodeStateMap<S>
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// Node
|
|
31
|
-
export type NodeMeta = {
|
|
32
|
-
id: string
|
|
33
|
-
theClass: string
|
|
34
|
-
// @ts-expect-error do can be any type.
|
|
35
|
-
do: DispatchFuncMap
|
|
21
|
+
defaultState: S
|
|
36
22
|
}
|
package/src/states.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
+
import type { DispatchFuncMap } from './dispatchFuncMap'
|
|
1
2
|
import type { ClassState, NodeState, State } from './stateTypes'
|
|
3
|
+
import type { ThunkModuleFunc } from './thunk'
|
|
4
|
+
import type { UseThunk } from './useThunk'
|
|
2
5
|
|
|
3
6
|
export const getDefaultID = <S extends State>(classState: ClassState<S>): string => {
|
|
4
7
|
return classState.defaultID ?? ''
|
|
5
8
|
}
|
|
6
9
|
|
|
7
|
-
export const getRootID = <S extends State>(classState: ClassState<S>): string => {
|
|
8
|
-
console.warn('[DEPRECATE] getRootID will be deprecated in 10.2.0.')
|
|
9
|
-
return getDefaultID(classState)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
10
|
export const getNode = <S extends State>(
|
|
13
11
|
classState: ClassState<S>,
|
|
14
12
|
myID?: string,
|
|
@@ -33,3 +31,17 @@ export const getState = <S extends State>(classState: ClassState<S>, myID?: stri
|
|
|
33
31
|
}
|
|
34
32
|
return me.state
|
|
35
33
|
}
|
|
34
|
+
|
|
35
|
+
export const mustGetState = <S extends State>(classState: ClassState<S>, myID?: string): S => {
|
|
36
|
+
return getState(classState, myID) || classState.defaultState
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const mustGetStateByThunk = <S extends State, R extends ThunkModuleFunc<S>>(
|
|
40
|
+
theUseThunk: UseThunk<S, R>,
|
|
41
|
+
myID?: string,
|
|
42
|
+
): [S, DispatchFuncMap<S, R>, string] => {
|
|
43
|
+
const [classState, theDo] = theUseThunk
|
|
44
|
+
const theID = myID ? myID : getDefaultID(classState)
|
|
45
|
+
const state = mustGetState(classState, theID)
|
|
46
|
+
return [state, theDo, theID]
|
|
47
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -7,9 +7,9 @@ import registerThunk from './registerThunk';
|
|
|
7
7
|
import { remove } from './remove';
|
|
8
8
|
import { setData } from './setData';
|
|
9
9
|
import { setDefaultID } from './setDefaultID';
|
|
10
|
-
import { getDefaultID, getNode,
|
|
11
|
-
import type { ClassState,
|
|
10
|
+
import { getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk } from './states';
|
|
11
|
+
import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes';
|
|
12
12
|
import ThunkContext from './ThunkContext';
|
|
13
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunk';
|
|
14
14
|
import useThunk, { type UseThunk } from './useThunk';
|
|
15
|
-
export { registerThunk, useThunk, ThunkContext, type UseThunk, type State, type NodeState, type
|
|
15
|
+
export { registerThunk, useThunk, ThunkContext, type UseThunk, type State, type NodeState, type NodeStateMap, type ClassState, type GetClassState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type Dispatch, type DispatchFuncMap, getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk, init, type InitParams, setData, remove, setDefaultID, genUUID, };
|
package/types/stateTypes.d.ts
CHANGED
|
@@ -8,16 +8,9 @@ export type NodeState<S extends State> = {
|
|
|
8
8
|
export type NodeStateMap<S extends State> = {
|
|
9
9
|
[key: string]: NodeState<S>;
|
|
10
10
|
};
|
|
11
|
-
export type NodeStateMapByClass<S extends State> = {
|
|
12
|
-
[className: string]: NodeStateMap<S>;
|
|
13
|
-
};
|
|
14
11
|
export type ClassState<S extends State> = {
|
|
15
12
|
myClass: string;
|
|
16
13
|
defaultID?: string | null;
|
|
17
14
|
nodes: NodeStateMap<S>;
|
|
18
|
-
|
|
19
|
-
export type NodeMeta = {
|
|
20
|
-
id: string;
|
|
21
|
-
theClass: string;
|
|
22
|
-
do: DispatchFuncMap;
|
|
15
|
+
defaultState: S;
|
|
23
16
|
};
|
package/types/states.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import type { DispatchFuncMap } from './dispatchFuncMap';
|
|
1
2
|
import type { ClassState, NodeState, State } from './stateTypes';
|
|
3
|
+
import type { ThunkModuleFunc } from './thunk';
|
|
4
|
+
import type { UseThunk } from './useThunk';
|
|
2
5
|
export declare const getDefaultID: <S extends State>(classState: ClassState<S>) => string;
|
|
3
|
-
export declare const getRootID: <S extends State>(classState: ClassState<S>) => string;
|
|
4
6
|
export declare const getNode: <S extends State>(classState: ClassState<S>, myID?: string) => NodeState<S> | null;
|
|
5
7
|
export declare const getState: <S extends State>(classState: ClassState<S>, myID?: string) => S | null;
|
|
8
|
+
export declare const mustGetState: <S extends State>(classState: ClassState<S>, myID?: string) => S;
|
|
9
|
+
export declare const mustGetStateByThunk: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, DispatchFuncMap<S, R>, string];
|