@chhsiao1981/use-thunk 10.4.0 → 12.0.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.
Files changed (88) hide show
  1. package/README.md +63 -59
  2. package/dist/ThunkContext.d.ts +7 -0
  3. package/dist/createReducer.d.ts +4 -0
  4. package/dist/createThunk.d.ts +4 -0
  5. package/dist/dispatch.d.ts +4 -0
  6. package/dist/dispatchFuncMap.d.ts +18 -0
  7. package/dist/genUUID.d.ts +1 -0
  8. package/dist/index.d.ts +15 -0
  9. package/{types → dist}/init.d.ts +0 -3
  10. package/dist/reduceMap.d.ts +6 -0
  11. package/dist/reducer.d.ts +5 -0
  12. package/dist/remove.d.ts +5 -0
  13. package/dist/setDefaultID.d.ts +5 -0
  14. package/dist/stateTypes.d.ts +16 -0
  15. package/dist/states.d.ts +9 -0
  16. package/{types → dist}/thunk.d.ts +6 -3
  17. package/dist/thunkContextMap.d.ts +15 -0
  18. package/dist/thunkContextTypes.d.ts +8 -0
  19. package/{types → dist}/thunkModuleFuncMap.d.ts +1 -0
  20. package/dist/update.d.ts +6 -0
  21. package/dist/useThunk.d.ts +12 -0
  22. package/dist/useThunkReducer.d.ts +17 -0
  23. package/package.json +1 -1
  24. package/src/ThunkContext.tsx +17 -17
  25. package/src/action/ActionOrThunk.ts +5 -0
  26. package/src/action/baseAction.ts +6 -0
  27. package/src/action/index.ts +16 -0
  28. package/src/action/thunk.ts +9 -0
  29. package/src/createReducer.ts +7 -7
  30. package/src/createThunk.ts +24 -0
  31. package/src/get.ts +3 -0
  32. package/src/index.ts +20 -30
  33. package/src/init/index.ts +26 -0
  34. package/src/init/initCore.ts +31 -0
  35. package/src/reduceMap.ts +3 -4
  36. package/src/reducer.ts +4 -4
  37. package/src/remove.ts +10 -25
  38. package/src/set.ts +7 -0
  39. package/src/setDefaultID.ts +5 -5
  40. package/src/setMap.ts +65 -0
  41. package/src/stateTypes.ts +3 -3
  42. package/src/states.ts +21 -18
  43. package/src/thunkContextMap.ts +4 -4
  44. package/src/thunkContextTypes.ts +4 -4
  45. package/src/thunkModule/defaultThunkModuleFuncMap.ts +15 -0
  46. package/src/thunkModule/index.ts +20 -0
  47. package/src/update.ts +26 -0
  48. package/src/useThunk.ts +23 -44
  49. package/src/useThunkReducer.ts +41 -42
  50. package/types/ThunkContext.d.ts +4 -4
  51. package/types/action/ActionOrThunk.d.ts +4 -0
  52. package/types/action/baseAction.d.ts +5 -0
  53. package/types/action/index.d.ts +8 -0
  54. package/types/action/thunk.d.ts +3 -0
  55. package/types/createThunk.d.ts +4 -0
  56. package/types/dispatch.d.ts +1 -1
  57. package/types/dispatchFuncMap.d.ts +4 -5
  58. package/types/get.d.ts +2 -0
  59. package/types/index.d.ts +10 -9
  60. package/types/init/index.d.ts +7 -0
  61. package/types/init/initCore.d.ts +9 -0
  62. package/types/reducer.d.ts +4 -4
  63. package/types/remove.d.ts +4 -4
  64. package/types/set.d.ts +4 -0
  65. package/types/setDefaultID.d.ts +3 -3
  66. package/types/setMap.d.ts +17 -0
  67. package/types/stateTypes.d.ts +2 -2
  68. package/types/states.d.ts +8 -8
  69. package/types/thunkContextMap.d.ts +4 -4
  70. package/types/thunkContextTypes.d.ts +4 -4
  71. package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +5 -0
  72. package/types/thunkModule/index.d.ts +15 -0
  73. package/types/update.d.ts +5 -0
  74. package/types/useThunk.d.ts +5 -8
  75. package/types/useThunkReducer.d.ts +5 -11
  76. package/dist/index.js +0 -1723
  77. package/dist/index.umd.cjs +0 -50
  78. package/src/action.ts +0 -20
  79. package/src/dispatch.ts +0 -6
  80. package/src/dispatchFuncMap.ts +0 -67
  81. package/src/init.ts +0 -64
  82. package/src/registerThunk.ts +0 -42
  83. package/src/setData.ts +0 -26
  84. package/src/thunk.ts +0 -20
  85. package/src/thunkModuleFuncMap.ts +0 -11
  86. package/types/registerThunk.d.ts +0 -4
  87. package/types/setData.d.ts +0 -5
  88. /package/{types → dist}/action.d.ts +0 -0
