@chhsiao1981/use-thunk 9.0.5 → 10.0.1

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 (52) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +59 -170
  3. package/dist/index.js +1723 -0
  4. package/dist/index.umd.cjs +50 -0
  5. package/package.json +2 -2
  6. package/src/ThunkContext.tsx +42 -0
  7. package/src/dispatchFuncMap.ts +48 -4
  8. package/src/index.ts +4 -18
  9. package/src/init.ts +12 -30
  10. package/src/reduceMap.ts +0 -14
  11. package/src/registerThunk.ts +35 -0
  12. package/src/remove.ts +6 -32
  13. package/src/setRoot.ts +4 -1
  14. package/src/stateTypes.ts +0 -29
  15. package/src/states.ts +4 -1
  16. package/src/thunk.ts +1 -1
  17. package/src/thunkContextMap.ts +22 -0
  18. package/src/thunkContextTypes.ts +8 -0
  19. package/src/thunkModuleFuncMap.ts +0 -11
  20. package/src/useThunk.ts +31 -55
  21. package/src/useThunkReducer.ts +19 -22
  22. package/types/ThunkContext.d.ts +7 -0
  23. package/types/dispatchFuncMap.d.ts +6 -5
  24. package/types/index.d.ts +3 -8
  25. package/types/init.d.ts +1 -1
  26. package/types/registerThunk.d.ts +4 -0
  27. package/types/remove.d.ts +2 -2
  28. package/types/stateTypes.d.ts +0 -19
  29. package/types/thunk.d.ts +1 -1
  30. package/types/thunkContextMap.d.ts +15 -0
  31. package/types/thunkContextTypes.d.ts +8 -0
  32. package/types/thunkModuleFuncMap.d.ts +1 -5
  33. package/types/useThunk.d.ts +6 -3
  34. package/types/useThunkReducer.d.ts +3 -4
  35. package/src/addChild.ts +0 -16
  36. package/src/addLink.ts +0 -27
  37. package/src/addRelation.ts +0 -32
  38. package/src/getRelation.ts +0 -43
  39. package/src/removeChild.ts +0 -46
  40. package/src/removeLink.ts +0 -47
  41. package/src/removeRelation.ts +0 -84
  42. package/types/addChild.d.ts +0 -5
  43. package/types/addLink.d.ts +0 -6
  44. package/types/addRelation.d.ts +0 -6
  45. package/types/getRelation.d.ts +0 -5
  46. package/types/reducerModuleFuncMap.d.ts +0 -10
  47. package/types/removeChild.d.ts +0 -9
  48. package/types/removeLink.d.ts +0 -9
  49. package/types/removeRelation.d.ts +0 -12
  50. package/types/thunk-reducer.d.ts +0 -16
  51. package/types/thunkReducer.d.ts +0 -18
  52. package/types/useReducer.d.ts +0 -8
