@chhsiao1981/use-thunk 10.3.0 → 11.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 (85) hide show
  1. package/dist/ThunkContext.d.ts +7 -0
  2. package/dist/createReducer.d.ts +4 -0
  3. package/dist/createThunk.d.ts +4 -0
  4. package/dist/dispatch.d.ts +4 -0
  5. package/dist/dispatchFuncMap.d.ts +18 -0
  6. package/dist/genUUID.d.ts +1 -0
  7. package/dist/index.d.ts +15 -0
  8. package/{types → dist}/init.d.ts +0 -3
  9. package/dist/reduceMap.d.ts +6 -0
  10. package/dist/reducer.d.ts +5 -0
  11. package/dist/remove.d.ts +5 -0
  12. package/dist/setDefaultID.d.ts +5 -0
  13. package/dist/stateTypes.d.ts +16 -0
  14. package/dist/states.d.ts +9 -0
  15. package/{types → dist}/thunk.d.ts +6 -3
  16. package/dist/thunkContextMap.d.ts +15 -0
  17. package/dist/thunkContextTypes.d.ts +8 -0
  18. package/{types → dist}/thunkModuleFuncMap.d.ts +1 -0
  19. package/dist/update.d.ts +6 -0
  20. package/dist/useThunk.d.ts +12 -0
  21. package/dist/useThunkReducer.d.ts +17 -0
  22. package/package.json +2 -2
  23. package/src/ThunkContext.tsx +23 -18
  24. package/src/action/ActionOrThunk.ts +5 -0
  25. package/src/action/baseAction.ts +6 -0
  26. package/src/action/index.ts +16 -0
  27. package/src/action/thunk.ts +5 -0
  28. package/src/createReducer.ts +7 -7
  29. package/src/createThunk.ts +31 -0
  30. package/src/index.ts +21 -27
  31. package/src/init/index.ts +26 -0
  32. package/src/init/initCore.ts +31 -0
  33. package/src/reduceMap.ts +3 -4
  34. package/src/reducer.ts +4 -4
  35. package/src/remove.ts +10 -25
  36. package/src/set.ts +7 -0
  37. package/src/setDefaultID.ts +5 -5
  38. package/src/setMap.ts +65 -0
  39. package/src/stateTypes.ts +4 -3
  40. package/src/states.ts +17 -17
  41. package/src/thunkContextMap.ts +4 -4
  42. package/src/thunkContextTypes.ts +4 -4
  43. package/src/thunkModule/defaultThunkModuleFuncMap.ts +16 -0
  44. package/src/thunkModule/index.ts +21 -0
  45. package/src/update.ts +31 -0
  46. package/src/useThunk.ts +25 -45
  47. package/src/useThunkReducer.ts +34 -43
  48. package/types/ThunkContext.d.ts +4 -4
  49. package/types/action/ActionOrThunk.d.ts +4 -0
  50. package/types/action/baseAction.d.ts +5 -0
  51. package/types/action/index.d.ts +8 -0
  52. package/types/action/thunk.d.ts +3 -0
  53. package/types/createThunk.d.ts +5 -0
  54. package/types/dispatch.d.ts +1 -1
  55. package/types/dispatchFuncMap.d.ts +4 -5
  56. package/types/index.d.ts +14 -8
  57. package/types/init/index.d.ts +7 -0
  58. package/types/init/initCore.d.ts +9 -0
  59. package/types/reducer.d.ts +4 -4
  60. package/types/remove.d.ts +4 -4
  61. package/types/set.d.ts +4 -0
  62. package/types/setDefaultID.d.ts +3 -3
  63. package/types/setMap.d.ts +17 -0
  64. package/types/stateTypes.d.ts +3 -2
  65. package/types/states.d.ts +8 -8
  66. package/types/thunkContextMap.d.ts +4 -4
  67. package/types/thunkContextTypes.d.ts +4 -4
  68. package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +5 -0
  69. package/types/thunkModule/index.d.ts +16 -0
  70. package/types/update.d.ts +6 -0
  71. package/types/useThunk.d.ts +5 -8
  72. package/types/useThunkReducer.d.ts +5 -11
  73. package/dist/index.js +0 -1723
  74. package/dist/index.umd.cjs +0 -50
  75. package/src/action.ts +0 -20
  76. package/src/dispatch.ts +0 -6
  77. package/src/dispatchFuncMap.ts +0 -66
  78. package/src/init.ts +0 -64
  79. package/src/registerThunk.ts +0 -36
  80. package/src/setData.ts +0 -26
  81. package/src/thunk.ts +0 -16
  82. package/src/thunkModuleFuncMap.ts +0 -11
  83. package/types/registerThunk.d.ts +0 -4
  84. package/types/setData.d.ts +0 -5
  85. /package/{types → dist}/action.d.ts +0 -0