@@ -1,71 +1,70 @@
1
1
  //https://medium.com/solute-labs/configuring-thunk-action-creators-and-redux-dev-tools-with-reacts-usereducer-hook-5a1608476812
2
2
  //https://github.com/nathanbuchar/react-hook-thunk-reducer/blob/master/src/thunk-reducer.js
3
3
 
4
- import { type Dispatch, type Reducer, useCallback, useContext } from 'react'
5
- import type { BaseAction } from './action'
6
- import type { ClassState, State } from './stateTypes'
4
+ import { useCallback, useContext } from 'react'
5
+ import type BaseAction from './action/baseAction'
6
+ import type { Reducer } from './reducer'
7
+ import type { set } from './set'
8
+ import { getStateOrNullByModule } from './states'
9
+ import type { ModuleState, State } from './stateTypes'
7
10
  import { THUNK_CONTEXT_MAP } from './thunkContextMap'
8
11
 
9
- export type Thunk<S extends State, A extends BaseAction> = (
10
- dispatch: Dispatch<ActionOrThunk<S, A>>,
11
- getClassState: () => ClassState<S>,
12
- ) => void
13
-
14
- export type ActionOrThunk<S extends State, A extends BaseAction> = A | Thunk<S, A>
15
12
  /**
16
13
  * useThunkReducer
17
14
  *
18
15
  * Augments React's useReducer() hook so that the action
19
- * dispatcher supports thunks.
20
- *
21
- * @param {Function} reducer
22
- * @param {string} className
23
- * @returns {[ClassState<S>, Dispatch]}
16
+ * setter (dispatcher) supports thunks.
24
17
  */
