@chhsiao1981/use-thunk 9.0.3 → 9.0.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chhsiao1981/use-thunk",
3
- "version": "9.0.3",
3
+ "version": "9.0.4",
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",
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 './thunkReducer'
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<ClassState<S>, BaseAction>
11
+ export type Thunk<S extends State> = rThunk<S, BaseAction>
12
12
 
13
- export type ActionOrThunk<S extends State> = rActionOrThunk<ClassState<S>, BaseAction>
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/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 './thunkReducer'
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 [state, dispatch] = useThunkReducer(
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 [state, dispatchMap]
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 [state, dispatchMap]
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
- getState: () => S,
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, A>,
27
- initArg: S,
28
- // biome-ignore lint/suspicious/noExplicitAny: params can by any types.
29
- init?: (...params: any[]) => S,
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 initState = init ? () => init(initArg) : initArg
31
+ const initClassState = init ? () => init(initArg) : initArg
33
32
 
34
33
  // 2. renderState
35
- const [renderState, setRenderState] = useState(initState)
34
+ const [renderClassState, setRenderClassState] = useState(initClassState)
36
35
 
37
36
  // 3. hookState
38
- const hookState = renderState
37
+ const hookClassState = renderClassState
39
38
 
40
39
  // 4. state management.
41
- const state = useRef(hookState)
42
- const getState = useCallback(() => state.current, [state])
43
- const setState = useCallback(
44
- (newState: S) => {
45
- state.current = newState
46
- setRenderState(newState)
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
- [state, setRenderState],
47
+ [classState, setRenderClassState],
49
48
  )
50
49
 
51
50
  // 5. reducer.
52
51
  const reduce = useCallback(
53
- (action: A): S => {
54
- return reducer(getState(), action)
52
+ (action: A): ClassState<S> => {
53
+ return reducer(getClassState(), action)
55
54
  },
56
- [reducer, getState],
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, getState)
62
+ action(dispatch, getClassState)
64
63
  return
65
64
  }
66
65
 
67
- setState(reduce(action))
66
+ setClassState(reduce(action))
68
67
  },
69
- [getState, setState, reduce],
68
+ [getClassState, setClassState, reduce],
70
69
  )
71
70
 
72
- return [hookState, dispatch]
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 './thunkReducer';
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<ClassState<S>, BaseAction>;
9
- export type ActionOrThunk<S extends State> = rActionOrThunk<ClassState<S>, BaseAction>;
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>;
@@ -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;