@@ -0,0 +1,7 @@
1
+ import { type ReactNode } from 'react';
2
+ type Props = {
3
+ classes?: string[];
4
+ children?: ReactNode;
5
+ };
6
+ declare const ThunkContext: (props: Props) => ReactNode;
7
+ export default ThunkContext;
@@ -0,0 +1,4 @@
1
+ import { type ReduceMap } from './reduceMap';
2
+ import type { Reducer } from './reducer';
3
+ import type { State } from './stateTypes';
4
+ export declare const createReducer: <S extends State>(reduceMap?: ReduceMap<S>) => Reducer<S>;
@@ -0,0 +1,4 @@
1
+ import type { State } from './stateTypes';
2
+ import type { ThunkModule } from './thunk';
3
+ export default function createThunk<S extends State>(theDo: ThunkModule<S>): void;
4
+ export declare function registerThunk<S extends State>(theDo: ThunkModule<S>): void;
@@ -0,0 +1,4 @@
1
+ import type { Dispatch as rDispatch } from 'react';
2
+ import type { ActionOrThunk } from './action';
3
+ import type { State } from './stateTypes';
4
+ export type Dispatch<S extends State> = rDispatch<ActionOrThunk<S>>;
@@ -0,0 +1,18 @@
1
+ import type { BaseAction } from './action';
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';
6
+ type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
7
+ export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
8
+ [action in keyof T]: VoidReturnType<T[action]>;
9
+ } & Omit<DefaultDispatchFuncMap, keyof T>;
10
+ export type DefaultDispatchFuncMap = {
11
+ [action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
12
+ };
13
+ export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
14
+ [className: string]: DispatchFuncMap<S, T>;
15
+ }
16
+ 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>, dispatch: (action: A | rThunk<S, A>) => void, dispatchMap: DispatchFuncMap<S, T>) => DispatchFuncMap<S, T>;
18
+ export {};
@@ -0,0 +1 @@
1
+ export declare const genUUID: (myuuidv4?: () => string) => string;
@@ -0,0 +1,15 @@
1
+ import type { GetClassState, Thunk } from './action';
2
+ import createThunk, { registerThunk } from './createThunk';
3
+ import type { Dispatch } from './dispatch';
4
+ import type { DispatchFuncMap } from './dispatchFuncMap';
5
+ import { genUUID } from './genUUID';
6
+ import { type InitParams, init } from './init';
7
+ import { remove } from './remove';
8
+ import { setDefaultID } from './setDefaultID';
9
+ import { getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk } from './states';
10
+ import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes';
11
+ import ThunkContext from './ThunkContext';
12
+ import type { ThunkModule, ThunkModuleToFunc } from './thunk';
13
+ import { setData, update } from './update';
14
+ import useThunk, { type UseThunk } from './useThunk';
15
+ export { createThunk, 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, update, remove, setDefaultID, genUUID, };
@@ -2,9 +2,6 @@ import type { BaseAction, Thunk } from './action';
2
2
  import type { ClassState, State } from './stateTypes';