25
- export default <S extends State, A extends BaseAction>(
26
- reducer: Reducer<ClassState<S>, A>,
27
- className: string,
28
- ): [ClassState<S>, Dispatch<A | Thunk<S, A>>] => {
29
- const { context } = THUNK_CONTEXT_MAP.theMap[className]
18
+ export default <S extends State>(reducer: Reducer<S>, moduleName: string): [ModuleState<S>, set<S>] => {
19
+ const { context } = THUNK_CONTEXT_MAP.theMap[moduleName]
20
+
21
+ const { refModuleState, setModuleState: setModuleState_c } = useContext(context)
22
+ const getModuleState = useCallback(() => {
23
+ return refModuleState.current
24
+ }, [refModuleState]) as () => ModuleState<S>
30
25
 
31
- const { refClassState, setClassState: setClassState_c } = useContext(context)
32
- const getClassState = useCallback(() => {
33
- return refClassState.current
34
- }, [refClassState])
26
+ const setModuleState = useCallback(
27
+ (newModuleState: ModuleState<S>) => {
28
+ refModuleState.current = newModuleState
29
+ setModuleState_c(newModuleState)
30
+ },
31
+ [refModuleState, setModuleState_c],
32
+ )
35
33
 
36
- const setClassState = useCallback(
37
- (newClassState: ClassState<S>) => {
38
- refClassState.current = newClassState
39
- setClassState_c(newClassState)
34
+ const get = useCallback(
35
+ (id?: string) => {
36
+ const moduleState = getModuleState()
37
+ const state = getStateOrNullByModule(moduleState, id)
38
+ return state
40
39
  },
41
- [refClassState, setClassState_c],
40
+ [getModuleState],
42
41
  )
43
42
 
44
43
  // 5. reducer.
45
44
  const reduce = useCallback(
46
- (action: A): ClassState<S> => {
47
- const classState = getClassState()
48
- const newClassState = reducer(classState, action)
49
- return newClassState
45
+ (action: BaseAction): ModuleState<S> => {
46
+ const moduleState = getModuleState()
47
+ const newModuleState = reducer(moduleState, action)
48
+ return newModuleState
50
49
  },
51
- [reducer, getClassState],
50
+ [reducer, getModuleState],
52
51
  )
53
52
 
54
- // augmented dispatcher.
55
- const dispatch = useCallback(
56
- (action: A | Thunk<S, A>) => {
53
+ // augmented setter.
54
+ const set: set<S> = useCallback(
55
+ (action) => {
57
56
  if (typeof action === 'function') {
58
57
  // action is Thunk<S, A>
59
- action(dispatch, getClassState)
58
+ action(set, get, getModuleState)
60
59
  return
61
60
  }
62
61
 
63
- // action is not function. so action is A
64
- const newClassState = reduce(action)
65
- setClassState(newClassState)
62
+ // action is not function. so action is BaseAction
63
+ const newModuleState = reduce(action)
64
+ setModuleState(newModuleState)
66
65
  },
67
- [getClassState, setClassState, reduce],
66
+ [getModuleState, setModuleState, reduce],
68
67
  )
69
68
 
70
- return [refClassState.current, dispatch]
69
+ return [refModuleState.current, set]
71
70
  }
@@ -1,7 +1,7 @@
1
- import { type JSX } from 'react';
1
+ import { type ReactNode } from 'react';
2
2
  type Props = {
3
- classes?: string[];
4
- children?: JSX.Element | JSX.Element[];
3
+ modules?: string[];
4
+ children?: ReactNode;
5
5
  };
6
- declare const ThunkContext: (props: Props) => JSX.Element;
6
+ declare const ThunkContext: (props: Props) => ReactNode;
7
7
  export default ThunkContext;
@@ -0,0 +1,4 @@
1
+ import type { State } from '../stateTypes';
2
+ import type BaseAction from './baseAction';
3
+ import type { Thunk } from './thunk';
4
+ export type ActionOrThunk<S extends State> = BaseAction | Thunk<S>;
@@ -0,0 +1,5 @@
1
+ export default interface BaseAction {
2
+ myID: string;
3
+ type: string;
4
+ [key: string]: unknown;
5
+ }
@@ -0,0 +1,8 @@
1
+ import type { ModuleState, State } from '../stateTypes';
2
+ import type { ActionOrThunk } from './ActionOrThunk';
3
+ import type BaseAction from './baseAction';
4
+ import type { Thunk } from './thunk';
5
+ export type { Thunk, ActionOrThunk, BaseAction };
6
+ export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>;
7
+ export type ThunkFunc<S extends State> = (...params: any[]) => Thunk<S>;
8
+ export type GetModuleState<S extends State> = () => ModuleState<S>;
@@ -0,0 +1,3 @@
1
+ import type { set } from '../set';
2
+ import type { ModuleState, State } from '../stateTypes';
3
+ export type Thunk<S extends State> = (set: set<S>, get: (id?: string) => S | null, getModuleState: () => ModuleState<S>) => void;
@@ -0,0 +1,4 @@
1
+ import type { State } from './stateTypes';
2
+ import type { ThunkModule } from './thunkModule';
3
+ declare const _default: <S extends State>(theDo: ThunkModule<S>) => void;
4
+ export default _default;
@@ -1,4 +1,4 @@
1
1
  import type { Dispatch as rDispatch } from 'react';
2
- import type { ActionOrThunk } from './action';
2
+ import type { ActionOrThunk } from './action/ActionOrThunk';
3
3
  import type { State } from './stateTypes';
4
4
  export type Dispatch<S extends State> = rDispatch<ActionOrThunk<S>>;
@@ -1,8 +1,7 @@
1
- import type { BaseAction } from './action';
1
+ import type { Dispatch } from './dispatch';
2
2
  import type { State } from './stateTypes';
3
- import type { ThunkModule, ThunkModuleFunc } from './thunk';
4
- import { type DefaultThunkModuleFuncMap } from './thunkModuleFuncMap';
5
- import type { Thunk as rThunk } from './useThunkReducer';
3
+ import type { ThunkModule, ThunkModuleFunc } from './thunkModule';
4
+ import { type DefaultThunkModuleFuncMap } from './thunkModule/defaultThunkModuleFuncMap';
6
5
  type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
7
6
  export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
8
7
  [action in keyof T]: VoidReturnType<T[action]>;
@@ -14,5 +13,5 @@ export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModul
14
13
  [className: string]: DispatchFuncMap<S, T>;
15
14
  }
16
15
  export declare const DISPATCH_FUNC_MAP_BY_CLASS_MAP: DispatchFuncMapByClassMap<any, any>;
17
- export declare const constructDispatchMap: <S extends State, T extends ThunkModuleFunc<S>, A extends BaseAction>(theDo: ThunkModule<S, T>, dispatch: (action: A | rThunk<S, A>) => void, dispatchMap: DispatchFuncMap<S, T>) => DispatchFuncMap<S, T>;
16
+ export declare const constructDispatchMap: <S extends State, T extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>, dispatch: Dispatch<S>, dispatchMap: DispatchFuncMap<S, T>) => DispatchFuncMap<S, T>;
18
17
  export {};
package/types/get.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { State } from './stateTypes';
2
+ export type get = <S extends State>(id: string) => S;
package/types/index.d.ts CHANGED
@@ -1,15 +1,16 @@
1
- import type { GetClassState, Thunk } from './action';
2
- import type { Dispatch } from './dispatch';
3
- import type { DispatchFuncMap } from './dispatchFuncMap';
1
+ import type { GetModuleState, Thunk } from './action';
2
+ import createThunk from './createThunk';
4
3
  import { genUUID } from './genUUID';
4
+ import type { get } from './get';
5
5
  import { type InitParams, init } from './init';
6
- import registerThunk from './registerThunk';
7
6
  import { remove } from './remove';
8
- import { setData } from './setData';
7
+ import type { set } from './set';
9
8
  import { setDefaultID } from './setDefaultID';
10
- import { getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk } from './states';
11
- import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes';
9
+ import type { setMap } from './setMap';
10
+ import { getDefaultID, getNode, getState, getStateByModule, getStateOrNullByModule } from './states';
11
+ import type { ModuleState, State } from './stateTypes';
12
12
  import ThunkContext from './ThunkContext';
13
- import type { ThunkModule, ThunkModuleToFunc } from './thunk';
13
+ import type { ThunkModule, ThunkModuleToFunc } from './thunkModule';
14
+ import { update } from './update';
14
15
  import useThunk, { type UseThunk } from './useThunk';
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, };
16
+ export { createThunk, useThunk, type UseThunk, ThunkContext, type State, type ModuleState, type GetModuleState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type set, type setMap, type get, getNode, getDefaultID, getStateOrNullByModule, getStateByModule, getState, init, type InitParams, update, remove, setDefaultID, genUUID, };
@@ -0,0 +1,7 @@
1
+ import type { Thunk } from '../action';
2
+ import type { State } from '../stateTypes';
3
+ export interface InitParams<S extends State> {
4
+ myID?: string;
5
+ state: S;
6
+ }
7
+ export declare const init: <S extends State>(params: InitParams<S>, myuuidv4?: () => string) => Thunk<S>;
@@ -0,0 +1,9 @@
1
+ import type BaseAction from '../action/baseAction';
2
+ import type { ModuleState, State } from '../stateTypes';
3
+ export interface InitAction<S extends State> extends BaseAction {
4
+ state: S;
5
+ }
6
+ export declare const INIT = "@chhsiao1981/use-thunk/INIT";
7
+ declare const _default: <S extends State>(myID: string, state: S) => InitAction<S>;
8
+ export default _default;
9
+ export declare const reduceInit: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
@@ -1,5 +1,5 @@
1
1
  import type { Reducer as rReducer } from 'react';
2
- import type { BaseAction } from './action';
3
- import type { ClassState, State } from './stateTypes';
4
- export type Reducer<S extends State> = rReducer<ClassState<S>, BaseAction>;
5
- export type ReduceFunc<S extends State> = (state: ClassState<S>, action: BaseAction) => ClassState<S>;
2
+ import type BaseAction from './action/baseAction';
3
+ import type { ModuleState, State } from './stateTypes';
4
+ export type Reducer<S extends State> = rReducer<ModuleState<S>, BaseAction>;
5
+ export type ReduceFunc<S extends State> = (state: ModuleState<S>, action: BaseAction) => ModuleState<S>;
package/types/remove.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { BaseAction, Thunk } from './action';
2
- import type { ClassState, State } from './stateTypes';
3
- export declare const remove: <S extends State>(myID: string) => Thunk<S>;
1
+ import type BaseAction from './action/baseAction';
2
+ import type { ModuleState, State } from './stateTypes';
4
3
  export declare const REMOVE = "@chhsiao1981/use-thunk/REMOVE";
5
- export declare const reduceRemove: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
4
+ export declare const remove: (myID: string) => BaseAction;
5
+ export declare const reduceRemove: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
package/types/set.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Dispatch } from 'react';
2
+ import type { ActionOrThunk } from './action/ActionOrThunk';
3
+ import type { State } from './stateTypes';
4
+ export type set<S extends State> = Dispatch<ActionOrThunk<S>>;
@@ -1,5 +1,5 @@
1
- import type { BaseAction } from './action';
2
- import type { ClassState, State } from './stateTypes';
1
+ import type BaseAction from './action/baseAction';
2
+ import type { ModuleState, State } from './stateTypes';
3
3
  export declare const SET_DEFAULT_ID = "@chhsiao1981/use-thunk/SET_DEFAULT_ID";
4
4
  export declare const setDefaultID: (myID: string) => BaseAction;
5
- export declare const reduceSetDefaultID: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
5
+ export declare const reduceSetDefaultID: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
@@ -0,0 +1,17 @@
1
+ import type { set } from './set';
2
+ import type { State } from './stateTypes';
3
+ import type { ThunkModule, ThunkModuleFunc } from './thunkModule';
4
+ import { type DefaultThunkModuleFuncMap } from './thunkModule/defaultThunkModuleFuncMap';
5
+ type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
6
+ export type setMap<S extends State, T extends ThunkModuleFunc<S>> = {
7
+ [action in keyof T]: VoidReturnType<T[action]>;
8
+ } & Omit<DefaultSetMap, keyof T>;
9
+ export type DefaultSetMap = {
10
+ [action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
11
+ };
12
+ export interface setMapByModuleMap<S extends State, T extends ThunkModuleFunc<S>> {
13
+ [name: string]: setMap<S, T>;
14
+ }
15
+ export declare const SET_MAP_BY_MODULE_MAP: setMapByModuleMap<any, any>;
16
+ export declare const constructSetMap: <S extends State, T extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>, set: set<S>, setMap: setMap<S, T>) => setMap<S, T>;
17
+ export {};
@@ -8,8 +8,8 @@ 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 ClassState<S extends State> = {
12
- myClass: string;
11
+ export type ModuleState<S extends State> = {
12
+ name: string;
13
13
  defaultID?: string | null;
14
14
  nodes: NodeStateMap<S>;
15
15
  defaultState: S;
package/types/states.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import type { DispatchFuncMap } from './dispatchFuncMap';
2
- import type { ClassState, NodeState, State } from './stateTypes';
3
- import type { ThunkModuleFunc } from './thunk';
1
+ import type { setMap } from './setMap';
2
+ import type { ModuleState, NodeState, State } from './stateTypes';
3
+ import type { ThunkModuleFunc } from './thunkModule';
4
4
  import type { UseThunk } from './useThunk';
5
- export declare const getDefaultID: <S extends State>(classState: ClassState<S>) => string;
6
- export declare const getNode: <S extends State>(classState: ClassState<S>, myID?: string) => NodeState<S> | null;
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];
5
+ export declare const getDefaultID: <S extends State>(moduleState: ModuleState<S>) => string;
6
+ export declare const getNode: <S extends State>(moduleState: ModuleState<S>, myID?: string) => NodeState<S> | null;
7
+ export declare const getStateOrNullByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S | null;
8
+ export declare const getStateByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S;
9
+ export declare const getState: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, setMap<S, R>, string];
@@ -1,12 +1,12 @@
1
1
  import type { Context as rContext } from 'react';
2
- import type { ClassState } from './stateTypes';
2
+ import type { ModuleState } from './stateTypes';
3
3
  import type { Context } from './thunkContextTypes';
4
4
  export type ThunkContextMap = {
5
5
  theMap: {
6
- [classname: string]: {
6
+ [moduleName: string]: {
7
7
  context: rContext<Context<any>>;
8
- refClassState: {
9
- current: ClassState<any>;
8
+ refModuleState: {
9
+ current: ModuleState<any>;
10
10
  };
11
11
  };
12
12
  };
@@ -1,8 +1,8 @@
1
1
  import type { Dispatch, SetStateAction } from 'react';
2
- import type { ClassState, State } from './stateTypes';
2
+ import type { ModuleState, State } from './stateTypes';
3
3
  export type Context<S extends State> = {
4
- refClassState: {
5
- current: ClassState<S>;
4
+ refModuleState: {
5
+ current: ModuleState<S>;
6
6
  };
7
- setClassState: Dispatch<SetStateAction<ClassState<S>>>;
7
+ setModuleState: Dispatch<SetStateAction<ModuleState<S>>>;
8
8
  };
@@ -0,0 +1,5 @@
1
+ import type { ActionFunc } from '../action';
2
+ export declare const DEFAULT_THUNK_MODULE_FUNC_MAP: DefaultThunkModuleFuncMap;
3
+ export type DefaultThunkModuleFuncMap = {
4
+ [action: string]: ActionFunc<any>;
5
+ };
@@ -0,0 +1,15 @@
1
+ import type { ThunkFunc } from '../action';
2
+ import type { Reducer } from '../reducer';
3
+ import type { State } from '../stateTypes';
4
+ export interface ThunkModuleBase<S extends State> {
5
+ [idx: string]: ThunkFunc<S> | string | Reducer<S> | S | undefined;
6
+ }
7
+ export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
8
+ [action: string]: ThunkFunc<S>;
9
+ }
10
+ export type ThunkModule<S extends State> = {
11
+ name: string;
12
+ default?: Reducer<S>;
13
+ defaultState: S;
14
+ } & ThunkModuleBase<S>;
15
+ export type ThunkModuleToFunc<T> = Omit<T, 'name' | 'default' | 'defaultState'>;
@@ -0,0 +1,5 @@
1
+ import type BaseAction from './action/baseAction';
2
+ import type { ModuleState, State } from './stateTypes';
3
+ export declare const UPDATE = "@chhsiao1981/use-thunk/UPDATE";
4
+ export declare const update: <S extends State>(myID: string, data: Partial<S>) => BaseAction;
5
+ export declare const reduceUpdate: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
@@ -1,12 +1,9 @@
1
- import { type DispatchFuncMap } from './dispatchFuncMap';
2
- import type { ClassState, State } from './stateTypes';
3
- import type { ThunkModule, ThunkModuleFunc } from './thunk';
4
- export type UseThunk<S extends State, R extends ThunkModuleFunc<S>> = [
5
- ClassState<S>,
6
- DispatchFuncMap<S, R>
7
- ];
1
+ import { type setMap } from './setMap';
2
+ import type { ModuleState, State } from './stateTypes';
3
+ import type { ThunkModule, ThunkModuleFunc } from './thunkModule';
4
+ export type UseThunk<S extends State, R extends ThunkModuleFunc<S>> = [ModuleState<S>, setMap<S, R>];
8
5
  /**********
9
6
  * useThunk
10
7
  **********/
11
- declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S, R>) => UseThunk<S, R>;
8
+ declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
12
9
  export default _default;
@@ -1,17 +1,11 @@
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>;
1
+ import type { Reducer } from './reducer';
2
+ import type { set } from './set';
3
+ import type { ModuleState, State } from './stateTypes';
6
4
  /**
7
5
  * useThunkReducer
8
6
  *
9
7
  * Augments React's useReducer() hook so that the action
10
- * dispatcher supports thunks.
11
- *
12
- * @param {Function} reducer
13
- * @param {string} className
14
- * @returns {[ClassState<S>, Dispatch]}
8
+ * setter (dispatcher) supports thunks.
15
9
  */
16
- declare const _default: <S extends State, A extends BaseAction>(reducer: Reducer<ClassState<S>, A>, className: string) => [ClassState<S>, Dispatch<A | Thunk<S, A>>];
10
+ declare const _default: <S extends State>(reducer: Reducer<S>, moduleName: string) => [ModuleState<S>, set<S>];
17
11
  export default _default;