@chhsiao1981/use-thunk 9.0.3 → 9.0.5
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/package.json +1 -1
- package/src/action.ts +3 -3
- package/src/setData.ts +1 -1
- package/src/useThunk.ts +4 -4
- package/src/{thunkReducer.ts → useThunkReducer.ts} +23 -24
- package/types/action.d.ts +3 -3
- package/types/setData.d.ts +1 -1
- package/types/thunkModuleFuncMap.d.ts +1 -1
- package/types/useThunkReducer.d.ts +18 -0
package/package.json
CHANGED
package/src/action.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ClassState, State } from './stateTypes'
|
|
2
|
-
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './
|
|
2
|
+
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './useThunkReducer'
|
|
3
3
|
|
|
4
4
|
// BaseAction contains only object-based actions, no thunk-based actions.
|
|
5
5
|
export interface BaseAction {
|
|
@@ -8,9 +8,9 @@ export interface BaseAction {
|
|
|
8
8
|
[key: string]: unknown
|
|
9
9
|
}
|
|
10
10
|
// Thunk
|
|
11
|
-
export type Thunk<S extends State> = rThunk<
|
|
11
|
+
export type Thunk<S extends State> = rThunk<S, BaseAction>
|
|
12
12
|
|
|
13
|
-
export type ActionOrThunk<S extends State> = rActionOrThunk<
|
|
13
|
+
export type ActionOrThunk<S extends State> = rActionOrThunk<S, BaseAction>
|
|
14
14
|
|
|
15
15
|
// ActionFunc
|
|
16
16
|
// biome-ignore lint/suspicious/noExplicitAny: unknown requires same type in list. use any for possible different types.
|
package/src/setData.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { BaseAction } from './action'
|
|
|
2
2
|
import type { ClassState, State } from './stateTypes'
|
|
3
3
|
|
|
4
4
|
export const SET_DATA = '@chhsiao1981/use-thunk/SET_DATA'
|
|
5
|
-
export const setData = <S extends State>(myID: string, data: S): BaseAction => ({
|
|
5
|
+
export const setData = <S extends State>(myID: string, data: Partial<S>): BaseAction => ({
|
|
6
6
|
myID,
|
|
7
7
|
type: SET_DATA,
|
|
8
8
|
data,
|
package/src/useThunk.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { DispatchFuncMap, DispatchFuncMapByClassMap, RefDispatchFuncMapByCl
|
|
|
5
5
|
import type { ClassState, NodeStateMap, State } from './stateTypes'
|
|
6
6
|
import type { ThunkModule, ThunkModuleFunc } from './thunk'
|
|
7
7
|
import { DEFAULT_THUNK_MODULE_FUNC_MAP } from './thunkModuleFuncMap'
|
|
8
|
-
import useThunkReducer from './
|
|
8
|
+
import useThunkReducer from './useThunkReducer'
|
|
9
9
|
|
|
10
10
|
export type UseThunk<S extends State, R extends ThunkModuleFunc<S>> = [ClassState<S>, DispatchFuncMap<S, R>]
|
|
11
11
|
|
|
@@ -47,7 +47,7 @@ export default <S extends State, R extends ThunkModuleFunc<S>>(
|
|
|
47
47
|
const [theReducer, _] = useState(() => theDo.default ?? createReducer<S>())
|
|
48
48
|
|
|
49
49
|
// 5. useThunkReducer
|
|
50
|
-
const [
|
|
50
|
+
const [classState, dispatch] = useThunkReducer(
|
|
51
51
|
theReducer,
|
|
52
52
|
{
|
|
53
53
|
myClass,
|
|
@@ -61,7 +61,7 @@ export default <S extends State, R extends ThunkModuleFunc<S>>(
|
|
|
61
61
|
// 7. the dispatchMap is always the same.
|
|
62
62
|
// we can do early return if not first time.
|
|
63
63
|
if (!isFirstTime) {
|
|
64
|
-
return [
|
|
64
|
+
return [classState, dispatchMap]
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
// 8. setup dispatchMap
|
|
@@ -91,5 +91,5 @@ export default <S extends State, R extends ThunkModuleFunc<S>>(
|
|
|
91
91
|
return val
|
|
92
92
|
}, dispatchMap)
|
|
93
93
|
|
|
94
|
-
return [
|
|
94
|
+
return [classState, dispatchMap]
|
|
95
95
|
}
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
import { type Dispatch, type Reducer, useCallback, useRef, useState } from 'react'
|
|
5
5
|
import type { BaseAction } from './action'
|
|
6
|
-
import type { State } from './stateTypes'
|
|
6
|
+
import type { ClassState, State } from './stateTypes'
|
|
7
7
|
|
|
8
8
|
export type Thunk<S extends State, A extends BaseAction> = (
|
|
9
9
|
dispatch: Dispatch<ActionOrThunk<S, A>>,
|
|
10
|
-
|
|
10
|
+
getClassState: () => ClassState<S>,
|
|
11
11
|
) => void
|
|
12
12
|
|
|
13
13
|
export type ActionOrThunk<S extends State, A extends BaseAction> = A | Thunk<S, A>
|
|
@@ -23,51 +23,50 @@ export type ActionOrThunk<S extends State, A extends BaseAction> = A | Thunk<S,
|
|
|
23
23
|
* @returns {[State, Dispatch]}
|
|
24
24
|
*/
|
|
25
25
|
export default <S extends State, A extends BaseAction>(
|
|
26
|
-
reducer: Reducer<S
|
|
27
|
-
initArg: S
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
): [S, Dispatch<A | Thunk<S, A>>] => {
|
|
26
|
+
reducer: Reducer<ClassState<S>, A>,
|
|
27
|
+
initArg: ClassState<S>,
|
|
28
|
+
init?: (initArg: ClassState<S>) => ClassState<S>,
|
|
29
|
+
): [ClassState<S>, Dispatch<A | Thunk<S, A>>] => {
|
|
31
30
|
// 1. initState
|
|
32
|
-
const
|
|
31
|
+
const initClassState = init ? () => init(initArg) : initArg
|
|
33
32
|
|
|
34
33
|
// 2. renderState
|
|
35
|
-
const [
|
|
34
|
+
const [renderClassState, setRenderClassState] = useState(initClassState)
|
|
36
35
|
|
|
37
36
|
// 3. hookState
|
|
38
|
-
const
|
|
37
|
+
const hookClassState = renderClassState
|
|
39
38
|
|
|
40
39
|
// 4. state management.
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
(
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
const classState = useRef(hookClassState)
|
|
41
|
+
const getClassState = useCallback(() => classState.current, [classState])
|
|
42
|
+
const setClassState = useCallback(
|
|
43
|
+
(newClassState: ClassState<S>) => {
|
|
44
|
+
classState.current = newClassState
|
|
45
|
+
setRenderClassState(newClassState)
|
|
47
46
|
},
|
|
48
|
-
[
|
|
47
|
+
[classState, setRenderClassState],
|
|
49
48
|
)
|
|
50
49
|
|
|
51
50
|
// 5. reducer.
|
|
52
51
|
const reduce = useCallback(
|
|
53
|
-
(action: A): S => {
|
|
54
|
-
return reducer(
|
|
52
|
+
(action: A): ClassState<S> => {
|
|
53
|
+
return reducer(getClassState(), action)
|
|
55
54
|
},
|
|
56
|
-
[reducer,
|
|
55
|
+
[reducer, getClassState],
|
|
57
56
|
)
|
|
58
57
|
|
|
59
58
|
// augmented dispatcher.
|
|
60
59
|
const dispatch = useCallback(
|
|
61
60
|
(action: A | Thunk<S, A>) => {
|
|
62
61
|
if (typeof action === 'function') {
|
|
63
|
-
action(dispatch,
|
|
62
|
+
action(dispatch, getClassState)
|
|
64
63
|
return
|
|
65
64
|
}
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
setClassState(reduce(action))
|
|
68
67
|
},
|
|
69
|
-
[
|
|
68
|
+
[getClassState, setClassState, reduce],
|
|
70
69
|
)
|
|
71
70
|
|
|
72
|
-
return [
|
|
71
|
+
return [hookClassState, dispatch]
|
|
73
72
|
}
|
package/types/action.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { ClassState, State } from './stateTypes';
|
|
2
|
-
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './
|
|
2
|
+
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './useThunkReducer';
|
|
3
3
|
export interface BaseAction {
|
|
4
4
|
myID: string;
|
|
5
5
|
type: string;
|
|
6
6
|
[key: string]: unknown;
|
|
7
7
|
}
|
|
8
|
-
export type Thunk<S extends State> = rThunk<
|
|
9
|
-
export type ActionOrThunk<S extends State> = rActionOrThunk<
|
|
8
|
+
export type Thunk<S extends State> = rThunk<S, BaseAction>;
|
|
9
|
+
export type ActionOrThunk<S extends State> = rActionOrThunk<S, BaseAction>;
|
|
10
10
|
export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>;
|
|
11
11
|
export type GetClassState<S extends State> = () => ClassState<S>;
|
package/types/setData.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BaseAction } from './action';
|
|
2
2
|
import type { ClassState, State } from './stateTypes';
|
|
3
3
|
export declare const SET_DATA = "@chhsiao1981/use-thunk/SET_DATA";
|
|
4
|
-
export declare const setData: <S extends State>(myID: string, data: S) => BaseAction;
|
|
4
|
+
export declare const setData: <S extends State>(myID: string, data: Partial<S>) => BaseAction;
|
|
5
5
|
export declare const reduceSetData: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const DEFAULT_THUNK_MODULE_FUNC_MAP: {
|
|
2
2
|
init: <S extends import("./stateTypes").State>(params: import("./init").InitParams<S>, myuuidv4?: () => string) => import("./action").Thunk<S>;
|
|
3
|
-
setData: <S extends import("./stateTypes").State>(myID: string, data: S) => import("./action").BaseAction;
|
|
3
|
+
setData: <S extends import("./stateTypes").State>(myID: string, data: Partial<S>) => import("./action").BaseAction;
|
|
4
4
|
remove: <S extends import("./stateTypes").State>(myID: string, isFromParent?: boolean) => import("./action").Thunk<S>;
|
|
5
5
|
addChild: (myID: string, child: import("./stateTypes").NodeMeta) => import("./addRelation").AddRelationAction;
|
|
6
6
|
removeChild: <S extends import("./stateTypes").State>(myID: string, childID: string, childClass: string, isFromChild?: boolean) => import("./action").Thunk<S>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Dispatch, type Reducer } from 'react';
|
|
2
|
+
import type { BaseAction } from './action';
|
|
3
|
+
import type { ClassState, State } from './stateTypes';
|
|
4
|
+
export type Thunk<S extends State, A extends BaseAction> = (dispatch: Dispatch<ActionOrThunk<S, A>>, getClassState: () => ClassState<S>) => void;
|
|
5
|
+
export type ActionOrThunk<S extends State, A extends BaseAction> = A | Thunk<S, A>;
|
|
6
|
+
/**
|
|
7
|
+
* useThunkReducer
|
|
8
|
+
*
|
|
9
|
+
* Augments React's useReducer() hook so that the action
|
|
10
|
+
* dispatcher supports thunks.
|
|
11
|
+
*
|
|
12
|
+
* @param {Function} reducer
|
|
13
|
+
* @param {State} initArg
|
|
14
|
+
* @param {Function} [init]
|
|
15
|
+
* @returns {[State, Dispatch]}
|
|
16
|
+
*/
|
|
17
|
+
declare const _default: <S extends State, A extends BaseAction>(reducer: Reducer<ClassState<S>, A>, initArg: ClassState<S>, init?: (initArg: ClassState<S>) => ClassState<S>) => [ClassState<S>, Dispatch<A | Thunk<S, A>>];
|
|
18
|
+
export default _default;
|