3
3
  export interface InitParams<S extends State> {
4
4
  myID?: string;
5
- parentID?: string;
6
- doParent?: DispatchFuncMap;
7
- parentClass?: string;
8
5
  state: S;
9
6
  }
10
7
  export declare const init: <S extends State>(params: InitParams<S>, myuuidv4?: () => string) => Thunk<S>;
@@ -0,0 +1,6 @@
1
+ import type { ReduceFunc } from './reducer';
2
+ import type { State } from './stateTypes';
3
+ export interface ReduceMap<S extends State> {
4
+ [type: string]: ReduceFunc<S>;
5
+ }
6
+ export declare const DEFAULT_REDUCE_MAP: <S extends State>() => ReduceMap<S>;
@@ -0,0 +1,5 @@
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>;
@@ -0,0 +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>;
4
+ export declare const REMOVE = "@chhsiao1981/use-thunk/REMOVE";
5
+ export declare const reduceRemove: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
@@ -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>;
@@ -0,0 +1,16 @@
1
+ export interface State {
2
+ [key: string]: unknown;
3
+ }
4
+ export type NodeState<S extends State> = {
5
+ id: string;
6
+ state: S;
7
+ };
8
+ export type NodeStateMap<S extends State> = {
9
+ [key: string]: NodeState<S>;
10
+ };
11
+ export type ClassState<S extends State> = {
12
+ myClass: string;
13
+ defaultID?: string | null;
14
+ nodes: NodeStateMap<S>;
15
+ defaultState: S;
16
+ };
@@ -0,0 +1,9 @@
1
+ import type { DispatchFuncMap } from './dispatchFuncMap';
2
+ import type { ClassState, NodeState, State } from './stateTypes';
3
+ import type { ThunkModuleFunc } from './thunk';
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];
@@ -1,12 +1,15 @@
1
1
  import type { ActionFunc } from './action';
2
2
  import type { Reducer } from './reducer';
3
3
  import type { State } from './stateTypes';
4
- export interface ThunkModuleFunc<S extends State> {
4
+ export interface ThunkModuleBase<S extends State> {
5
+ [idx: string]: ActionFunc<S> | string | Reducer<S> | S | undefined;
6
+ }
7
+ export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
5
8
  [action: string]: ActionFunc<S>;
6
9
  }
7
- export type ThunkModule<S extends State, T extends ThunkModuleFunc<S>> = {
10
+ export type ThunkModule<S extends State> = {
8
11
  myClass: string;
9
12
  default?: Reducer<S>;
10
13
  defaultState: S;
11
- } & T;
14
+ } & ThunkModuleBase<S>;
12
15
  export type ThunkModuleToFunc<T> = Omit<T, 'myClass' | 'default' | 'defaultState'>;
@@ -0,0 +1,15 @@
1
+ import type { Context as rContext } from 'react';
2
+ import type { ClassState } from './stateTypes';
3
+ import type { Context } from './thunkContextTypes';
4
+ export type ThunkContextMap = {
5
+ theMap: {
6
+ [classname: string]: {
7
+ context: rContext<Context<any>>;
8
+ refClassState: {
9
+ current: ClassState<any>;
10
+ };
11
+ };
12
+ };
13
+ theList: string[];
14
+ };
15
+ export declare const THUNK_CONTEXT_MAP: ThunkContextMap;
@@ -0,0 +1,8 @@
1
+ import type { Dispatch, SetStateAction } from 'react';
2
+ import type { ClassState, State } from './stateTypes';
3
+ export type Context<S extends State> = {
4
+ refClassState: {
5
+ current: ClassState<S>;
6
+ };
7
+ setClassState: Dispatch<SetStateAction<ClassState<S>>>;
8
+ };
@@ -1,5 +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
+ update: <S extends import("./stateTypes").State>(myID: string, data: Partial<S>) => import("./action").BaseAction;
3
4
  setData: <S extends import("./stateTypes").State>(myID: string, data: Partial<S>) => import("./action").BaseAction;
