@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.
- package/README.md +63 -59
- package/dist/ThunkContext.d.ts +7 -0
- package/dist/createReducer.d.ts +4 -0
- package/dist/createThunk.d.ts +4 -0
- package/dist/dispatch.d.ts +4 -0
- package/dist/dispatchFuncMap.d.ts +18 -0
- package/dist/genUUID.d.ts +1 -0
- package/dist/index.d.ts +15 -0
- package/{types → dist}/init.d.ts +0 -3
- package/dist/reduceMap.d.ts +6 -0
- package/dist/reducer.d.ts +5 -0
- package/dist/remove.d.ts +5 -0
- package/dist/setDefaultID.d.ts +5 -0
- package/dist/stateTypes.d.ts +16 -0
- package/dist/states.d.ts +9 -0
- package/{types → dist}/thunk.d.ts +6 -3
- package/dist/thunkContextMap.d.ts +15 -0
- package/dist/thunkContextTypes.d.ts +8 -0
- package/{types → dist}/thunkModuleFuncMap.d.ts +1 -0
- package/dist/update.d.ts +6 -0
- package/dist/useThunk.d.ts +12 -0
- package/dist/useThunkReducer.d.ts +17 -0
- package/package.json +1 -1
- package/src/ThunkContext.tsx +17 -17
- package/src/action/ActionOrThunk.ts +5 -0
- package/src/action/baseAction.ts +6 -0
- package/src/action/index.ts +16 -0
- package/src/action/thunk.ts +9 -0
- package/src/createReducer.ts +7 -7
- package/src/createThunk.ts +24 -0
- package/src/get.ts +3 -0
- package/src/index.ts +20 -30
- package/src/init/index.ts +26 -0
- package/src/init/initCore.ts +31 -0
- package/src/reduceMap.ts +3 -4
- package/src/reducer.ts +4 -4
- package/src/remove.ts +10 -25
- package/src/set.ts +7 -0
- package/src/setDefaultID.ts +5 -5
- package/src/setMap.ts +65 -0
- package/src/stateTypes.ts +3 -3
- package/src/states.ts +21 -18
- package/src/thunkContextMap.ts +4 -4
- package/src/thunkContextTypes.ts +4 -4
- package/src/thunkModule/defaultThunkModuleFuncMap.ts +15 -0
- package/src/thunkModule/index.ts +20 -0
- package/src/update.ts +26 -0
- package/src/useThunk.ts +23 -44
- package/src/useThunkReducer.ts +41 -42
- package/types/ThunkContext.d.ts +4 -4
- package/types/action/ActionOrThunk.d.ts +4 -0
- package/types/action/baseAction.d.ts +5 -0
- package/types/action/index.d.ts +8 -0
- package/types/action/thunk.d.ts +3 -0
- package/types/createThunk.d.ts +4 -0
- package/types/dispatch.d.ts +1 -1
- package/types/dispatchFuncMap.d.ts +4 -5
- package/types/get.d.ts +2 -0
- package/types/index.d.ts +10 -9
- package/types/init/index.d.ts +7 -0
- package/types/init/initCore.d.ts +9 -0
- package/types/reducer.d.ts +4 -4
- package/types/remove.d.ts +4 -4
- package/types/set.d.ts +4 -0
- package/types/setDefaultID.d.ts +3 -3
- package/types/setMap.d.ts +17 -0
- package/types/stateTypes.d.ts +2 -2
- package/types/states.d.ts +8 -8
- package/types/thunkContextMap.d.ts +4 -4
- package/types/thunkContextTypes.d.ts +4 -4
- package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +5 -0
- package/types/thunkModule/index.d.ts +15 -0
- package/types/update.d.ts +5 -0
- package/types/useThunk.d.ts +5 -8
- package/types/useThunkReducer.d.ts +5 -11
- package/dist/index.js +0 -1723
- package/dist/index.umd.cjs +0 -50
- package/src/action.ts +0 -20
- package/src/dispatch.ts +0 -6
- package/src/dispatchFuncMap.ts +0 -67
- package/src/init.ts +0 -64
- package/src/registerThunk.ts +0 -42
- package/src/setData.ts +0 -26
- package/src/thunk.ts +0 -20
- package/src/thunkModuleFuncMap.ts +0 -11
- package/types/registerThunk.d.ts +0 -4
- package/types/setData.d.ts +0 -5
- /package/{types → dist}/action.d.ts +0 -0
package/src/useThunkReducer.ts
CHANGED
|
@@ -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 {
|
|
5
|
-
import type
|
|
6
|
-
import type {
|
|
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
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
37
|
-
(
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const get = useCallback(
|
|
35
|
+
(id?: string) => {
|
|
36
|
+
const moduleState = getModuleState()
|
|
37
|
+
const state = getStateOrNullByModule(moduleState, id)
|
|
38
|
+
return state
|
|
40
39
|
},
|
|
41
|
-
[
|
|
40
|
+
[getModuleState],
|
|
42
41
|
)
|
|
43
42
|
|
|
44
43
|
// 5. reducer.
|
|
45
44
|
const reduce = useCallback(
|
|
46
|
-
(action:
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
return
|
|
45
|
+
(action: BaseAction): ModuleState<S> => {
|
|
46
|
+
const moduleState = getModuleState()
|
|
47
|
+
const newModuleState = reducer(moduleState, action)
|
|
48
|
+
return newModuleState
|
|
50
49
|
},
|
|
51
|
-
[reducer,
|
|
50
|
+
[reducer, getModuleState],
|
|
52
51
|
)
|
|
53
52
|
|
|
54
|
-
// augmented
|
|
55
|
-
const
|
|
56
|
-
(action
|
|
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(
|
|
58
|
+
action(set, get, getModuleState)
|
|
60
59
|
return
|
|
61
60
|
}
|
|
62
61
|
|
|
63
|
-
// action is not function. so action is
|
|
64
|
-
const
|
|
65
|
-
|
|
62
|
+
// action is not function. so action is BaseAction
|
|
63
|
+
const newModuleState = reduce(action)
|
|
64
|
+
setModuleState(newModuleState)
|
|
66
65
|
},
|
|
67
|
-
[
|
|
66
|
+
[getModuleState, setModuleState, reduce],
|
|
68
67
|
)
|
|
69
68
|
|
|
70
|
-
return [
|
|
69
|
+
return [refModuleState.current, set]
|
|
71
70
|
}
|
package/types/ThunkContext.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
2
|
type Props = {
|
|
3
|
-
|
|
4
|
-
children?:
|
|
3
|
+
modules?: string[];
|
|
4
|
+
children?: ReactNode;
|
|
5
5
|
};
|
|
6
|
-
declare const ThunkContext: (props: Props) =>
|
|
6
|
+
declare const ThunkContext: (props: Props) => ReactNode;
|
|
7
7
|
export default ThunkContext;
|
|
@@ -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>;
|
package/types/dispatch.d.ts
CHANGED
|
@@ -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 {
|
|
1
|
+
import type { Dispatch } from './dispatch';
|
|
2
2
|
import type { State } from './stateTypes';
|
|
3
|
-
import type { ThunkModule, ThunkModuleFunc } from './
|
|
4
|
-
import { type DefaultThunkModuleFuncMap } from './
|
|
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
|
|
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
package/types/index.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
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 {
|
|
7
|
+
import type { set } from './set';
|
|
9
8
|
import { setDefaultID } from './setDefaultID';
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
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 './
|
|
13
|
+
import type { ThunkModule, ThunkModuleToFunc } from './thunkModule';
|
|
14
|
+
import { update } from './update';
|
|
14
15
|
import useThunk, { type UseThunk } from './useThunk';
|
|
15
|
-
export {
|
|
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>;
|
package/types/reducer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Reducer as rReducer } from 'react';
|
|
2
|
-
import type
|
|
3
|
-
import type {
|
|
4
|
-
export type Reducer<S extends State> = rReducer<
|
|
5
|
-
export type ReduceFunc<S extends State> = (state:
|
|
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
|
|
2
|
-
import type {
|
|
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
|
|
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
package/types/setDefaultID.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
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>(
|
|
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 {};
|
package/types/stateTypes.d.ts
CHANGED
|
@@ -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
|
|
12
|
-
|
|
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 {
|
|
2
|
-
import type {
|
|
3
|
-
import type { ThunkModuleFunc } from './
|
|
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>(
|
|
6
|
-
export declare const getNode: <S extends State>(
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const
|
|
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 {
|
|
2
|
+
import type { ModuleState } from './stateTypes';
|
|
3
3
|
import type { Context } from './thunkContextTypes';
|
|
4
4
|
export type ThunkContextMap = {
|
|
5
5
|
theMap: {
|
|
6
|
-
[
|
|
6
|
+
[moduleName: string]: {
|
|
7
7
|
context: rContext<Context<any>>;
|
|
8
|
-
|
|
9
|
-
current:
|
|
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 {
|
|
2
|
+
import type { ModuleState, State } from './stateTypes';
|
|
3
3
|
export type Context<S extends State> = {
|
|
4
|
-
|
|
5
|
-
current:
|
|
4
|
+
refModuleState: {
|
|
5
|
+
current: ModuleState<S>;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
setModuleState: Dispatch<SetStateAction<ModuleState<S>>>;
|
|
8
8
|
};
|
|
@@ -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>;
|
package/types/useThunk.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type {
|
|
3
|
-
import type { ThunkModule, ThunkModuleFunc } from './
|
|
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
|
|
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
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
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
|
|
10
|
+
declare const _default: <S extends State>(reducer: Reducer<S>, moduleName: string) => [ModuleState<S>, set<S>];
|
|
17
11
|
export default _default;
|