@chhsiao1981/use-thunk 12.0.0 → 14.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 +211 -122
- package/{types → dist}/action/ActionOrThunk.d.ts +1 -1
- package/{types → dist}/action/index.d.ts +2 -2
- package/dist/action/thunk.d.ts +5 -0
- package/dist/defaultThunks/index.d.ts +10 -0
- package/dist/defaultThunks/init/index.d.ts +9 -0
- package/{types → dist/defaultThunks}/init/initCore.d.ts +2 -2
- package/{types → dist/defaultThunks}/remove.d.ts +2 -2
- package/{types → dist/defaultThunks}/setDefaultID.d.ts +2 -2
- package/{types → dist/defaultThunks}/update.d.ts +2 -2
- package/dist/defaultThunks/upsert.d.ts +5 -0
- package/dist/dispatch.d.ts +3 -3
- package/dist/get.d.ts +3 -0
- package/dist/index.d.ts +11 -14
- package/dist/index.js +173 -0
- package/dist/index.umd.cjs +1 -0
- package/dist/reducer/createReducer.d.ts +3 -0
- package/dist/reducer/defaultReduceMap.d.ts +2 -0
- package/{types/reducer.d.ts → dist/reducer/index.d.ts} +4 -2
- package/dist/reducer/reduceMap.d.ts +5 -0
- package/{types/createThunk.d.ts → dist/registerThunk.d.ts} +1 -1
- package/dist/set/index.d.ts +5 -0
- package/dist/set/setMap.d.ts +16 -0
- package/dist/states/index.d.ts +10 -0
- package/{types/stateTypes.d.ts → dist/states/types.d.ts} +5 -4
- package/dist/thunkContext/ThunkContext.d.ts +7 -0
- package/dist/thunkContext/index.d.ts +6 -0
- package/{types → dist/thunkContext}/thunkContextMap.d.ts +2 -2
- package/{types/thunkContextTypes.d.ts → dist/thunkContext/types.d.ts} +1 -1
- package/dist/thunkModule/defaultDoModule.d.ts +5 -0
- package/dist/thunkModule/index.d.ts +13 -0
- package/dist/useThunk/index.d.ts +9 -0
- package/{types → dist/useThunk}/useThunkReducer.d.ts +3 -3
- package/dist/utils/deepCopy.d.ts +2 -0
- package/dist/utils/genID.d.ts +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/package.json +35 -30
- package/dist/ThunkContext.d.ts +0 -7
- package/dist/action.d.ts +0 -11
- package/dist/createReducer.d.ts +0 -4
- package/dist/createThunk.d.ts +0 -4
- package/dist/dispatchFuncMap.d.ts +0 -18
- package/dist/genUUID.d.ts +0 -1
- package/dist/init.d.ts +0 -16
- package/dist/reduceMap.d.ts +0 -6
- package/dist/reducer.d.ts +0 -5
- package/dist/remove.d.ts +0 -5
- package/dist/setDefaultID.d.ts +0 -5
- package/dist/stateTypes.d.ts +0 -16
- package/dist/states.d.ts +0 -9
- package/dist/thunk.d.ts +0 -15
- package/dist/thunkContextMap.d.ts +0 -15
- package/dist/thunkContextTypes.d.ts +0 -8
- package/dist/thunkModuleFuncMap.d.ts +0 -7
- package/dist/update.d.ts +0 -6
- package/dist/useThunk.d.ts +0 -12
- package/dist/useThunkReducer.d.ts +0 -17
- package/src/ThunkContext.tsx +0 -46
- package/src/action/ActionOrThunk.ts +0 -5
- package/src/action/baseAction.ts +0 -6
- package/src/action/index.ts +0 -16
- package/src/action/thunk.ts +0 -9
- package/src/createReducer.ts +0 -24
- package/src/createThunk.ts +0 -24
- package/src/genUUID.ts +0 -38
- package/src/get.ts +0 -3
- package/src/index.ts +0 -42
- package/src/init/index.ts +0 -26
- package/src/init/initCore.ts +0 -31
- package/src/reduceMap.ts +0 -22
- package/src/reducer.ts +0 -9
- package/src/remove.ts +0 -35
- package/src/set.ts +0 -7
- package/src/setDefaultID.ts +0 -17
- package/src/setMap.ts +0 -65
- package/src/stateTypes.ts +0 -22
- package/src/states.ts +0 -50
- package/src/thunkContextMap.ts +0 -22
- package/src/thunkContextTypes.ts +0 -8
- package/src/thunkModule/defaultThunkModuleFuncMap.ts +0 -15
- package/src/thunkModule/index.ts +0 -20
- package/src/update.ts +0 -26
- package/src/useThunk.ts +0 -50
- package/src/useThunkReducer.ts +0 -70
- package/types/ThunkContext.d.ts +0 -7
- package/types/action/thunk.d.ts +0 -3
- package/types/createReducer.d.ts +0 -4
- package/types/dispatch.d.ts +0 -4
- package/types/dispatchFuncMap.d.ts +0 -17
- package/types/genUUID.d.ts +0 -1
- package/types/get.d.ts +0 -2
- package/types/index.d.ts +0 -16
- package/types/init/index.d.ts +0 -7
- package/types/reduceMap.d.ts +0 -6
- package/types/set.d.ts +0 -4
- package/types/setMap.d.ts +0 -17
- package/types/states.d.ts +0 -9
- package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +0 -5
- package/types/thunkModule/index.d.ts +0 -15
- package/types/useThunk.d.ts +0 -9
- /package/{types → dist}/action/baseAction.d.ts +0 -0
package/src/update.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type BaseAction from './action/baseAction'
|
|
2
|
-
import type { ModuleState, State } from './stateTypes'
|
|
3
|
-
|
|
4
|
-
export const UPDATE = '@chhsiao1981/use-thunk/UPDATE'
|
|
5
|
-
export const update = <S extends State>(myID: string, data: Partial<S>): BaseAction => ({
|
|
6
|
-
myID,
|
|
7
|
-
type: UPDATE,
|
|
8
|
-
data,
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
export const reduceUpdate = <S extends State>(
|
|
12
|
-
moduleState: ModuleState<S>,
|
|
13
|
-
action: BaseAction,
|
|
14
|
-
): ModuleState<S> => {
|
|
15
|
-
const { myID, data } = action
|
|
16
|
-
|
|
17
|
-
const myNode = moduleState.nodes[myID]
|
|
18
|
-
if (!myNode) return moduleState
|
|
19
|
-
|
|
20
|
-
const newMyState = Object.assign({}, myNode.state, data)
|
|
21
|
-
const newMyNode = Object.assign({}, myNode, { state: newMyState })
|
|
22
|
-
const newNodes = Object.assign({}, moduleState.nodes, { [myID]: newMyNode })
|
|
23
|
-
const newModuleState = Object.assign({}, moduleState, { nodes: newNodes })
|
|
24
|
-
|
|
25
|
-
return newModuleState
|
|
26
|
-
}
|
package/src/useThunk.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react'
|
|
2
|
-
import { createReducer } from './createReducer'
|
|
3
|
-
import { constructSetMap, type setMap, type setMapByModuleMap } from './setMap'
|
|
4
|
-
import type { ModuleState, State } from './stateTypes'
|
|
5
|
-
import type { ThunkModule, ThunkModuleFunc } from './thunkModule'
|
|
6
|
-
import useThunkReducer from './useThunkReducer'
|
|
7
|
-
|
|
8
|
-
// biome-ignore lint/suspicious/noExplicitAny: SET_MAP_BY_MODULE can by any type
|
|
9
|
-
const SET_MAP_BY_MODULE: setMapByModuleMap<any, any> = {}
|
|
10
|
-
|
|
11
|
-
export type UseThunk<S extends State, R extends ThunkModuleFunc<S>> = [ModuleState<S>, setMap<S, R>]
|
|
12
|
-
|
|
13
|
-
/**********
|
|
14
|
-
* useThunk
|
|
15
|
-
**********/
|
|
16
|
-
export default <S extends State, R extends ThunkModuleFunc<S>>(
|
|
17
|
-
theDo: ThunkModule<S>,
|
|
18
|
-
): UseThunk<S, R> => {
|
|
19
|
-
const { name } = theDo
|
|
20
|
-
|
|
21
|
-
// 1. It requires shared nodes for the same module to have the same setMap.
|
|
22
|
-
const isFirstTime = !SET_MAP_BY_MODULE[name]
|
|
23
|
-
if (isFirstTime) {
|
|
24
|
-
SET_MAP_BY_MODULE[name] = {}
|
|
25
|
-
}
|
|
26
|
-
const setMap = SET_MAP_BY_MODULE[name] as setMap<S, R>
|
|
27
|
-
|
|
28
|
-
// 2. reducer.
|
|
29
|
-
// theReducer is different for different useThunk,
|
|
30
|
-
// even within the same module.
|
|
31
|
-
// However, because theReducer is a pure function by
|
|
32
|
-
// having ModuleState as the input. It is ok to have
|
|
33
|
-
// different reducers within the same module.
|
|
34
|
-
const theReducer = useMemo(() => createReducer<S>(), [])
|
|
35
|
-
|
|
36
|
-
// 3. useThunkReducer
|
|
37
|
-
const [moduleState, set] = useThunkReducer(theReducer, name)
|
|
38
|
-
|
|
39
|
-
const ret: UseThunk<S, R> = useMemo(() => {
|
|
40
|
-
return [moduleState, setMap]
|
|
41
|
-
}, [moduleState, setMap])
|
|
42
|
-
|
|
43
|
-
if (!isFirstTime) {
|
|
44
|
-
return ret
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
constructSetMap(theDo, set, setMap)
|
|
48
|
-
|
|
49
|
-
return ret
|
|
50
|
-
}
|
package/src/useThunkReducer.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
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 { 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'
|
|
10
|
-
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* useThunkReducer
|
|
14
|
-
*
|
|
15
|
-
* Augments React's useReducer() hook so that the action
|
|
16
|
-
* setter (dispatcher) supports thunks.
|
|
17
|
-
*/
|
|
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>
|
|
25
|
-
|
|
26
|
-
const setModuleState = useCallback(
|
|
27
|
-
(newModuleState: ModuleState<S>) => {
|
|
28
|
-
refModuleState.current = newModuleState
|
|
29
|
-
setModuleState_c(newModuleState)
|
|
30
|
-
},
|
|
31
|
-
[refModuleState, setModuleState_c],
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
const get = useCallback(
|
|
35
|
-
(id?: string) => {
|
|
36
|
-
const moduleState = getModuleState()
|
|
37
|
-
const state = getStateOrNullByModule(moduleState, id)
|
|
38
|
-
return state
|
|
39
|
-
},
|
|
40
|
-
[getModuleState],
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
// 5. reducer.
|
|
44
|
-
const reduce = useCallback(
|
|
45
|
-
(action: BaseAction): ModuleState<S> => {
|
|
46
|
-
const moduleState = getModuleState()
|
|
47
|
-
const newModuleState = reducer(moduleState, action)
|
|
48
|
-
return newModuleState
|
|
49
|
-
},
|
|
50
|
-
[reducer, getModuleState],
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
// augmented setter.
|
|
54
|
-
const set: set<S> = useCallback(
|
|
55
|
-
(action) => {
|
|
56
|
-
if (typeof action === 'function') {
|
|
57
|
-
// action is Thunk<S, A>
|
|
58
|
-
action(set, get, getModuleState)
|
|
59
|
-
return
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// action is not function. so action is BaseAction
|
|
63
|
-
const newModuleState = reduce(action)
|
|
64
|
-
setModuleState(newModuleState)
|
|
65
|
-
},
|
|
66
|
-
[getModuleState, setModuleState, reduce],
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
return [refModuleState.current, set]
|
|
70
|
-
}
|
package/types/ThunkContext.d.ts
DELETED
package/types/action/thunk.d.ts
DELETED
package/types/createReducer.d.ts
DELETED
package/types/dispatch.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Dispatch } from './dispatch';
|
|
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 DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
7
|
-
[action in keyof T]: VoidReturnType<T[action]>;
|
|
8
|
-
} & Omit<DefaultDispatchFuncMap, keyof T>;
|
|
9
|
-
export type DefaultDispatchFuncMap = {
|
|
10
|
-
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
|
|
11
|
-
};
|
|
12
|
-
export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
13
|
-
[className: string]: DispatchFuncMap<S, T>;
|
|
14
|
-
}
|
|
15
|
-
export declare const DISPATCH_FUNC_MAP_BY_CLASS_MAP: DispatchFuncMapByClassMap<any, any>;
|
|
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>;
|
|
17
|
-
export {};
|
package/types/genUUID.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const genUUID: (myuuidv4?: () => string) => string;
|
package/types/get.d.ts
DELETED
package/types/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { GetModuleState, Thunk } from './action';
|
|
2
|
-
import createThunk from './createThunk';
|
|
3
|
-
import { genUUID } from './genUUID';
|
|
4
|
-
import type { get } from './get';
|
|
5
|
-
import { type InitParams, init } from './init';
|
|
6
|
-
import { remove } from './remove';
|
|
7
|
-
import type { set } from './set';
|
|
8
|
-
import { setDefaultID } from './setDefaultID';
|
|
9
|
-
import type { setMap } from './setMap';
|
|
10
|
-
import { getDefaultID, getNode, getState, getStateByModule, getStateOrNullByModule } from './states';
|
|
11
|
-
import type { ModuleState, State } from './stateTypes';
|
|
12
|
-
import ThunkContext from './ThunkContext';
|
|
13
|
-
import type { ThunkModule, ThunkModuleToFunc } from './thunkModule';
|
|
14
|
-
import { update } from './update';
|
|
15
|
-
import useThunk, { type UseThunk } from './useThunk';
|
|
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, };
|
package/types/init/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
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>;
|
package/types/reduceMap.d.ts
DELETED
package/types/set.d.ts
DELETED
package/types/setMap.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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/states.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { setMap } from './setMap';
|
|
2
|
-
import type { ModuleState, NodeState, State } from './stateTypes';
|
|
3
|
-
import type { ThunkModuleFunc } from './thunkModule';
|
|
4
|
-
import type { UseThunk } from './useThunk';
|
|
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,15 +0,0 @@
|
|
|
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'>;
|
package/types/useThunk.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
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>];
|
|
5
|
-
/**********
|
|
6
|
-
* useThunk
|
|
7
|
-
**********/
|
|
8
|
-
declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
|
|
9
|
-
export default _default;
|
|
File without changes
|