4
5
  remove: <S extends import("./stateTypes").State>(myID: string) => import("./action").Thunk<S>;
5
6
  };
@@ -0,0 +1,6 @@
1
+ import type { BaseAction } from './action';
2
+ import type { ClassState, 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 setData: <S extends State>(myID: string, data: Partial<S>) => BaseAction;
6
+ export declare const reduceUpdate: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
@@ -0,0 +1,12 @@
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
+ ];
8
+ /**********
9
+ * useThunk
10
+ **********/
11
+ declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
12
+ export default _default;
@@ -0,0 +1,17 @@
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 {string} className
14
+ * @returns {[ClassState<S>, Dispatch]}
15
+ */
16
+ declare const _default: <S extends State, A extends BaseAction>(reducer: Reducer<ClassState<S>, A>, className: string) => [ClassState<S>, Dispatch<A | Thunk<S, A>>];
17
+ export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chhsiao1981/use-thunk",
3
- "version": "10.3.0",
3
+ "version": "11.0.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",
@@ -56,7 +56,7 @@
56
56
  "vite": "^6.4.1",
57
57
  "vitest": "^4.1.0"
58
58
  },
59
- "dependencies": {
59
+ "peerDependencies": {
60
60
  "react": ">=18.3.1",
61
61
  "uuid": "^14.0.0"
62
62
  }
@@ -1,40 +1,45 @@
1
- import { type JSX, useMemo, useState } from 'react'
1
+ import { type ReactNode, useMemo, useState } from 'react'
2
2
  import { THUNK_CONTEXT_MAP } from './thunkContextMap'
3
3
 
4
4
  type Props = {
5
- classes?: string[]
6
- children?: JSX.Element | JSX.Element[]
5
+ modules?: string[]
6
+ children?: ReactNode
7
7
  }
8
- const ThunkContext = (props: Props): JSX.Element => {
9
- let { classes, children } = props
10
- if (!classes) {
11
- classes = THUNK_CONTEXT_MAP.theList
12
- }
13
- if (classes.length === 0) {
14
- // @ts-expect-error with children
8
+
9
+ const ThunkContext = (props: Props): ReactNode => {
10
+ const { modules: propsModules, children } = props
11
+ const modules = propsModules || THUNK_CONTEXT_MAP.theList
12
+ // 0. if there is no Thunk modules (no createThunk): return children.
13
+ if (modules.length === 0) {
15
14
  return children
16
15
  }
17
16
 
18
- const theClass = classes[0]
17
+ // render the 0th module.
18
+ const theModule = modules[0]
19
19
 
20
- const { context: Context_m, refClassState } = THUNK_CONTEXT_MAP.theMap[theClass]
20
+ // 1. get the context and moduleState from context map.
21
+ const { context: Context_m, refModuleState } = THUNK_CONTEXT_MAP.theMap[theModule]
21
22
 
23
+ // 2. setup moduleState.
22
24
  // biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
23
- const [classState, setClassState] = useState(refClassState.current)
25
+ const [moduleState, setModuleState] = useState(refModuleState.current)
26
+ refModuleState.current = moduleState
24
27
 
25
- refClassState.current = classState
28
+ // 3. value reset only if moduleState is changed.
26
29
  // biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
27
30
  const value = useMemo(
28
31
  () => ({
29
- refClassState,
30
- setClassState,
32
+ refModuleState: refModuleState,
33
+ setModuleState: setModuleState,
31
34
  }),
32
- [classState],
35
+ [moduleState],
33
36
  )
34
37
 
38
+ // 4. get theChildren
35
39
  const theChildren =
36
- classes.length === 1 ? children : ThunkContext({ classes: classes.slice(1), children })
40
+ modules.length === 1 ? children : ThunkContext({ modules: modules.slice(1), children })
37
41
 
42
+ // 5. return context.
38
43
  return <Context_m.Provider value={value}>{theChildren}</Context_m.Provider>
39
44
  }
40
45
 
@@ -0,0 +1,5 @@
1
+ import type { State } from '../stateTypes'
2
+ import type BaseAction from './baseAction'
3
+ import type { Thunk } from './thunk'
4
+
5
+ export type ActionOrThunk<S extends State> = BaseAction | Thunk<S>
@@ -0,0 +1,6 @@
1
+ // BaseAction contains only object-based actions, no thunk-based actions.
2
+ export default interface BaseAction {
3
+ myID: string
4
+ type: string
5
+ [key: string]: unknown
6
+ }
@@ -0,0 +1,16 @@
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
+
6
+ export type { Thunk, ActionOrThunk, BaseAction }
7
+
8
+ // ActionFunc
9
+ // biome-ignore lint/suspicious/noExplicitAny: params can be any type.
10
+ export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>
11
+
12
+ // biome-ignore lint/suspicious/noExplicitAny: params can by any type.
13
+ export type ThunkFunc<S extends State> = (...params: any[]) => Thunk<S>
14
+
15
+ // GetModuleState
16
+ export type GetModuleState<S extends State> = () => ModuleState<S>
@@ -0,0 +1,5 @@
1
+ import type { set } from '../set'
2
+ import type { ModuleState, State } from '../stateTypes'
3
+
4
+ // Thunk
5
+ export type Thunk<S extends State> = (set: set<S>, getModuleState: () => ModuleState<S>) => void
@@ -1,24 +1,24 @@
1
- import type { BaseAction } from './action'
1
+ import type BaseAction from './action/baseAction'
2
2
  import { DEFAULT_REDUCE_MAP, type ReduceMap } from './reduceMap'
3
3
  import type { Reducer } from './reducer'
4
4
 
5
- import type { ClassState, State } from './stateTypes'
5
+ import type { ModuleState, State } from './stateTypes'
6
6
 
7
7
  export const createReducer = <S extends State>(reduceMap?: ReduceMap<S>): Reducer<S> => {
8
- return (classState: ClassState<S>, action: BaseAction): ClassState<S> => {
8
+ return (moduleState: ModuleState<S>, action: BaseAction): ModuleState<S> => {
9
9
  if (!action) {
10
- return classState
10
+ return moduleState
11
11
  }
12
12
 
13
13
  if (reduceMap?.[action.type]) {
14
- return reduceMap[action.type](classState, action)
14
+ return reduceMap[action.type](moduleState, action)
15
15
  }
16
16
 
17
17
  const defaultReduceMap = DEFAULT_REDUCE_MAP<S>()
18
18
  if (defaultReduceMap?.[action.type]) {
19
- return defaultReduceMap[action.type](classState, action)
19
+ return defaultReduceMap[action.type](moduleState, action)
20
20
  }
21
21
 
22
- return classState
22
+ return moduleState
23
23
  }
24
24
  }
@@ -0,0 +1,31 @@
1
+ import { createContext, type Dispatch, type SetStateAction } from 'react'
2
+ import { createThunk } from '.'
3
+ import type { ModuleState, State } from './stateTypes'
4
+ import { THUNK_CONTEXT_MAP } from './thunkContextMap'
5
+ import type { ThunkModule } from './thunkModule'
6
+
7
+ export default <S extends State>(theDo: ThunkModule<S>) => {
8
+ const { name: propsName, myClass, defaultState } = theDo
9
+ const name = (propsName ? propsName : myClass) || ''
10
+
11
+ if (THUNK_CONTEXT_MAP.theMap[name]) {
12
+ console.warn('createThunk: already init:', name)
13
+ return
14
+ }
15
+
16
+ const moduleState: ModuleState<S> = { myClass, name, nodes: {}, defaultState }
17
+ const setModuleState: Dispatch<SetStateAction<ModuleState<S>>> = () => {}
18
+ const refModuleState = { current: moduleState }
19
+ const context = createContext({ refModuleState: refModuleState, setModuleState })
20
+
21
+ THUNK_CONTEXT_MAP.theMap[name] = { context, refModuleState }
22
+ const theList = Object.keys(THUNK_CONTEXT_MAP.theMap).sort()
23
+ THUNK_CONTEXT_MAP.theList = theList
24
+
25
+ console.info('createThunk: done:', name)
26
+ }
27
+
28
+ export const registerThunk = <S extends State>(theDo: ThunkModule<S>) => {
29
+ console.warn('registerThunk will be deprecated in the next version.')
30
+ return createThunk(theDo)
31
+ }
package/src/index.ts CHANGED
@@ -1,52 +1,46 @@
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, { registerThunk } from './createThunk'
4
3
  import { genUUID } from './genUUID'
5
4
  import { type InitParams, init } from './init'
6
- import registerThunk from './registerThunk'
7
5
  import { remove } from './remove'
8
- import { setData } from './setData'
6
+ import type { set } from './set'
9
7
  import { setDefaultID } from './setDefaultID'
8
+ import type { setMap } from './setMap'
10
9
  import { getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk } from './states'
11
- import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes'
10
+ import type { ModuleState, State } from './stateTypes'
12
11
  import ThunkContext from './ThunkContext'
13
- import type { ThunkModule, ThunkModuleToFunc } from './thunk'
12
+ import type { ThunkModule, ThunkModuleToFunc } from './thunkModule'
13
+ import { setData, update } from './update'
14
14
  import useThunk, { type UseThunk } from './useThunk'
15
15
 
16
16
  export {
17
- registerThunk,
17
+ createThunk,
18
+ registerThunk, // to deprecate
18
19
  useThunk,
19
- ThunkContext,
20
20
  type UseThunk,
21
+ ThunkContext,
21
22
  type State,
22
- type NodeState,
23
- type NodeStateMap,
24
- // type NodeStateMapByClass, // XXX for global state
25
- type ClassState,
26
- type GetClassState,
27
- // type BaseAction, // XXX deemphasize action
23
+ type ModuleState,
24
+ type ModuleState as ClassState, // to deprecate
25
+ type GetModuleState,
26
+ type GetModuleState as GetClassState, // to deprecate
28
27
  type Thunk,
29
- // type ActionOrThunk, // XXX deemphasize action
30
- // type ActionFunc, // XXX deemphasize action
31
- // type Reducer, // XXX deemphasize reducer
32
28
  type ThunkModule,
33
29
  type ThunkModuleToFunc,
34
- // type ReduceFunc, // XXX deemphasize reducer
35
- type Dispatch,
36
- type DispatchFuncMap,
37
- // type DefaultDispatchFuncMap, // XXX deemphasize default
38
- getDefaultID,
30
+ type set as Dispatch, // to deprecate
31
+ type set,
32
+ type setMap as DispatchFuncMap, // to deprecate
33
+ type setMap,
39
34
  getNode,
35
+ getDefaultID,
40
36
  getState,
41
37
  mustGetState,
42
38
  mustGetStateByThunk,
43
39
  init,
44
40
  type InitParams,
45
- setData,
41
+ setData, // to deprecate
42
+ update,
46
43
  remove,
47
- // type DefaultThunkModuleFuncMap as DefaultReducerModuleFuncMap, // XXX deemphasize default
48
- // type ReduceMap, // XXX deemphasize reducer
49
- // createReducer, // XXX deemphasize reducer
50
44
  setDefaultID,
51
45
  genUUID,
52
46
  }
@@ -0,0 +1,26 @@
1
+ import type { Thunk } from '../action'
2
+ import { genUUID } from '../genUUID'
3
+ import { setDefaultID } from '../setDefaultID'
4
+ import type { State } from '../stateTypes'
5
+ import initCore from './initCore'
6
+
7
+ // InitParams
8
+ export interface InitParams<S extends State> {
9
+ myID?: string
10
+ state: S
11
+ }
12
+
13
+ export const init = <S extends State>(params: InitParams<S>, myuuidv4?: () => string): Thunk<S> => {
14
+ return (set, getModuleState) => {
15
+ const myID = params.myID ?? genUUID(myuuidv4)
16
+
17
+ const { state } = params
18
+ set(initCore(myID, state))
19
+
20
+ const { defaultID } = getModuleState()
21
+
22
+ if (!defaultID) {
23
+ set(setDefaultID(myID))
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,31 @@
1
+ import type BaseAction from '../action/baseAction'
2
+ import type { ModuleState, NodeState, NodeStateMap, State } from '../stateTypes'
3
+
4
+ export interface InitAction<S extends State> extends BaseAction {
5
+ state: S
6
+ }
7
+
8
+ export const INIT = '@chhsiao1981/use-thunk/INIT'
9
+ export default <S extends State>(myID: string, state: S): InitAction<S> => {
10
+ return {
11
+ myID,
12
+ type: INIT,
13
+ state,
14
+ }
15
+ }
16
+
17
+ export const reduceInit = <S extends State>(
18
+ moduleState: ModuleState<S>,
19
+ action: BaseAction,
20
+ ): ModuleState<S> => {
21
+ const { myID, state } = action as InitAction<S>
22
+
23
+ const myNode: NodeState<S> = {
24
+ id: myID,
25
+ state: state,
26
+ }
27
+ const newNodes: NodeStateMap<S> = Object.assign({}, moduleState.nodes, { [myID]: myNode })
28
+ const newModuleState: ModuleState<S> = Object.assign({}, moduleState, { nodes: newNodes })
29
+
30
+ return newModuleState
31
+ }
package/src/reduceMap.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { INIT, reduceInit } from './init'
1
+ import { INIT, reduceInit } from './init/initCore'
2
2
  import type { ReduceFunc } from './reducer'
3
3
  import { REMOVE, reduceRemove } from './remove'
4
- import { reduceSetData, SET_DATA } from './setData'
5
4
  import { reduceSetDefaultID, SET_DEFAULT_ID } from './setDefaultID'
6
5
  import type { State } from './stateTypes'
6
+ import { reduceUpdate, UPDATE } from './update'
7
7
 
8
8
  export interface ReduceMap<S extends State> {
9
9
  [type: string]: ReduceFunc<S>
@@ -11,9 +11,8 @@ export interface ReduceMap<S extends State> {
11
11
 
12
12
  // default reduceMap
13
13
  export const DEFAULT_REDUCE_MAP: <S extends State>() => ReduceMap<S> = () => ({
14
- // @ts-expect-error baseAction in ReduceMap
15
14
  [INIT]: reduceInit,
16
- [SET_DATA]: reduceSetData,
15
+ [UPDATE]: reduceUpdate,
17
16
  [REMOVE]: reduceRemove,
18
17
 
19
18
  // setDefaultID.
package/src/reducer.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { Reducer as rReducer } from 'react'
2
- import type { BaseAction } from './action'
3
- import type { ClassState, State } from './stateTypes'
2
+ import type BaseAction from './action/baseAction'
3
+ import type { ModuleState, State } from './stateTypes'
4
4
 
5
5
  // Reducer
6
- export type Reducer<S extends State> = rReducer<ClassState<S>, BaseAction>
6
+ export type Reducer<S extends State> = rReducer<ModuleState<S>, BaseAction>
7
7
 
8
8
  // ReduceFunc
9
- export type ReduceFunc<S extends State> = (state: ClassState<S>, action: BaseAction) => ClassState<S>
9
+ export type ReduceFunc<S extends State> = (state: ModuleState<S>, action: BaseAction) => ModuleState<S>