@chhsiao1981/use-thunk 10.1.1 → 10.2.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 +17 -11
- package/package.json +2 -2
- package/src/ThunkContext.tsx +2 -4
- package/src/index.ts +3 -2
- package/src/registerThunk.ts +2 -1
- package/src/stateTypes.ts +1 -3
- package/src/states.ts +17 -5
- package/types/ThunkContext.d.ts +1 -1
- package/types/index.d.ts +2 -2
- package/types/stateTypes.d.ts +1 -0
- 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
|
|
|
@@ -311,18 +314,21 @@ 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
|
+
##### `getStateOrDefault(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
|
+
##### `getStateByThunk(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
|
+
##### `getDefaultID(state: ClassState): string`
|
|
330
|
+
|
|
331
|
+
get the default id.
|
|
326
332
|
|
|
327
333
|
### NodeState
|
|
328
334
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chhsiao1981/use-thunk",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.2.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,10 +1,9 @@
|
|
|
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 = {
|
|
6
5
|
classes?: string[]
|
|
7
|
-
children?: JSX.Element[]
|
|
6
|
+
children?: JSX.Element | JSX.Element[]
|
|
8
7
|
}
|
|
9
8
|
const ThunkContext = (props: Props): JSX.Element => {
|
|
10
9
|
let { classes, children } = 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,7 +7,7 @@ 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,
|
|
10
|
+
import { getDefaultID, getNode, getState, getStateByThunk, getStateOrDefault } from './states'
|
|
11
11
|
import type { ClassState, NodeMeta, NodeState, NodeStateMap, State } from './stateTypes'
|
|
12
12
|
import ThunkContext from './ThunkContext'
|
|
13
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunk'
|
|
@@ -37,9 +37,10 @@ export {
|
|
|
37
37
|
type DispatchFuncMap,
|
|
38
38
|
// type DefaultDispatchFuncMap, // XXX deemphasize default
|
|
39
39
|
getDefaultID,
|
|
40
|
-
getRootID,
|
|
41
40
|
getNode,
|
|
42
41
|
getState,
|
|
42
|
+
getStateOrDefault,
|
|
43
|
+
getStateByThunk,
|
|
43
44
|
init,
|
|
44
45
|
type InitParams,
|
|
45
46
|
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
|
@@ -21,10 +21,8 @@ export type NodeStateMapByClass<S extends State> = {
|
|
|
21
21
|
export type ClassState<S extends State> = {
|
|
22
22
|
myClass: string
|
|
23
23
|
defaultID?: string | null
|
|
24
|
-
// XXX doMe is a hidden variable for ClassState
|
|
25
|
-
// used only for parents / children / links.
|
|
26
|
-
// doMe: DispatchFuncMap
|
|
27
24
|
nodes: NodeStateMap<S>
|
|
25
|
+
defaultState: S
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
// Node
|
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 getStateOrDefault = <S extends State>(classState: ClassState<S>, myID?: string): S => {
|
|
36
|
+
return getState(classState, myID) || classState.defaultState
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const getStateByThunk = <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 = getStateOrDefault(classState, theID)
|
|
46
|
+
return [state, theDo, theID]
|
|
47
|
+
}
|
package/types/ThunkContext.d.ts
CHANGED
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,
|
|
10
|
+
import { getDefaultID, getNode, getState, getStateByThunk, getStateOrDefault } from './states';
|
|
11
11
|
import type { ClassState, NodeMeta, 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 NodeMeta, type NodeStateMap, type ClassState, type GetClassState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type Dispatch, type DispatchFuncMap, getDefaultID,
|
|
15
|
+
export { registerThunk, useThunk, ThunkContext, type UseThunk, type State, type NodeState, type NodeMeta, type NodeStateMap, type ClassState, type GetClassState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type Dispatch, type DispatchFuncMap, getDefaultID, getNode, getState, getStateOrDefault, getStateByThunk, init, type InitParams, setData, remove, setDefaultID, genUUID, };
|
package/types/stateTypes.d.ts
CHANGED
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 getStateOrDefault: <S extends State>(classState: ClassState<S>, myID?: string) => S;
|
|
9
|
+
export declare const getStateByThunk: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, DispatchFuncMap<S, R>, string];
|