@@ -1,84 +0,0 @@
1
- import type { BaseAction, GetClassState } from './action'
2
- import type { Dispatch } from './dispatch'
3
- import type { ClassState, Relation, State } from './stateTypes'
4
-
5
- export interface RemoveRelationAction extends BaseAction {
6
- relationID: string
7
- relationClass: string
8
- }
9
-
10
- // @ts-expect-error DispatchFuncMap can be any type
11
- type RelationRemove = (theDo: DispatchFuncMap) => void
12
- type RemoveRelationCore = (myID: string, relationID: string, relationClass: string) => BaseAction
13
-
14
- export const removeRelation = <S extends State>(
15
- dispatch: Dispatch<S>,
16
- getClassState: GetClassState<S>,
17
- myID: string,
18
- relationID: string,
19
- relationClass: string,
20
- isFromRelation: boolean,
21
- relationRemove: RelationRemove,
22
- removeRelationCore: RemoveRelationCore,
23
- relationName: Relation,
24
- ) => {
25
- const classState = getClassState()
26
-
27
- const myNode = classState.nodes[myID]
28
- if (!myNode) {
29
- return
30
- }
31
-
32
- const relation = myNode[relationName]
33
- if (!relation) {
34
- return
35
- }
36
- const relationByClass = relation[relationClass]
37
- if (!relationByClass) {
38
- return
39
- }
40
-
41
- const newIDs = relationByClass.list.filter((eachID: string) => eachID !== relationID)
42
- if (relationByClass.list.length === newIDs.length) return
43
-
44
- if (!isFromRelation) {
45
- relationRemove(relationByClass.do)
46
- }
47
-
48
- dispatch(removeRelationCore(myID, relationID, relationClass))
49
- }
50
-
51
- export const reduceRemoveRelation = <S extends State>(
52
- classState: ClassState<S>,
53
- myID: string,
54
- relationID: string,
55
- relationClass: string,
56
- relationName: Relation.LINKS | Relation.CHILDREN,
57
- ): ClassState<S> => {
58
- const myNode = classState.nodes[myID]
59
- if (!myNode) return classState
60
-
61
- const relation = myNode[relationName]
62
- if (!relation) return classState
63
-
64
- const relationByClass = relation[relationClass]
65
- if (!relationByClass) return classState
66
-
67
- const relationIDs = relationByClass.list || []
68
- const newIDs = relationIDs.filter((eachID: string) => eachID !== relationID)
69
- if (relationIDs.length === newIDs.length) return classState
70
-
71
- const newRelation = Object.assign({}, relation)
72
- if (newIDs.length === 0) {
73
- delete newRelation[relationClass]
74
- } else {
75
- const newRelationByClass = Object.assign({}, relationByClass, { list: newIDs })
76
- newRelation[relationClass] = newRelationByClass
77
- }
78
-
79
- const newMyNode = Object.assign({}, myNode, { [relationName]: newRelation })
80
- const newNodes = Object.assign({}, classState.nodes, { [myID]: newMyNode })
81
- const newClassState = Object.assign({}, classState, { nodes: newNodes })
82
-
83
- return newClassState
84
- }
@@ -1,5 +0,0 @@
1
- import { type AddRelationAction } from './addRelation';
2
- import { type ClassState, type NodeMeta, type State } from './stateTypes';
3
- export declare const ADD_CHILD = "@chhsiao1981/use-thunk/ADD_CHILD";
4
- export declare const addChild: (myID: string, child: NodeMeta) => AddRelationAction;
5
- export declare const reduceAddChild: <S extends State>(classState: ClassState<S>, action: AddRelationAction) => ClassState<S>;
@@ -1,6 +0,0 @@
1
- import type { Thunk } from './action';
2
- import { type AddRelationAction } from './addRelation';
3
- import { type ClassState, type NodeMeta, type State } from './stateTypes';
4
- export declare const addLink: <S extends State>(myID: string, link: NodeMeta, isFromLink?: boolean) => Thunk<S>;
5
- export declare const ADD_LINK = "@chhsiao1981/use-thunk/ADD_LINK";
6
- export declare const reduceAddLink: <S extends State>(classState: ClassState<S>, action: AddRelationAction) => ClassState<S>;
@@ -1,6 +0,0 @@
1
- import type { BaseAction } from './action';
2
- import type { ClassState, NodeMeta, Relation, State } from './stateTypes';
3
- export interface AddRelationAction extends BaseAction {
4
- relaton: NodeMeta;
5
- }
6
- export declare const reduceAddRelation: <S extends State>(classState: ClassState<S>, action: AddRelationAction, relationName: Relation) => ClassState<S>;
@@ -1,5 +0,0 @@
1
- import { type NodeState, type State } from './stateTypes';
2
- export declare const getChildIDs: <S extends State>(myNode: NodeState<S>, childClass: string) => string[];
3
- export declare const getChildID: <S extends State>(myNode: NodeState<S>, childClass: string) => string;
4
- export declare const getLinkIDs: <S extends State>(myNode: NodeState<S>, linkClass: string) => string[];
5
- export declare const getLinkID: <S extends State>(myNode: NodeState<S>, linkClass: string) => string;
@@ -1,10 +0,0 @@
1
- export declare const DEFAULT_REDUCER_MODULE_FUNC_MAP: {
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;
4
- remove: <S extends import("./stateTypes").State>(myID: string, isFromParent?: boolean) => import("./action").Thunk<S>;
5
- addChild: (myID: string, child: import("./stateTypes").NodeMeta) => import("./addRelation").AddRelationAction;
6
- removeChild: <S extends import("./stateTypes").State>(myID: string, childID: string, childClass: string, isFromChild?: boolean) => import("./action").Thunk<S>;
7
- addLink: <S extends import("./stateTypes").State>(myID: string, link: import("./stateTypes").NodeMeta, isFromLink?: boolean) => import("./action").Thunk<S>;
8
- removeLink: <S extends import("./stateTypes").State>(myID: string, linkID: string, linkClass: string, isFromLink?: boolean) => import("./action").Thunk<S>;
9
- };
10
- export type DefaultReducerModuleFuncMap = typeof DEFAULT_REDUCER_MODULE_FUNC_MAP;
@@ -1,9 +0,0 @@
1
- import type { Thunk } from './action';
2
- import { type RemoveRelationAction } from './removeRelation';
3
- import { type ClassState, type State } from './stateTypes';
4
- /***
5
- * remove-child
6
- */
7
- export declare const removeChild: <S extends State>(myID: string, childID: string, childClass: string, isFromChild?: boolean) => Thunk<S>;
8
- export declare const REMOVE_CHILD = "@chhsiao1981/use-thunk/REMOVE_CHILD";
9
- export declare const reduceRemoveChild: <S extends State>(classState: ClassState<S>, action: RemoveRelationAction) => ClassState<S>;
@@ -1,9 +0,0 @@
1
- import type { Thunk } from './action';
2
- import { type RemoveRelationAction } from './removeRelation';
3
- import { type ClassState, type State } from './stateTypes';
4
- /***
5
- * remove-link
6
- */
7
- export declare const removeLink: <S extends State>(myID: string, linkID: string, linkClass: string, isFromLink?: boolean) => Thunk<S>;
8
- export declare const REMOVE_LINK = "@chhsiao1981/use-thunk/REMOVE_LINK";
9
- export declare const reduceRemoveLink: <S extends State>(classState: ClassState<S>, action: RemoveRelationAction) => ClassState<S>;
@@ -1,12 +0,0 @@
1
- import type { BaseAction, GetClassState } from './action';
2
- import type { Dispatch } from './dispatch';
3
- import type { ClassState, Relation, State } from './stateTypes';
4
- export interface RemoveRelationAction extends BaseAction {
5
- relationID: string;
6
- relationClass: string;
7
- }
8
- type RelationRemove = (theDo: DispatchFuncMap) => void;
9
- type RemoveRelationCore = (myID: string, relationID: string, relationClass: string) => BaseAction;
10
- export declare const removeRelation: <S extends State>(dispatch: Dispatch<S>, getClassState: GetClassState<S>, myID: string, relationID: string, relationClass: string, isFromRelation: boolean, relationRemove: RelationRemove, removeRelationCore: RemoveRelationCore, relationName: Relation) => void;
11
- export declare const reduceRemoveRelation: <S extends State>(classState: ClassState<S>, myID: string, relationID: string, relationClass: string, relationName: Relation.LINKS | Relation.CHILDREN) => ClassState<S>;
12
- export {};
@@ -1,16 +0,0 @@
1
- import { type Dispatch, type Reducer } from 'react';
2
- export type Thunk<State, BaseAction> = (dispatch: Dispatch<ActionOrThunk<State, BaseAction>>, getState: () => State) => void;
3
- export type ActionOrThunk<State, BaseAction> = BaseAction | Thunk<State, BaseAction>;
4
- /**
5
- * useThunkReducer
6
- *
7
- * Augments React's useReducer() hook so that the action
8
- * dispatcher supports thunks.
9
- *
10
- * @param {Function} reducer
11
- * @param {Sas} initArg
12
- * @param {Function} [init]
13
- * @returns {[Sas, Dispatch]}
14
- */
15
- export declare const useThunkReducer: <State, BaseAction>(reducer: Reducer<State, BaseAction>, initArg: State, init?: (s: State) => State) => [State, Dispatch<BaseAction | Thunk<State, BaseAction>>];
16
- export default useThunkReducer;
@@ -1,18 +0,0 @@
1
- import { type Dispatch, type Reducer } from 'react';
2
- import type { BaseAction } from './action';
3
- import type { State } from './stateTypes';
4
- export type Thunk<S extends State, A extends BaseAction> = (dispatch: Dispatch<ActionOrThunk<S, A>>, getState: () => 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<S, A>, initArg: S, init?: (...params: any[]) => S) => [S, Dispatch<A | Thunk<S, A>>];
18
- export default _default;
@@ -1,8 +0,0 @@
1
- import type { DispatchFuncMap } from './dispatchFuncMap';
2
- import type { ReducerModule, ReducerModuleFunc } from './reducer';
3
- import type { ClassState, State, StateType } from './stateTypes';
4
- /**********
5
- * useReducer
6
- **********/
7
- declare const _default: <S extends State, R extends ReducerModuleFunc<S>>(theDo: ReducerModule<S, R>, stateType: StateType, init?: (...params: any[]) => S) => [ClassState<S>, DispatchFuncMap<S, R>];
8
- export default _default;