@chhsiao1981/use-thunk 10.0.4 → 10.1.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 +13 -8
- package/package.json +1 -1
- package/src/ThunkContext.tsx +1 -1
- package/src/index.ts +4 -1
- package/src/init.ts +4 -4
- package/src/reduceMap.ts +4 -4
- package/src/remove.ts +3 -3
- package/src/setDefaultID.ts +17 -0
- package/src/stateTypes.ts +1 -1
- package/src/states.ts +12 -25
- package/types/index.d.ts +3 -2
- package/types/setDefaultID.d.ts +5 -0
- package/types/stateTypes.d.ts +1 -1
- package/types/states.d.ts +1 -0
- package/src/setRoot.ts +0 -17
- package/types/setRoot.d.ts +0 -5
package/README.md
CHANGED
|
@@ -67,7 +67,7 @@ export const increment = (myID: string): Thunk<State> => {
|
|
|
67
67
|
App.tsx:
|
|
68
68
|
|
|
69
69
|
```tsx
|
|
70
|
-
import { type ThunkModuleToFunc, useThunk,
|
|
70
|
+
import { type ThunkModuleToFunc, useThunk, getDefaultID, getState } from '@chhsiao1981/use-thunk'
|
|
71
71
|
import * as DoIncrement from './reducers/increment'
|
|
72
72
|
|
|
73
73
|
type TDoIncrement = ThunkModuleToFunc(typeof DoIncrement)
|
|
@@ -84,7 +84,7 @@ export default (props: Props) => {
|
|
|
84
84
|
}, [])
|
|
85
85
|
|
|
86
86
|
// states
|
|
87
|
-
const incrementID =
|
|
87
|
+
const incrementID = getDefaultID(classStateIncrement)
|
|
88
88
|
const increment = getState(classStateIncrement) || DoIncrement.defaultState
|
|
89
89
|
|
|
90
90
|
// to render
|
|
@@ -136,7 +136,7 @@ export interface State extends rState {
|
|
|
136
136
|
### Must Included in a Top-level Component
|
|
137
137
|
|
|
138
138
|
```ts
|
|
139
|
-
import { type ThunkModuleToFunc, useThunk,
|
|
139
|
+
import { type ThunkModuleToFunc, useThunk, getDefaultID, getState } from '@chhsiao1981/use-thunk'
|
|
140
140
|
import * as DoModule from '../reducers/module'
|
|
141
141
|
|
|
142
142
|
type TDoModule = ThunkModuleToFunc<typeof DoModule>
|
|
@@ -144,7 +144,7 @@ type TDoModule = ThunkModuleToFunc<typeof DoModule>
|
|
|
144
144
|
const Component = () => {
|
|
145
145
|
const [stateModule, doModule] = useThunk<DoModule.State, TDoModule>(DoModule)
|
|
146
146
|
|
|
147
|
-
const moduleID =
|
|
147
|
+
const moduleID = getDefaultID(stateModule)
|
|
148
148
|
const theModule = getState(stateModule)
|
|
149
149
|
|
|
150
150
|
.
|
|
@@ -175,7 +175,7 @@ createRoot(document.getElementById("root")!).render(
|
|
|
175
175
|
The general concept of normalized state can be found in [Normalizing State Shape](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape)
|
|
176
176
|
with the following features:
|
|
177
177
|
|
|
178
|
-
1. ClassState: the state of the class, including the nodes and the
|
|
178
|
+
1. ClassState: the state of the class, including the nodes and the defaultID of the class.
|
|
179
179
|
2. NodeState: the state of a node, including the id of the node and the content (state) of the node.
|
|
180
180
|
3. State: the content of the node, represented as a state.
|
|
181
181
|
|
|
@@ -313,14 +313,19 @@ generate uuid for react-object.
|
|
|
313
313
|
|
|
314
314
|
##### `getState(state: ClassState, myID?: string): State`
|
|
315
315
|
|
|
316
|
-
Get the state of `myID`. Get the state of `
|
|
316
|
+
Get the state of `myID`. Get the state of `defaultID` if `myID` is not present.
|
|
317
|
+
|
|
318
|
+
##### `getDefaultID(state: ClassState): string`
|
|
319
|
+
|
|
320
|
+
get the default id.
|
|
317
321
|
|
|
318
322
|
##### `getRootID(state: ClassState): string`
|
|
319
323
|
|
|
320
|
-
|
|
324
|
+
(to be deprecated in 10.2.0.)
|
|
325
|
+
alias of `getDefaultID` for compatibility.
|
|
321
326
|
|
|
322
327
|
### NodeState
|
|
323
328
|
|
|
324
329
|
##### `getNode(state: ClassState, myID?: string): NodeState`
|
|
325
330
|
|
|
326
|
-
Get the node of `myID`. Get the node of `
|
|
331
|
+
Get the node of `myID`. Get the node of `defaultID` if `myID` is not present.
|
package/package.json
CHANGED
package/src/ThunkContext.tsx
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,7 +6,8 @@ import { type InitParams, init } from './init'
|
|
|
6
6
|
import registerThunk from './registerThunk'
|
|
7
7
|
import { remove } from './remove'
|
|
8
8
|
import { setData } from './setData'
|
|
9
|
-
import {
|
|
9
|
+
import { setDefaultID } from './setDefaultID'
|
|
10
|
+
import { getDefaultID, getNode, getRootID, getState } from './states'
|
|
10
11
|
import type { ClassState, NodeMeta, NodeState, NodeStateMap, State } from './stateTypes'
|
|
11
12
|
import ThunkContext from './ThunkContext'
|
|
12
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunk'
|
|
@@ -35,6 +36,7 @@ export {
|
|
|
35
36
|
type Dispatch,
|
|
36
37
|
type DispatchFuncMap,
|
|
37
38
|
// type DefaultDispatchFuncMap, // XXX deemphasize default
|
|
39
|
+
getDefaultID,
|
|
38
40
|
getRootID,
|
|
39
41
|
getNode,
|
|
40
42
|
getState,
|
|
@@ -45,5 +47,6 @@ export {
|
|
|
45
47
|
// type DefaultThunkModuleFuncMap as DefaultReducerModuleFuncMap, // XXX deemphasize default
|
|
46
48
|
// type ReduceMap, // XXX deemphasize reducer
|
|
47
49
|
// createReducer, // XXX deemphasize reducer
|
|
50
|
+
setDefaultID,
|
|
48
51
|
genUUID,
|
|
49
52
|
}
|
package/src/init.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BaseAction, Thunk } from './action'
|
|
2
2
|
import { genUUID } from './genUUID'
|
|
3
|
-
import {
|
|
3
|
+
import { setDefaultID } from './setDefaultID'
|
|
4
4
|
import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes'
|
|
5
5
|
|
|
6
6
|
// InitParams
|
|
@@ -21,10 +21,10 @@ export const init = <S extends State>(params: InitParams<S>, myuuidv4?: () => st
|
|
|
21
21
|
const { state } = params
|
|
22
22
|
dispatch(initCore(myID, state))
|
|
23
23
|
|
|
24
|
-
const {
|
|
24
|
+
const { defaultID } = getClassState()
|
|
25
25
|
|
|
26
|
-
if (!
|
|
27
|
-
dispatch(
|
|
26
|
+
if (!defaultID) {
|
|
27
|
+
dispatch(setDefaultID(myID))
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
}
|
package/src/reduceMap.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { INIT, reduceInit } from './init'
|
|
|
2
2
|
import type { ReduceFunc } from './reducer'
|
|
3
3
|
import { REMOVE, reduceRemove } from './remove'
|
|
4
4
|
import { reduceSetData, SET_DATA } from './setData'
|
|
5
|
-
import {
|
|
5
|
+
import { reduceSetDefaultID, SET_DEFAULT_ID } from './setDefaultID'
|
|
6
6
|
import type { State } from './stateTypes'
|
|
7
7
|
|
|
8
8
|
export interface ReduceMap<S extends State> {
|
|
@@ -16,8 +16,8 @@ export const DEFAULT_REDUCE_MAP: <S extends State>() => ReduceMap<S> = () => ({
|
|
|
16
16
|
[SET_DATA]: reduceSetData,
|
|
17
17
|
[REMOVE]: reduceRemove,
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// setDefaultID.
|
|
20
20
|
// Typically we don't need this in programming.
|
|
21
|
-
// The
|
|
22
|
-
[
|
|
21
|
+
// The defaultID is automatically determined if defaultID is not set.
|
|
22
|
+
[SET_DEFAULT_ID]: reduceSetDefaultID,
|
|
23
23
|
})
|
package/src/remove.ts
CHANGED
|
@@ -40,10 +40,10 @@ export const reduceRemove = <S extends State>(
|
|
|
40
40
|
return r
|
|
41
41
|
}, {})
|
|
42
42
|
|
|
43
|
-
//
|
|
43
|
+
// defaultID
|
|
44
44
|
const newState = Object.assign({}, classState, { nodes: newNodes })
|
|
45
|
-
if (newState.
|
|
46
|
-
newState.
|
|
45
|
+
if (newState.defaultID === myID) {
|
|
46
|
+
newState.defaultID = null
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
return newState
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BaseAction } from './action'
|
|
2
|
+
import type { ClassState, State } from './stateTypes'
|
|
3
|
+
|
|
4
|
+
export const SET_DEFAULT_ID = '@chhsiao1981/use-thunk/SET_DEFAULT_ID'
|
|
5
|
+
export const setDefaultID = (myID: string): BaseAction => ({
|
|
6
|
+
myID,
|
|
7
|
+
type: SET_DEFAULT_ID,
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
export const reduceSetDefaultID = <S extends State>(
|
|
11
|
+
classState: ClassState<S>,
|
|
12
|
+
action: BaseAction,
|
|
13
|
+
): ClassState<S> => {
|
|
14
|
+
const { myID } = action
|
|
15
|
+
|
|
16
|
+
return Object.assign({}, classState, { defaultID: myID })
|
|
17
|
+
}
|
package/src/stateTypes.ts
CHANGED
|
@@ -20,7 +20,7 @@ export type NodeStateMapByClass<S extends State> = {
|
|
|
20
20
|
// ClassState
|
|
21
21
|
export type ClassState<S extends State> = {
|
|
22
22
|
myClass: string
|
|
23
|
-
|
|
23
|
+
defaultID?: string | null
|
|
24
24
|
// XXX doMe is a hidden variable for ClassState
|
|
25
25
|
// used only for parents / children / links.
|
|
26
26
|
// doMe: DispatchFuncMap
|
package/src/states.ts
CHANGED
|
@@ -1,46 +1,33 @@
|
|
|
1
1
|
import type { ClassState, NodeState, State } from './stateTypes'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
if (!root) {
|
|
6
|
-
return null
|
|
7
|
-
}
|
|
8
|
-
return classState.nodes[root] || null
|
|
3
|
+
export const getDefaultID = <S extends State>(classState: ClassState<S>): string => {
|
|
4
|
+
return classState.defaultID ?? ''
|
|
9
5
|
}
|
|
10
6
|
|
|
11
7
|
export const getRootID = <S extends State>(classState: ClassState<S>): string => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const getRoot = <S extends State>(classState: ClassState<S>): S | null => {
|
|
16
|
-
const root = classState.root
|
|
17
|
-
if (!root) {
|
|
18
|
-
return null
|
|
19
|
-
}
|
|
20
|
-
const me = classState.nodes[root]
|
|
21
|
-
if (!me) {
|
|
22
|
-
return null
|
|
23
|
-
}
|
|
24
|
-
return me.state
|
|
8
|
+
console.warn('[DEPRECATE] getRootID will be deprecated in 10.2.0.')
|
|
9
|
+
return getDefaultID(classState)
|
|
25
10
|
}
|
|
26
11
|
|
|
27
12
|
export const getNode = <S extends State>(
|
|
28
13
|
classState: ClassState<S>,
|
|
29
14
|
myID?: string,
|
|
30
15
|
): NodeState<S> | null => {
|
|
31
|
-
|
|
32
|
-
|
|
16
|
+
const theID = myID ? myID : getDefaultID(classState)
|
|
17
|
+
if (!theID) {
|
|
18
|
+
return null
|
|
33
19
|
}
|
|
34
20
|
|
|
35
|
-
return classState.nodes[
|
|
21
|
+
return classState.nodes[theID] || null
|
|
36
22
|
}
|
|
37
23
|
|
|
38
24
|
export const getState = <S extends State>(classState: ClassState<S>, myID?: string): S | null => {
|
|
39
|
-
|
|
40
|
-
|
|
25
|
+
const theID = myID ? myID : getDefaultID(classState)
|
|
26
|
+
if (!theID) {
|
|
27
|
+
return null
|
|
41
28
|
}
|
|
42
29
|
|
|
43
|
-
const me = classState.nodes[
|
|
30
|
+
const me = classState.nodes[theID]
|
|
44
31
|
if (!me) {
|
|
45
32
|
return null
|
|
46
33
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -6,9 +6,10 @@ import { type InitParams, init } from './init';
|
|
|
6
6
|
import registerThunk from './registerThunk';
|
|
7
7
|
import { remove } from './remove';
|
|
8
8
|
import { setData } from './setData';
|
|
9
|
-
import {
|
|
9
|
+
import { setDefaultID } from './setDefaultID';
|
|
10
|
+
import { getDefaultID, getNode, getRootID, getState } from './states';
|
|
10
11
|
import type { ClassState, NodeMeta, NodeState, NodeStateMap, State } from './stateTypes';
|
|
11
12
|
import ThunkContext from './ThunkContext';
|
|
12
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunk';
|
|
13
14
|
import useThunk, { type UseThunk } from './useThunk';
|
|
14
|
-
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, getRootID, getNode, getState, init, type InitParams, setData, remove, genUUID, };
|
|
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, getRootID, getNode, getState, init, type InitParams, setData, remove, setDefaultID, genUUID, };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseAction } from './action';
|
|
2
|
+
import type { ClassState, State } from './stateTypes';
|
|
3
|
+
export declare const SET_DEFAULT_ID = "@chhsiao1981/use-thunk/SET_DEFAULT_ID";
|
|
4
|
+
export declare const setDefaultID: (myID: string) => BaseAction;
|
|
5
|
+
export declare const reduceSetDefaultID: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
|
package/types/stateTypes.d.ts
CHANGED
package/types/states.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ClassState, NodeState, State } from './stateTypes';
|
|
2
|
+
export declare const getDefaultID: <S extends State>(classState: ClassState<S>) => string;
|
|
2
3
|
export declare const getRootID: <S extends State>(classState: ClassState<S>) => string;
|
|
3
4
|
export declare const getNode: <S extends State>(classState: ClassState<S>, myID?: string) => NodeState<S> | null;
|
|
4
5
|
export declare const getState: <S extends State>(classState: ClassState<S>, myID?: string) => S | null;
|
package/src/setRoot.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from './action'
|
|
2
|
-
import type { ClassState, State } from './stateTypes'
|
|
3
|
-
|
|
4
|
-
export const SET_ROOT = '@chhsiao1981/use-thunk/SET_ROOT'
|
|
5
|
-
export const setRoot = (myID: string): BaseAction => ({
|
|
6
|
-
myID,
|
|
7
|
-
type: SET_ROOT,
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
export const reduceSetRoot = <S extends State>(
|
|
11
|
-
classState: ClassState<S>,
|
|
12
|
-
action: BaseAction,
|
|
13
|
-
): ClassState<S> => {
|
|
14
|
-
const { myID } = action
|
|
15
|
-
|
|
16
|
-
return Object.assign({}, classState, { root: myID })
|
|
17
|
-
}
|
package/types/setRoot.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from './action';
|
|
2
|
-
import type { ClassState, State } from './stateTypes';
|
|
3
|
-
export declare const SET_ROOT = "@chhsiao1981/use-thunk/SET_ROOT";
|
|
4
|
-
export declare const setRoot: (myID: string) => BaseAction;
|
|
5
|
-
export declare const reduceSetRoot: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
|