@chhsiao1981/use-thunk 9.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.
- package/LICENSE +21 -0
- package/README.md +437 -0
- package/package.json +63 -0
- package/src/action.ts +20 -0
- package/src/addChild.ts +13 -0
- package/src/addLink.ts +27 -0
- package/src/addRelation.ts +32 -0
- package/src/createReducer.ts +24 -0
- package/src/dispatch.ts +6 -0
- package/src/dispatchFuncMap.ts +22 -0
- package/src/genUUID.ts +38 -0
- package/src/getRelation.ts +35 -0
- package/src/index.ts +62 -0
- package/src/init.ts +82 -0
- package/src/reduceMap.ts +37 -0
- package/src/reducer.ts +9 -0
- package/src/remove.ts +76 -0
- package/src/removeChild.ts +46 -0
- package/src/removeLink.ts +47 -0
- package/src/removeRelation.ts +84 -0
- package/src/setData.ts +23 -0
- package/src/setRoot.ts +14 -0
- package/src/stateTypes.ts +62 -0
- package/src/states.ts +45 -0
- package/src/thunk.ts +16 -0
- package/src/thunkModuleFuncMap.ts +22 -0
- package/src/thunkReducer.ts +73 -0
- package/src/useThunk.ts +93 -0
- package/types/action.d.ts +11 -0
- package/types/addChild.d.ts +5 -0
- package/types/addLink.d.ts +6 -0
- package/types/addRelation.d.ts +6 -0
- package/types/createReducer.d.ts +4 -0
- package/types/dispatch.d.ts +4 -0
- package/types/dispatchFuncMap.d.ts +17 -0
- package/types/genUUID.d.ts +1 -0
- package/types/getRelation.d.ts +5 -0
- package/types/index.d.ts +19 -0
- package/types/init.d.ts +19 -0
- package/types/reduceMap.d.ts +6 -0
- package/types/reducer.d.ts +5 -0
- package/types/reducerModuleFuncMap.d.ts +10 -0
- package/types/remove.d.ts +5 -0
- package/types/removeChild.d.ts +9 -0
- package/types/removeLink.d.ts +9 -0
- package/types/removeRelation.d.ts +12 -0
- package/types/setData.d.ts +5 -0
- package/types/setRoot.d.ts +5 -0
- package/types/stateTypes.d.ts +42 -0
- package/types/states.d.ts +6 -0
- package/types/thunk-reducer.d.ts +16 -0
- package/types/thunk.d.ts +12 -0
- package/types/thunkModuleFuncMap.d.ts +10 -0
- package/types/thunkReducer.d.ts +18 -0
- package/types/useReducer.d.ts +8 -0
- package/types/useThunk.d.ts +8 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { addChild } from './addChild'
|
|
2
|
+
import { addLink } from './addLink'
|
|
3
|
+
import { init } from './init'
|
|
4
|
+
import { remove } from './remove'
|
|
5
|
+
import { removeChild } from './removeChild'
|
|
6
|
+
import { removeLink } from './removeLink'
|
|
7
|
+
import { setData } from './setData'
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_THUNK_MODULE_FUNC_MAP = {
|
|
10
|
+
init,
|
|
11
|
+
setData,
|
|
12
|
+
remove,
|
|
13
|
+
|
|
14
|
+
addChild,
|
|
15
|
+
|
|
16
|
+
removeChild,
|
|
17
|
+
|
|
18
|
+
addLink,
|
|
19
|
+
removeLink,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type DefaultThunkModuleFuncMap = typeof DEFAULT_THUNK_MODULE_FUNC_MAP
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//https://medium.com/solute-labs/configuring-thunk-action-creators-and-redux-dev-tools-with-reacts-usereducer-hook-5a1608476812
|
|
2
|
+
//https://github.com/nathanbuchar/react-hook-thunk-reducer/blob/master/src/thunk-reducer.js
|
|
3
|
+
|
|
4
|
+
import { type Dispatch, type Reducer, useCallback, useRef, useState } from 'react'
|
|
5
|
+
import type { BaseAction } from './action'
|
|
6
|
+
import type { State } from './stateTypes'
|
|
7
|
+
|
|
8
|
+
export type Thunk<S extends State, A extends BaseAction> = (
|
|
9
|
+
dispatch: Dispatch<ActionOrThunk<S, A>>,
|
|
10
|
+
getState: () => S,
|
|
11
|
+
) => void
|
|
12
|
+
|
|
13
|
+
export type ActionOrThunk<S extends State, A extends BaseAction> = A | Thunk<S, A>
|
|
14
|
+
/**
|
|
15
|
+
* useThunkReducer
|
|
16
|
+
*
|
|
17
|
+
* Augments React's useReducer() hook so that the action
|
|
18
|
+
* dispatcher supports thunks.
|
|
19
|
+
*
|
|
20
|
+
* @param {Function} reducer
|
|
21
|
+
* @param {State} initArg
|
|
22
|
+
* @param {Function} [init]
|
|
23
|
+
* @returns {[State, Dispatch]}
|
|
24
|
+
*/
|
|
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>>] => {
|
|
31
|
+
// 1. initState
|
|
32
|
+
const initState = init ? () => init(initArg) : initArg
|
|
33
|
+
|
|
34
|
+
// 2. renderState
|
|
35
|
+
const [renderState, setRenderState] = useState(initState)
|
|
36
|
+
|
|
37
|
+
// 3. hookState
|
|
38
|
+
const hookState = renderState
|
|
39
|
+
|
|
40
|
+
// 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)
|
|
47
|
+
},
|
|
48
|
+
[state, setRenderState],
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
// 5. reducer.
|
|
52
|
+
const reduce = useCallback(
|
|
53
|
+
(action: A): S => {
|
|
54
|
+
return reducer(getState(), action)
|
|
55
|
+
},
|
|
56
|
+
[reducer, getState],
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
// augmented dispatcher.
|
|
60
|
+
const dispatch = useCallback(
|
|
61
|
+
(action: A | Thunk<S, A>) => {
|
|
62
|
+
if (typeof action === 'function') {
|
|
63
|
+
action(dispatch, getState)
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setState(reduce(action))
|
|
68
|
+
},
|
|
69
|
+
[getState, setState, reduce],
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
return [hookState, dispatch]
|
|
73
|
+
}
|
package/src/useThunk.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useRef, useState } from 'react'
|
|
2
|
+
import type { ActionFunc } from './action'
|
|
3
|
+
import { createReducer } from './createReducer'
|
|
4
|
+
import type { DispatchFuncMap, DispatchFuncMapByClassMap, RefDispatchFuncMapByClassMap } from './dispatchFuncMap'
|
|
5
|
+
import type { ClassState, NodeStateMap, State, StateType } from './stateTypes'
|
|
6
|
+
import type { ThunkModule, ThunkModuleFunc } from './thunk'
|
|
7
|
+
import { DEFAULT_THUNK_MODULE_FUNC_MAP } from './thunkModuleFuncMap'
|
|
8
|
+
import useThunkReducer from './thunkReducer'
|
|
9
|
+
|
|
10
|
+
/**********
|
|
11
|
+
* useThunk
|
|
12
|
+
**********/
|
|
13
|
+
export default <S extends State, R extends ThunkModuleFunc<S>>(
|
|
14
|
+
theDo: ThunkModule<S, R>,
|
|
15
|
+
// biome-ignore lint/suspicious/noExplicitAny: params can by any types.
|
|
16
|
+
init?: (...params: any[]) => S,
|
|
17
|
+
): [ClassState<S>, DispatchFuncMap<S, R>] => {
|
|
18
|
+
const { myClass } = theDo
|
|
19
|
+
|
|
20
|
+
// 1. dispatchMapByClass
|
|
21
|
+
const refDispatchMapByClass: RefDispatchFuncMapByClassMap<S, R> = useRef({})
|
|
22
|
+
const dispatchMapByClass: DispatchFuncMapByClassMap<S, R> = refDispatchMapByClass.current
|
|
23
|
+
|
|
24
|
+
// 2. It requires shared nodes for the same class to have the same dispatchMap.
|
|
25
|
+
// We don't optimize the dispatchMap in this PR.
|
|
26
|
+
const isFirstTime = !dispatchMapByClass[myClass]
|
|
27
|
+
if (isFirstTime) {
|
|
28
|
+
// @ts-expect-error {} is a kind of DispatchFuncMap<S, R>
|
|
29
|
+
dispatchMapByClass[myClass] = {}
|
|
30
|
+
}
|
|
31
|
+
const dispatchMap = dispatchMapByClass[myClass]
|
|
32
|
+
|
|
33
|
+
// 3. local nodes
|
|
34
|
+
const nodes: NodeStateMap<S> = {}
|
|
35
|
+
|
|
36
|
+
// 4. reducer.
|
|
37
|
+
// using useState to have theDo.default as optional.
|
|
38
|
+
// theReducer won't be changed.
|
|
39
|
+
//
|
|
40
|
+
// theReducer is different for different reducers,
|
|
41
|
+
// even within the same class.
|
|
42
|
+
// However, because theReducer is a pure function
|
|
43
|
+
// having ClassState as the input. It is ok to have
|
|
44
|
+
// different reducers within the same class.
|
|
45
|
+
const [theReducer, _] = useState(() => theDo.default ?? createReducer<S>())
|
|
46
|
+
|
|
47
|
+
// 5. useThunkReducer
|
|
48
|
+
const [state, dispatch] = useThunkReducer(
|
|
49
|
+
theReducer,
|
|
50
|
+
{
|
|
51
|
+
myClass,
|
|
52
|
+
// @ts-expect-error doMe is a hidden variable for ClassState
|
|
53
|
+
doMe: dispatchMap,
|
|
54
|
+
nodes,
|
|
55
|
+
},
|
|
56
|
+
init,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
// 7. the dispatchMap is always the same.
|
|
60
|
+
// we can do early return if not first time.
|
|
61
|
+
if (!isFirstTime) {
|
|
62
|
+
return [state, dispatchMap]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 8. setup dispatchMap
|
|
66
|
+
Object.keys(theDo)
|
|
67
|
+
// default and myClass are reserved words.
|
|
68
|
+
// functions starting reduce are included in default and not exported.
|
|
69
|
+
.filter((each) => typeof theDo[each] === 'function')
|
|
70
|
+
.reduce((val, eachAction) => {
|
|
71
|
+
const action: ActionFunc<S> = theDo[eachAction]
|
|
72
|
+
// @ts-expect-error eachAction is in DispatchFuncMap<S, R>
|
|
73
|
+
// biome-ignore lint/suspicious/noExplicitAny: action parameters can be any types.
|
|
74
|
+
val[eachAction] = (...params: any[]) => dispatch(action(...params))
|
|
75
|
+
return val
|
|
76
|
+
}, dispatchMap)
|
|
77
|
+
|
|
78
|
+
// 9. default functions for disapatchMap
|
|
79
|
+
Object.keys(DEFAULT_THUNK_MODULE_FUNC_MAP).reduce((val, eachAction) => {
|
|
80
|
+
if (val[eachAction]) {
|
|
81
|
+
return val
|
|
82
|
+
}
|
|
83
|
+
// @ts-expect-error DEFAULT_REDUCER_MODULE_FUNCS are all ActionFunc<S>
|
|
84
|
+
const action: ActionFunc<S> = DEFAULT_THUNK_MODULE_FUNC_MAP[eachAction]
|
|
85
|
+
|
|
86
|
+
// @ts-expect-error eachAction is in DispatchFuncMap<S, R>
|
|
87
|
+
// biome-ignore lint/suspicious/noExplicitAny: action parameters can be any types.
|
|
88
|
+
val[eachAction] = (...params: any[]) => dispatch(action(...params))
|
|
89
|
+
return val
|
|
90
|
+
}, dispatchMap)
|
|
91
|
+
|
|
92
|
+
return [state, dispatchMap]
|
|
93
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ClassState, State } from './stateTypes';
|
|
2
|
+
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './thunkReducer';
|
|
3
|
+
export interface BaseAction {
|
|
4
|
+
myID: string;
|
|
5
|
+
type: string;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
}
|
|
8
|
+
export type Thunk<S extends State> = rThunk<ClassState<S>, BaseAction>;
|
|
9
|
+
export type ActionOrThunk<S extends State> = rActionOrThunk<ClassState<S>, BaseAction>;
|
|
10
|
+
export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>;
|
|
11
|
+
export type GetClassState<S extends State> = () => ClassState<S>;
|
|
@@ -0,0 +1,5 @@
|
|
|
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>(state: ClassState<S>, action: AddRelationAction) => ClassState<S>;
|
|
@@ -0,0 +1,6 @@
|
|
|
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>(state: ClassState<S>, action: AddRelationAction) => ClassState<S>;
|
|
@@ -0,0 +1,6 @@
|
|
|
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>(state: ClassState<S>, action: AddRelationAction, relationName: Relation) => ClassState<S>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { State } from './stateTypes';
|
|
2
|
+
import type { ThunkModuleFunc } from './thunk';
|
|
3
|
+
import type { DefaultThunkModuleFuncMap } from './thunkModuleFuncMap';
|
|
4
|
+
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
|
|
5
|
+
export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
6
|
+
[action in keyof T]: VoidReturnType<T[action]>;
|
|
7
|
+
} & Omit<DefaultDispatchFuncMap, keyof T>;
|
|
8
|
+
export type DefaultDispatchFuncMap = {
|
|
9
|
+
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
|
|
10
|
+
};
|
|
11
|
+
export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
12
|
+
[className: string]: DispatchFuncMap<S, T>;
|
|
13
|
+
}
|
|
14
|
+
export interface RefDispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
15
|
+
current: DispatchFuncMapByClassMap<S, T>;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const genUUID: (myuuidv4?: () => string) => string;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type NodeState, type State } from './stateTypes';
|
|
2
|
+
export declare const getChildIDs: <S extends State>(me: NodeState<S>, childClass: string) => string[];
|
|
3
|
+
export declare const getChildID: <S extends State>(me: NodeState<S>, childClass: string) => string;
|
|
4
|
+
export declare const getLinkIDs: <S extends State>(me: NodeState<S>, linkClass: string) => string[];
|
|
5
|
+
export declare const getLinkID: <S extends State>(me: NodeState<S>, linkClass: string) => string;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { GetClassState, Thunk } from './action';
|
|
2
|
+
import { addChild } from './addChild';
|
|
3
|
+
import { addLink } from './addLink';
|
|
4
|
+
import type { AddRelationAction } from './addRelation';
|
|
5
|
+
import type { Dispatch } from './dispatch';
|
|
6
|
+
import type { DispatchFuncMap } from './dispatchFuncMap';
|
|
7
|
+
import { genUUID } from './genUUID';
|
|
8
|
+
import { getChildID, getChildIDs, getLinkID, getLinkIDs } from './getRelation';
|
|
9
|
+
import { type InitParams, init } from './init';
|
|
10
|
+
import { remove } from './remove';
|
|
11
|
+
import { removeChild } from './removeChild';
|
|
12
|
+
import { removeLink } from './removeLink';
|
|
13
|
+
import type { RemoveRelationAction } from './removeRelation';
|
|
14
|
+
import { setData } from './setData';
|
|
15
|
+
import { getNode, getRootID, getState } from './states';
|
|
16
|
+
import type { ClassState, NodeMeta, NodeState, NodeStateMap, State } from './stateTypes';
|
|
17
|
+
import type { ThunkModule, ThunkModuleToFunc } from './thunk';
|
|
18
|
+
import useThunk from './useThunk';
|
|
19
|
+
export { useThunk, type State, type NodeState, type NodeMeta, type NodeStateMap, type ClassState, type GetClassState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type Dispatch, type DispatchFuncMap, getRootID, getNode, getState, getChildIDs, getChildID, getLinkIDs, getLinkID, init, type InitParams, setData, remove, addChild, removeChild, addLink, removeLink, type AddRelationAction, type RemoveRelationAction, genUUID, };
|
package/types/init.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { BaseAction, Thunk } from './action';
|
|
2
|
+
import { type ClassState, type State } from './stateTypes';
|
|
3
|
+
export interface InitParams<S extends State> {
|
|
4
|
+
myID?: string;
|
|
5
|
+
parentID?: string;
|
|
6
|
+
doParent?: DispatchFuncMap;
|
|
7
|
+
parentClass?: string;
|
|
8
|
+
state: S;
|
|
9
|
+
}
|
|
10
|
+
export declare const init: <S extends State>(params: InitParams<S>, myuuidv4?: () => string) => Thunk<S>;
|
|
11
|
+
interface InitAction<S extends State> extends BaseAction {
|
|
12
|
+
parentID?: string;
|
|
13
|
+
doParent?: DispatchFuncMap;
|
|
14
|
+
parentClass?: string;
|
|
15
|
+
state: S;
|
|
16
|
+
}
|
|
17
|
+
export declare const INIT = "@chhsiao1981/use-thunk/INIT";
|
|
18
|
+
export declare const reduceInit: <S extends State>(state: ClassState<S>, action: InitAction<S>) => ClassState<S>;
|
|
19
|
+
export {};
|
|
@@ -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,10 @@
|
|
|
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;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseAction, Thunk } from './action';
|
|
2
|
+
import { type ClassState, type State } from './stateTypes';
|
|
3
|
+
export declare const remove: <S extends State>(myID: string, isFromParent?: boolean) => Thunk<S>;
|
|
4
|
+
export declare const REMOVE = "@chhsiao1981/use-thunk/REMOVE";
|
|
5
|
+
export declare const reduceRemove: <S extends State>(state: ClassState<S>, action: BaseAction) => ClassState<S>;
|
|
@@ -0,0 +1,9 @@
|
|
|
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>(state: ClassState<S>, action: RemoveRelationAction) => ClassState<S>;
|
|
@@ -0,0 +1,9 @@
|
|
|
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>(state: ClassState<S>, action: RemoveRelationAction) => ClassState<S>;
|
|
@@ -0,0 +1,12 @@
|
|
|
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>(state: ClassState<S>, myID: string, relationID: string, relationClass: string, relationName: Relation.LINKS | Relation.CHILDREN) => ClassState<S>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseAction } from './action';
|
|
2
|
+
import type { ClassState, State } from './stateTypes';
|
|
3
|
+
export declare const SET_DATA = "@chhsiao1981/use-thunk/SET_DATA";
|
|
4
|
+
export declare const setData: <S extends State>(myID: string, data: S) => BaseAction;
|
|
5
|
+
export declare const reduceSetData: <S extends State>(state: 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_ROOT = "@chhsiao1981/use-thunk/SET_ROOT";
|
|
4
|
+
export declare const setRoot: (myID: string) => BaseAction;
|
|
5
|
+
export declare const reduceSetRoot: <S extends State>(state: ClassState<S>, action: BaseAction) => ClassState<S>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare enum StateType {
|
|
2
|
+
LOCAL = "local"
|
|
3
|
+
}
|
|
4
|
+
export declare enum Relation {
|
|
5
|
+
CHILDREN = "_children",
|
|
6
|
+
LINKS = "_links"
|
|
7
|
+
}
|
|
8
|
+
export declare const PARENT = "_parent";
|
|
9
|
+
export interface State {
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
export type NodeState<S extends State> = {
|
|
13
|
+
id: string;
|
|
14
|
+
state: S;
|
|
15
|
+
[Relation.CHILDREN]?: NodeStateRelationMap | null;
|
|
16
|
+
[PARENT]?: NodeMeta | null;
|
|
17
|
+
[Relation.LINKS]?: NodeStateRelationMap | null;
|
|
18
|
+
};
|
|
19
|
+
type NodeStateRelationMap = {
|
|
20
|
+
[relationClass: string]: NodeStateRelation;
|
|
21
|
+
};
|
|
22
|
+
type NodeStateRelation = {
|
|
23
|
+
list: string[];
|
|
24
|
+
do: DispatchFuncMap;
|
|
25
|
+
};
|
|
26
|
+
export type NodeStateMap<S extends State> = {
|
|
27
|
+
[key: string]: NodeState<S>;
|
|
28
|
+
};
|
|
29
|
+
export type NodeStateMapByClass<S extends State> = {
|
|
30
|
+
[className: string]: NodeStateMap<S>;
|
|
31
|
+
};
|
|
32
|
+
export type ClassState<S extends State> = {
|
|
33
|
+
myClass: string;
|
|
34
|
+
root?: string | null;
|
|
35
|
+
nodes: NodeStateMap<S>;
|
|
36
|
+
};
|
|
37
|
+
export type NodeMeta = {
|
|
38
|
+
id: string;
|
|
39
|
+
theClass: string;
|
|
40
|
+
do: DispatchFuncMap;
|
|
41
|
+
};
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ClassState, NodeState, State } from './stateTypes';
|
|
2
|
+
export declare const getRootNode: <S extends State>(state: ClassState<S>) => NodeState<S> | null;
|
|
3
|
+
export declare const getRootID: <S extends State>(state: ClassState<S>) => string;
|
|
4
|
+
export declare const getRoot: <S extends State>(state: ClassState<S>) => S | null;
|
|
5
|
+
export declare const getNode: <S extends State>(state: ClassState<S>, myID?: string) => NodeState<S> | null;
|
|
6
|
+
export declare const getState: <S extends State>(state: ClassState<S>, myID?: string) => S | null;
|
|
@@ -0,0 +1,16 @@
|
|
|
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;
|
package/types/thunk.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ActionFunc } from './action';
|
|
2
|
+
import type { Reducer } from './reducer';
|
|
3
|
+
import type { State } from './stateTypes';
|
|
4
|
+
export interface ThunkModuleFunc<S extends State> {
|
|
5
|
+
[action: string]: ActionFunc<S>;
|
|
6
|
+
}
|
|
7
|
+
export type ThunkModule<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
8
|
+
myClass: string;
|
|
9
|
+
default?: Reducer<S>;
|
|
10
|
+
defaultState?: S;
|
|
11
|
+
} & T;
|
|
12
|
+
export type ThunkModuleToFunc<T> = Omit<T, 'myClass' | 'default' | 'defaultState'>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const DEFAULT_THUNK_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 DefaultThunkModuleFuncMap = typeof DEFAULT_THUNK_MODULE_FUNC_MAP;
|
|
@@ -0,0 +1,18 @@
|
|
|
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;
|
|
@@ -0,0 +1,8 @@
|
|
|
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;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DispatchFuncMap } from './dispatchFuncMap';
|
|
2
|
+
import type { ClassState, State } from './stateTypes';
|
|
3
|
+
import type { ThunkModule, ThunkModuleFunc } from './thunk';
|
|
4
|
+
/**********
|
|
5
|
+
* useThunk
|
|
6
|
+
**********/
|
|
7
|
+
declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S, R>, init?: (...params: any[]) => S) => [ClassState<S>, DispatchFuncMap<S, R>];
|
|
8
|
+
export default _default;
|