@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
package/types/index.d.ts CHANGED
@@ -1,15 +1,21 @@
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
- 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, };
15
+ export { createThunk, registerThunk, // to deprecate
16
+ useThunk, type UseThunk, ThunkContext, type State, type ModuleState, type ModuleState as ClassState, // to deprecate
17
+ type GetModuleState, type GetModuleState as GetClassState, // to deprecate
18
+ type Thunk, type ThunkModule, type ThunkModuleToFunc, type set as Dispatch, // to deprecate
19
+ type set, type setMap as DispatchFuncMap, // to deprecate
20
+ type setMap, getNode, getDefaultID, getState, mustGetState, mustGetStateByThunk, init, type InitParams, setData, // to deprecate
21
+ 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,9 @@ 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
+ myClass?: string;
13
14
  defaultID?: string | null;
14
15
  nodes: NodeStateMap<S>;
15
16
  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 getState: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S | null;
8
+ export declare const mustGetState: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S;
9
+ export declare const mustGetStateByThunk: <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,16 @@
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
+ myClass?: string;
13
+ default?: Reducer<S>;
14
+ defaultState: S;
15
+ } & ThunkModuleBase<S>;
16
+ export type ThunkModuleToFunc<T> = Omit<T, 'name' | 'myClass' | 'default' | 'defaultState'>;
@@ -0,0 +1,6 @@
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 setData: <S extends State>(myID: string, data: Partial<S>) => BaseAction;
6
+ 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;