@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
|
@@ -1,7 +0,0 @@
|
|
|
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
|
-
update: <S extends import("./stateTypes").State>(myID: string, data: Partial<S>) => import("./action").BaseAction;
|
|
4
|
-
setData: <S extends import("./stateTypes").State>(myID: string, data: Partial<S>) => import("./action").BaseAction;
|
|
5
|
-
remove: <S extends import("./stateTypes").State>(myID: string) => import("./action").Thunk<S>;
|
|
6
|
-
};
|
|
7
|
-
export type DefaultThunkModuleFuncMap = typeof DEFAULT_THUNK_MODULE_FUNC_MAP;
|
package/dist/update.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from './action';
|
|
2
|
-
import type { ClassState, 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>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
|
package/dist/useThunk.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
];
|
|
8
|
-
/**********
|
|
9
|
-
* useThunk
|
|
10
|
-
**********/
|
|
11
|
-
declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
|
|
12
|
-
export default _default;
|
|
@@ -1,17 +0,0 @@
|
|
|
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>;
|
|
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 {string} className
|
|
14
|
-
* @returns {[ClassState<S>, Dispatch]}
|
|
15
|
-
*/
|
|
16
|
-
declare const _default: <S extends State, A extends BaseAction>(reducer: Reducer<ClassState<S>, A>, className: string) => [ClassState<S>, Dispatch<A | Thunk<S, A>>];
|
|
17
|
-
export default _default;
|
package/src/ThunkContext.tsx
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { type ReactNode, useMemo, useState } from 'react'
|
|
2
|
-
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
3
|
-
|
|
4
|
-
type Props = {
|
|
5
|
-
modules?: string[]
|
|
6
|
-
children?: ReactNode
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const ThunkContext = (props: Props): ReactNode => {
|
|
10
|
-
const { modules: propsModules, children } = props
|
|
11
|
-
const modules = propsModules || THUNK_CONTEXT_MAP.theList
|
|
12
|
-
// 0. if there is no Thunk modules (no createThunk): return children.
|
|
13
|
-
if (modules.length === 0) {
|
|
14
|
-
return children
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// render the 0th module.
|
|
18
|
-
const theModule = modules[0]
|
|
19
|
-
|
|
20
|
-
// 1. get the context and moduleState from context map.
|
|
21
|
-
const { context: Context_m, refModuleState } = THUNK_CONTEXT_MAP.theMap[theModule]
|
|
22
|
-
|
|
23
|
-
// 2. setup moduleState.
|
|
24
|
-
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
25
|
-
const [moduleState, setModuleState] = useState(refModuleState.current)
|
|
26
|
-
refModuleState.current = moduleState
|
|
27
|
-
|
|
28
|
-
// 3. value reset only if moduleState is changed.
|
|
29
|
-
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
30
|
-
const value = useMemo(
|
|
31
|
-
() => ({
|
|
32
|
-
refModuleState: refModuleState,
|
|
33
|
-
setModuleState: setModuleState,
|
|
34
|
-
}),
|
|
35
|
-
[moduleState],
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
// 4. get theChildren
|
|
39
|
-
const theChildren =
|
|
40
|
-
modules.length === 1 ? children : ThunkContext({ modules: modules.slice(1), children })
|
|
41
|
-
|
|
42
|
-
// 5. return context.
|
|
43
|
-
return <Context_m.Provider value={value}>{theChildren}</Context_m.Provider>
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export default ThunkContext
|
package/src/action/baseAction.ts
DELETED
package/src/action/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
|
|
6
|
-
export type { Thunk, ActionOrThunk, BaseAction }
|
|
7
|
-
|
|
8
|
-
// ActionFunc
|
|
9
|
-
// biome-ignore lint/suspicious/noExplicitAny: params can be any type.
|
|
10
|
-
export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>
|
|
11
|
-
|
|
12
|
-
// biome-ignore lint/suspicious/noExplicitAny: params can by any type.
|
|
13
|
-
export type ThunkFunc<S extends State> = (...params: any[]) => Thunk<S>
|
|
14
|
-
|
|
15
|
-
// GetModuleState
|
|
16
|
-
export type GetModuleState<S extends State> = () => ModuleState<S>
|
package/src/action/thunk.ts
DELETED
package/src/createReducer.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type BaseAction from './action/baseAction'
|
|
2
|
-
import { DEFAULT_REDUCE_MAP, type ReduceMap } from './reduceMap'
|
|
3
|
-
import type { Reducer } from './reducer'
|
|
4
|
-
|
|
5
|
-
import type { ModuleState, State } from './stateTypes'
|
|
6
|
-
|
|
7
|
-
export const createReducer = <S extends State>(reduceMap?: ReduceMap<S>): Reducer<S> => {
|
|
8
|
-
return (moduleState: ModuleState<S>, action: BaseAction): ModuleState<S> => {
|
|
9
|
-
if (!action) {
|
|
10
|
-
return moduleState
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (reduceMap?.[action.type]) {
|
|
14
|
-
return reduceMap[action.type](moduleState, action)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const defaultReduceMap = DEFAULT_REDUCE_MAP<S>()
|
|
18
|
-
if (defaultReduceMap?.[action.type]) {
|
|
19
|
-
return defaultReduceMap[action.type](moduleState, action)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return moduleState
|
|
23
|
-
}
|
|
24
|
-
}
|
package/src/createThunk.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { createContext, type Dispatch, type SetStateAction } from 'react'
|
|
2
|
-
import type { ModuleState, State } from './stateTypes'
|
|
3
|
-
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
4
|
-
import type { ThunkModule } from './thunkModule'
|
|
5
|
-
|
|
6
|
-
export default <S extends State>(theDo: ThunkModule<S>) => {
|
|
7
|
-
const { name, defaultState } = theDo
|
|
8
|
-
|
|
9
|
-
if (THUNK_CONTEXT_MAP.theMap[name]) {
|
|
10
|
-
console.warn('createThunk: already init:', name)
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const moduleState: ModuleState<S> = { name, nodes: {}, defaultState }
|
|
15
|
-
const setModuleState: Dispatch<SetStateAction<ModuleState<S>>> = () => {}
|
|
16
|
-
const refModuleState = { current: moduleState }
|
|
17
|
-
const context = createContext({ refModuleState: refModuleState, setModuleState })
|
|
18
|
-
|
|
19
|
-
THUNK_CONTEXT_MAP.theMap[name] = { context, refModuleState }
|
|
20
|
-
const theList = Object.keys(THUNK_CONTEXT_MAP.theMap).sort()
|
|
21
|
-
THUNK_CONTEXT_MAP.theList = theList
|
|
22
|
-
|
|
23
|
-
console.info('createThunk: done:', name)
|
|
24
|
-
}
|
package/src/genUUID.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid'
|
|
2
|
-
|
|
3
|
-
const _GLOBAL_IDS = new Set()
|
|
4
|
-
|
|
5
|
-
const _GEN_UUID_COUNT = 3
|
|
6
|
-
|
|
7
|
-
const _GEN_UUID_STATE = {
|
|
8
|
-
iterate: 1,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const genUUID = (myuuidv4?: () => string): string => {
|
|
12
|
-
let theID = ''
|
|
13
|
-
let isAdd = false
|
|
14
|
-
for (let i = 0; i < _GEN_UUID_COUNT; i++) {
|
|
15
|
-
theID = genUUIDCore(myuuidv4)
|
|
16
|
-
if (_GLOBAL_IDS.has(theID)) {
|
|
17
|
-
continue
|
|
18
|
-
}
|
|
19
|
-
_GLOBAL_IDS.add(theID)
|
|
20
|
-
isAdd = true
|
|
21
|
-
break
|
|
22
|
-
}
|
|
23
|
-
if (isAdd) {
|
|
24
|
-
return theID
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
_GEN_UUID_STATE.iterate += 1
|
|
28
|
-
theID = genUUIDCore(myuuidv4)
|
|
29
|
-
return theID
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const genUUIDCore = (myuuid: () => string = uuidv4): string => {
|
|
33
|
-
let theID = ''
|
|
34
|
-
for (let j = 0; j < _GEN_UUID_STATE.iterate; j++) {
|
|
35
|
-
theID += myuuid()
|
|
36
|
-
}
|
|
37
|
-
return theID
|
|
38
|
-
}
|
package/src/get.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,42 +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
|
-
|
|
17
|
-
export {
|
|
18
|
-
createThunk,
|
|
19
|
-
useThunk,
|
|
20
|
-
type UseThunk,
|
|
21
|
-
ThunkContext,
|
|
22
|
-
type State,
|
|
23
|
-
type ModuleState,
|
|
24
|
-
type GetModuleState,
|
|
25
|
-
type Thunk,
|
|
26
|
-
type ThunkModule,
|
|
27
|
-
type ThunkModuleToFunc,
|
|
28
|
-
type set,
|
|
29
|
-
type setMap,
|
|
30
|
-
type get,
|
|
31
|
-
getNode,
|
|
32
|
-
getDefaultID,
|
|
33
|
-
getStateOrNullByModule,
|
|
34
|
-
getStateByModule,
|
|
35
|
-
getState,
|
|
36
|
-
init,
|
|
37
|
-
type InitParams,
|
|
38
|
-
update,
|
|
39
|
-
remove,
|
|
40
|
-
setDefaultID,
|
|
41
|
-
genUUID,
|
|
42
|
-
}
|
package/src/init/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Thunk } from '../action'
|
|
2
|
-
import { genUUID } from '../genUUID'
|
|
3
|
-
import { setDefaultID } from '../setDefaultID'
|
|
4
|
-
import type { State } from '../stateTypes'
|
|
5
|
-
import initCore from './initCore'
|
|
6
|
-
|
|
7
|
-
// InitParams
|
|
8
|
-
export interface InitParams<S extends State> {
|
|
9
|
-
myID?: string
|
|
10
|
-
state: S
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const init = <S extends State>(params: InitParams<S>, myuuidv4?: () => string): Thunk<S> => {
|
|
14
|
-
return (set, _, getModuleState) => {
|
|
15
|
-
const myID = params.myID ?? genUUID(myuuidv4)
|
|
16
|
-
|
|
17
|
-
const { state } = params
|
|
18
|
-
set(initCore(myID, state))
|
|
19
|
-
|
|
20
|
-
const { defaultID } = getModuleState()
|
|
21
|
-
|
|
22
|
-
if (!defaultID) {
|
|
23
|
-
set(setDefaultID(myID))
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
package/src/init/initCore.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type BaseAction from '../action/baseAction'
|
|
2
|
-
import type { ModuleState, NodeState, NodeStateMap, State } from '../stateTypes'
|
|
3
|
-
|
|
4
|
-
export interface InitAction<S extends State> extends BaseAction {
|
|
5
|
-
state: S
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const INIT = '@chhsiao1981/use-thunk/INIT'
|
|
9
|
-
export default <S extends State>(myID: string, state: S): InitAction<S> => {
|
|
10
|
-
return {
|
|
11
|
-
myID,
|
|
12
|
-
type: INIT,
|
|
13
|
-
state,
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const reduceInit = <S extends State>(
|
|
18
|
-
moduleState: ModuleState<S>,
|
|
19
|
-
action: BaseAction,
|
|
20
|
-
): ModuleState<S> => {
|
|
21
|
-
const { myID, state } = action as InitAction<S>
|
|
22
|
-
|
|
23
|
-
const myNode: NodeState<S> = {
|
|
24
|
-
id: myID,
|
|
25
|
-
state: state,
|
|
26
|
-
}
|
|
27
|
-
const newNodes: NodeStateMap<S> = Object.assign({}, moduleState.nodes, { [myID]: myNode })
|
|
28
|
-
const newModuleState: ModuleState<S> = Object.assign({}, moduleState, { nodes: newNodes })
|
|
29
|
-
|
|
30
|
-
return newModuleState
|
|
31
|
-
}
|
package/src/reduceMap.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { INIT, reduceInit } from './init/initCore'
|
|
2
|
-
import type { ReduceFunc } from './reducer'
|
|
3
|
-
import { REMOVE, reduceRemove } from './remove'
|
|
4
|
-
import { reduceSetDefaultID, SET_DEFAULT_ID } from './setDefaultID'
|
|
5
|
-
import type { State } from './stateTypes'
|
|
6
|
-
import { reduceUpdate, UPDATE } from './update'
|
|
7
|
-
|
|
8
|
-
export interface ReduceMap<S extends State> {
|
|
9
|
-
[type: string]: ReduceFunc<S>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// default reduceMap
|
|
13
|
-
export const DEFAULT_REDUCE_MAP: <S extends State>() => ReduceMap<S> = () => ({
|
|
14
|
-
[INIT]: reduceInit,
|
|
15
|
-
[UPDATE]: reduceUpdate,
|
|
16
|
-
[REMOVE]: reduceRemove,
|
|
17
|
-
|
|
18
|
-
// setDefaultID.
|
|
19
|
-
// Typically we don't need this in programming.
|
|
20
|
-
// The defaultID is automatically determined if defaultID is not set.
|
|
21
|
-
[SET_DEFAULT_ID]: reduceSetDefaultID,
|
|
22
|
-
})
|
package/src/reducer.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Reducer as rReducer } from 'react'
|
|
2
|
-
import type BaseAction from './action/baseAction'
|
|
3
|
-
import type { ModuleState, State } from './stateTypes'
|
|
4
|
-
|
|
5
|
-
// Reducer
|
|
6
|
-
export type Reducer<S extends State> = rReducer<ModuleState<S>, BaseAction>
|
|
7
|
-
|
|
8
|
-
// ReduceFunc
|
|
9
|
-
export type ReduceFunc<S extends State> = (state: ModuleState<S>, action: BaseAction) => ModuleState<S>
|
package/src/remove.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type BaseAction from './action/baseAction'
|
|
2
|
-
import type { ModuleState, NodeStateMap, State } from './stateTypes'
|
|
3
|
-
|
|
4
|
-
export const REMOVE = '@chhsiao1981/use-thunk/REMOVE'
|
|
5
|
-
export const remove = (myID: string): BaseAction => ({
|
|
6
|
-
myID,
|
|
7
|
-
type: REMOVE,
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
export const reduceRemove = <S extends State>(
|
|
11
|
-
moduleState: ModuleState<S>,
|
|
12
|
-
action: BaseAction,
|
|
13
|
-
): ModuleState<S> => {
|
|
14
|
-
const { myID } = action
|
|
15
|
-
|
|
16
|
-
const myNode = moduleState.nodes[myID]
|
|
17
|
-
if (!myNode) {
|
|
18
|
-
return moduleState
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const newNodes = Object.keys(moduleState.nodes)
|
|
22
|
-
.filter((each) => each !== myID)
|
|
23
|
-
.reduce((r: NodeStateMap<S>, x) => {
|
|
24
|
-
r[x] = moduleState.nodes[x]
|
|
25
|
-
return r
|
|
26
|
-
}, {})
|
|
27
|
-
|
|
28
|
-
// defaultID
|
|
29
|
-
const newState = Object.assign({}, moduleState, { nodes: newNodes })
|
|
30
|
-
if (newState.defaultID === myID) {
|
|
31
|
-
newState.defaultID = null
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return newState
|
|
35
|
-
}
|
package/src/set.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Dispatch } from 'react'
|
|
2
|
-
// XXX requiring to import directly from action/ActionOrThunk, or it will cause looping.
|
|
3
|
-
import type { ActionOrThunk } from './action/ActionOrThunk'
|
|
4
|
-
import type { State } from './stateTypes'
|
|
5
|
-
|
|
6
|
-
// set
|
|
7
|
-
export type set<S extends State> = Dispatch<ActionOrThunk<S>>
|
package/src/setDefaultID.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type BaseAction from './action/baseAction'
|
|
2
|
-
import type { ModuleState, State } from './stateTypes'
|
|
3
|
-
|
|
4
|
-
export const SET_DEFAULT_ID = '@chhsiao1981/use-thunk/SET_DEFAULT_ID'
|
|
5
|
-
export const setDefaultID = (myID: string): BaseAction => ({
|
|
6
|
-
myID,
|
|
7
|
-
type: SET_DEFAULT_ID,
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
export const reduceSetDefaultID = <S extends State>(
|
|
11
|
-
moduleState: ModuleState<S>,
|
|
12
|
-
action: BaseAction,
|
|
13
|
-
): ModuleState<S> => {
|
|
14
|
-
const { myID } = action
|
|
15
|
-
|
|
16
|
-
return Object.assign({}, moduleState, { defaultID: myID })
|
|
17
|
-
}
|
package/src/setMap.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { ActionFunc } from './action'
|
|
2
|
-
import type { set } from './set'
|
|
3
|
-
import type { State } from './stateTypes'
|
|
4
|
-
import type { ThunkModule, ThunkModuleFunc } from './thunkModule'
|
|
5
|
-
import {
|
|
6
|
-
DEFAULT_THUNK_MODULE_FUNC_MAP,
|
|
7
|
-
type DefaultThunkModuleFuncMap,
|
|
8
|
-
} from './thunkModule/defaultThunkModuleFuncMap'
|
|
9
|
-
|
|
10
|
-
// biome-ignore lint/suspicious/noExplicitAny: unknown requires same type in list, use any for possible different types.
|
|
11
|
-
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void
|
|
12
|
-
|
|
13
|
-
export type setMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
14
|
-
[action in keyof T]: VoidReturnType<T[action]>
|
|
15
|
-
} & Omit<DefaultSetMap, keyof T>
|
|
16
|
-
|
|
17
|
-
export type DefaultSetMap = {
|
|
18
|
-
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface setMapByModuleMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
22
|
-
[name: string]: setMap<S, T>
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// biome-ignore lint/suspicious/noExplicitAny: set map by module map can be any.
|
|
26
|
-
export const SET_MAP_BY_MODULE_MAP: setMapByModuleMap<any, any> = {}
|
|
27
|
-
|
|
28
|
-
export const constructSetMap = <S extends State, T extends ThunkModuleFunc<S>>(
|
|
29
|
-
theDo: ThunkModule<S>,
|
|
30
|
-
set: set<S>,
|
|
31
|
-
setMap: setMap<S, T>,
|
|
32
|
-
) => {
|
|
33
|
-
Object.keys(theDo)
|
|
34
|
-
// default and name are reserved words.
|
|
35
|
-
// functions starting reduce are included in default and not exported.
|
|
36
|
-
.filter((each) => typeof theDo[each] === 'function')
|
|
37
|
-
.reduce((val, eachAction) => {
|
|
38
|
-
if (val[eachAction]) {
|
|
39
|
-
return val
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// because action is a function.
|
|
43
|
-
const action = theDo[eachAction] as ActionFunc<S>
|
|
44
|
-
|
|
45
|
-
// @ts-expect-error eachAction is in setMap<S, R>
|
|
46
|
-
// biome-ignore lint/suspicious/noExplicitAny: action parameters can be any types.
|
|
47
|
-
val[eachAction] = (...params: any[]) => set(action(...params))
|
|
48
|
-
return val
|
|
49
|
-
}, setMap)
|
|
50
|
-
|
|
51
|
-
Object.keys(DEFAULT_THUNK_MODULE_FUNC_MAP).reduce((val, eachAction) => {
|
|
52
|
-
if (val[eachAction]) {
|
|
53
|
-
return val
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const action = DEFAULT_THUNK_MODULE_FUNC_MAP[eachAction]
|
|
57
|
-
|
|
58
|
-
// @ts-expect-error eachAction is in setMap<S, R>
|
|
59
|
-
// biome-ignore lint/suspicious/noExplicitAny: action parameters can be any types.
|
|
60
|
-
val[eachAction] = (...params: any[]) => set(action(...params))
|
|
61
|
-
return val
|
|
62
|
-
}, setMap)
|
|
63
|
-
|
|
64
|
-
return setMap
|
|
65
|
-
}
|
package/src/stateTypes.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//State
|
|
2
|
-
export interface State {
|
|
3
|
-
[key: string]: unknown
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// NodeState
|
|
7
|
-
export type NodeState<S extends State> = {
|
|
8
|
-
id: string
|
|
9
|
-
state: S
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type NodeStateMap<S extends State> = {
|
|
13
|
-
[key: string]: NodeState<S>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// ModuleState
|
|
17
|
-
export type ModuleState<S extends State> = {
|
|
18
|
-
name: string
|
|
19
|
-
defaultID?: string | null
|
|
20
|
-
nodes: NodeStateMap<S>
|
|
21
|
-
defaultState: S
|
|
22
|
-
}
|
package/src/states.ts
DELETED
|
@@ -1,50 +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
|
-
|
|
6
|
-
export const getDefaultID = <S extends State>(moduleState: ModuleState<S>): string => {
|
|
7
|
-
return moduleState.defaultID ?? ''
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const getNode = <S extends State>(
|
|
11
|
-
moduleState: ModuleState<S>,
|
|
12
|
-
myID?: string,
|
|
13
|
-
): NodeState<S> | null => {
|
|
14
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
15
|
-
if (!theID) {
|
|
16
|
-
return null
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return moduleState.nodes[theID] || null
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const getStateOrNullByModule = <S extends State>(
|
|
23
|
-
moduleState: ModuleState<S>,
|
|
24
|
-
myID?: string,
|
|
25
|
-
): S | null => {
|
|
26
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
27
|
-
if (!theID) {
|
|
28
|
-
return null
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const me = moduleState.nodes[theID]
|
|
32
|
-
if (!me) {
|
|
33
|
-
return null
|
|
34
|
-
}
|
|
35
|
-
return me.state
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const getStateByModule = <S extends State>(moduleState: ModuleState<S>, myID?: string): S => {
|
|
39
|
-
return getStateOrNullByModule(moduleState, myID) || moduleState.defaultState
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const getState = <S extends State, R extends ThunkModuleFunc<S>>(
|
|
43
|
-
theUseThunk: UseThunk<S, R>,
|
|
44
|
-
myID?: string,
|
|
45
|
-
): [S, setMap<S, R>, string] => {
|
|
46
|
-
const [moduleState, theDo] = theUseThunk
|
|
47
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
48
|
-
const state = getStateByModule(moduleState, theID)
|
|
49
|
-
return [state, theDo, theID]
|
|
50
|
-
}
|
package/src/thunkContextMap.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Context as rContext } from 'react'
|
|
2
|
-
import type { ModuleState } from './stateTypes'
|
|
3
|
-
import type { Context } from './thunkContextTypes'
|
|
4
|
-
|
|
5
|
-
export type ThunkContextMap = {
|
|
6
|
-
theMap: {
|
|
7
|
-
[moduleName: string]: {
|
|
8
|
-
// biome-ignore lint/suspicious/noExplicitAny: ThunkContextMap can be any type
|
|
9
|
-
context: rContext<Context<any>>
|
|
10
|
-
|
|
11
|
-
// we need to use refModuleState to sync all the moduleState in all ops.
|
|
12
|
-
// biome-ignore lint/suspicious/noExplicitAny: ThunkContextMap can be any type
|
|
13
|
-
refModuleState: { current: ModuleState<any> }
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
theList: string[]
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const THUNK_CONTEXT_MAP: ThunkContextMap = {
|
|
20
|
-
theMap: {},
|
|
21
|
-
theList: [],
|
|
22
|
-
}
|
package/src/thunkContextTypes.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Dispatch, SetStateAction } from 'react'
|
|
2
|
-
import type { ModuleState, State } from './stateTypes'
|
|
3
|
-
|
|
4
|
-
export type Context<S extends State> = {
|
|
5
|
-
// INFO: we use refModuleState to reference across all the useThunk of the same module in different ops.
|
|
6
|
-
refModuleState: { current: ModuleState<S> }
|
|
7
|
-
setModuleState: Dispatch<SetStateAction<ModuleState<S>>>
|
|
8
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ActionFunc } from '../action'
|
|
2
|
-
import { init } from '../init'
|
|
3
|
-
import { remove } from '../remove'
|
|
4
|
-
import { update } from '../update'
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_THUNK_MODULE_FUNC_MAP: DefaultThunkModuleFuncMap = {
|
|
7
|
-
init,
|
|
8
|
-
update,
|
|
9
|
-
remove,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type DefaultThunkModuleFuncMap = {
|
|
13
|
-
// biome-ignore lint/suspicious/noExplicitAny: DefaultThunkModuleFuncMap can be any type.
|
|
14
|
-
[action: string]: ActionFunc<any>
|
|
15
|
-
}
|
package/src/thunkModule/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { ThunkFunc } from '../action'
|
|
2
|
-
import type { Reducer } from '../reducer'
|
|
3
|
-
import type { State } from '../stateTypes'
|
|
4
|
-
|
|
5
|
-
export interface ThunkModuleBase<S extends State> {
|
|
6
|
-
[idx: string]: ThunkFunc<S> | string | Reducer<S> | S | undefined
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
|
|
10
|
-
[action: string]: ThunkFunc<S>
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// This is used as the parameter for useThunk.
|
|
14
|
-
export type ThunkModule<S extends State> = {
|
|
15
|
-
name: string
|
|
16
|
-
default?: Reducer<S>
|
|
17
|
-
defaultState: S
|
|
18
|
-
} & ThunkModuleBase<S>
|
|
19
|
-
|
|
20
|
-
export type ThunkModuleToFunc<T> = Omit<T, 'name' | 'default' | 'defaultState'>
|