@chhsiao1981/use-thunk 13.0.0 → 14.0.2
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 +109 -83
- package/dist/dispatch.d.ts +3 -3
- package/dist/index.d.ts +11 -14
- package/dist/index.js +173 -0
- package/dist/index.umd.cjs +1 -0
- package/{types → dist}/states/index.d.ts +2 -2
- package/dist/thunkContext/ThunkContext.d.ts +7 -0
- package/{types → dist}/thunkModule/index.d.ts +1 -1
- package/package.json +28 -17
- 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/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 -13
- package/src/defaultThunks/index.ts +0 -15
- package/src/defaultThunks/init/index.ts +0 -27
- package/src/defaultThunks/init/initCore.ts +0 -31
- package/src/defaultThunks/remove.ts +0 -35
- package/src/defaultThunks/setDefaultID.ts +0 -18
- package/src/defaultThunks/update.ts +0 -26
- package/src/defaultThunks/upsert.ts +0 -27
- package/src/dispatch.ts +0 -7
- package/src/get.ts +0 -4
- package/src/index.ts +0 -58
- package/src/reducer/createReducer.ts +0 -18
- package/src/reducer/defaultReduceMap.ts +0 -26
- package/src/reducer/index.ts +0 -12
- package/src/reducer/reduceMap.ts +0 -6
- package/src/registerThunk.ts +0 -29
- package/src/set/index.ts +0 -9
- package/src/set/setMap.ts +0 -59
- package/src/states/index.ts +0 -84
- package/src/states/types.ts +0 -23
- package/src/thunkContext/ThunkContext.tsx +0 -46
- package/src/thunkContext/index.ts +0 -9
- package/src/thunkContext/thunkContextMap.ts +0 -22
- package/src/thunkContext/types.ts +0 -8
- package/src/thunkModule/defaultDoModule.ts +0 -13
- package/src/thunkModule/index.ts +0 -21
- package/src/useThunk/index.ts +0 -48
- package/src/useThunk/useThunkReducer.ts +0 -100
- package/src/utils/deepCopy.ts +0 -30
- package/src/utils/genID.ts +0 -6
- package/src/utils/index.ts +0 -3
- package/types/dispatch.d.ts +0 -4
- package/types/index.d.ts +0 -12
- package/types/thunkContext/ThunkContext.d.ts +0 -7
- /package/{types → dist}/action/ActionOrThunk.d.ts +0 -0
- /package/{types → dist}/action/baseAction.d.ts +0 -0
- /package/{types → dist}/action/index.d.ts +0 -0
- /package/{types → dist}/action/thunk.d.ts +0 -0
- /package/{types → dist}/defaultThunks/index.d.ts +0 -0
- /package/{types → dist}/defaultThunks/init/index.d.ts +0 -0
- /package/{types → dist}/defaultThunks/init/initCore.d.ts +0 -0
- /package/{types → dist}/defaultThunks/remove.d.ts +0 -0
- /package/{types → dist}/defaultThunks/setDefaultID.d.ts +0 -0
- /package/{types → dist}/defaultThunks/update.d.ts +0 -0
- /package/{types → dist}/defaultThunks/upsert.d.ts +0 -0
- /package/{types → dist}/get.d.ts +0 -0
- /package/{types → dist}/reducer/createReducer.d.ts +0 -0
- /package/{types → dist}/reducer/defaultReduceMap.d.ts +0 -0
- /package/{types → dist}/reducer/index.d.ts +0 -0
- /package/{types → dist}/reducer/reduceMap.d.ts +0 -0
- /package/{types → dist}/registerThunk.d.ts +0 -0
- /package/{types → dist}/set/index.d.ts +0 -0
- /package/{types → dist}/set/setMap.d.ts +0 -0
- /package/{types → dist}/states/types.d.ts +0 -0
- /package/{types → dist}/thunkContext/index.d.ts +0 -0
- /package/{types → dist}/thunkContext/thunkContextMap.d.ts +0 -0
- /package/{types → dist}/thunkContext/types.d.ts +0 -0
- /package/{types → dist}/thunkModule/defaultDoModule.d.ts +0 -0
- /package/{types → dist}/useThunk/index.d.ts +0 -0
- /package/{types → dist}/useThunk/useThunkReducer.d.ts +0 -0
- /package/{types → dist}/utils/deepCopy.d.ts +0 -0
- /package/{types → dist}/utils/genID.d.ts +0 -0
- /package/{types → dist}/utils/index.d.ts +0 -0
package/src/set/setMap.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { ActionFunc } from '../action'
|
|
2
|
-
import type { set } from '../set'
|
|
3
|
-
import type { State } from '../states'
|
|
4
|
-
import type { doModule, ThunkModule } from '../thunkModule'
|
|
5
|
-
import { DEFAULT_DO_MODULE, type defaultDoModule } from '../thunkModule'
|
|
6
|
-
|
|
7
|
-
// biome-ignore lint/suspicious/noExplicitAny: unknown requires same type in list, use any for possible different types.
|
|
8
|
-
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void
|
|
9
|
-
|
|
10
|
-
export type setMap<S extends State, T extends doModule<S>> = {
|
|
11
|
-
[action in keyof T]: VoidReturnType<T[action]>
|
|
12
|
-
} & Omit<DefaultSetMap, keyof T>
|
|
13
|
-
|
|
14
|
-
export type DefaultSetMap = {
|
|
15
|
-
[action in keyof defaultDoModule]: VoidReturnType<defaultDoModule[action]>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface setMapByModuleMap<S extends State, T extends doModule<S>> {
|
|
19
|
-
[moduleMap: string]: setMap<S, T>
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const constructSetMap = <S extends State, T extends doModule<S>>(
|
|
23
|
-
theDo: ThunkModule<S>,
|
|
24
|
-
set: set<S>,
|
|
25
|
-
setMap: setMap<S, T>,
|
|
26
|
-
) => {
|
|
27
|
-
Object.keys(theDo)
|
|
28
|
-
// default and name are reserved words.
|
|
29
|
-
// functions starting reduce are included in default and not exported.
|
|
30
|
-
.filter((each) => typeof theDo[each] === 'function')
|
|
31
|
-
.reduce((val, eachAction) => {
|
|
32
|
-
if (val[eachAction]) {
|
|
33
|
-
return val
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// because action is a function.
|
|
37
|
-
const action = theDo[eachAction] as ActionFunc<S>
|
|
38
|
-
|
|
39
|
-
// @ts-expect-error eachAction is in setMap<S, R>
|
|
40
|
-
// biome-ignore lint/suspicious/noExplicitAny: action parameters can be any types.
|
|
41
|
-
val[eachAction] = (...params: any[]) => set(action(...params))
|
|
42
|
-
return val
|
|
43
|
-
}, setMap)
|
|
44
|
-
|
|
45
|
-
Object.keys(DEFAULT_DO_MODULE).reduce((val, eachAction) => {
|
|
46
|
-
if (val[eachAction]) {
|
|
47
|
-
return val
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const action = DEFAULT_DO_MODULE[eachAction]
|
|
51
|
-
|
|
52
|
-
// @ts-expect-error eachAction is in setMap<S, R>
|
|
53
|
-
// biome-ignore lint/suspicious/noExplicitAny: action parameters can be any types.
|
|
54
|
-
val[eachAction] = (...params: any[]) => set(action(...params))
|
|
55
|
-
return val
|
|
56
|
-
}, setMap)
|
|
57
|
-
|
|
58
|
-
return setMap
|
|
59
|
-
}
|
package/src/states/index.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import type { setMap } from '../set'
|
|
2
|
-
import type { doModule } from '../thunkModule'
|
|
3
|
-
import type { UseThunk } from '../useThunk'
|
|
4
|
-
import { deepCopy } from '../utils'
|
|
5
|
-
import type { ModuleState, NodeState, NodeStateMap, State } from './types'
|
|
6
|
-
|
|
7
|
-
export type { ModuleState, NodeState, State, NodeStateMap }
|
|
8
|
-
|
|
9
|
-
export const getDefaultID = <S extends State>(moduleState: ModuleState<S>): string => {
|
|
10
|
-
return moduleState.defaultID ?? ''
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const getNodeOrNull = <S extends State>(
|
|
14
|
-
moduleState: ModuleState<S>,
|
|
15
|
-
myID?: string,
|
|
16
|
-
): Readonly<NodeState<S> | null> => {
|
|
17
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
18
|
-
if (!theID) {
|
|
19
|
-
return null
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return moduleState.nodes[theID] || null
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const getStateOrNullByModule = <S extends State>(
|
|
26
|
-
moduleState: ModuleState<S>,
|
|
27
|
-
myID?: string,
|
|
28
|
-
): Readonly<S | null> => {
|
|
29
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
30
|
-
if (!theID) {
|
|
31
|
-
return null
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const me = moduleState.nodes[theID]
|
|
35
|
-
if (!me) {
|
|
36
|
-
return null
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return me.state
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const getStateByModule = <S extends State>(
|
|
43
|
-
moduleState: ModuleState<S>,
|
|
44
|
-
myID?: string,
|
|
45
|
-
): Readonly<S> => {
|
|
46
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
47
|
-
if (!theID) {
|
|
48
|
-
return moduleState.defaultState
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const state = getStateOrNullByModule(moduleState, theID)
|
|
52
|
-
if (state) {
|
|
53
|
-
return state
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// XXX magic for new nodes
|
|
57
|
-
// 1. reduceInit
|
|
58
|
-
const newState = deepCopy(moduleState.defaultState)
|
|
59
|
-
const newNode = { id: theID, state: newState }
|
|
60
|
-
moduleState.nodes[theID] = newNode
|
|
61
|
-
|
|
62
|
-
// 2. already init default-id, no need to init default-id here.
|
|
63
|
-
if (moduleState.isInitDefaultID) {
|
|
64
|
-
return newState
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 3. check defaultID
|
|
68
|
-
if (!moduleState.defaultID) {
|
|
69
|
-
moduleState.defaultID = theID
|
|
70
|
-
moduleState.isInitDefaultID = true
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return newState
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export const getState = <S extends State, R extends doModule<S>>(
|
|
77
|
-
theUseThunk: UseThunk<S, R>,
|
|
78
|
-
myID?: string,
|
|
79
|
-
): [Readonly<S>, setMap<S, R>, string] => {
|
|
80
|
-
const [moduleState, theDo] = theUseThunk
|
|
81
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
82
|
-
const state = getStateByModule(moduleState, theID)
|
|
83
|
-
return [state, theDo, theID]
|
|
84
|
-
}
|
package/src/states/types.ts
DELETED
|
@@ -1,23 +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
|
-
// ModuleState
|
|
13
|
-
export type ModuleState<S extends State> = {
|
|
14
|
-
name: string
|
|
15
|
-
nodes: NodeStateMap<S>
|
|
16
|
-
defaultState: S
|
|
17
|
-
defaultID?: string | null
|
|
18
|
-
isInitDefaultID?: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type NodeStateMap<S extends State> = {
|
|
22
|
-
[key: string]: NodeState<S>
|
|
23
|
-
}
|
|
@@ -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
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Context } from './types'
|
|
2
|
-
export type { Context }
|
|
3
|
-
|
|
4
|
-
// THUNK_CONTEXT_MAP is used in registerThunk and useThunkReducer
|
|
5
|
-
import { THUNK_CONTEXT_MAP, type ThunkContextMap } from './thunkContextMap'
|
|
6
|
-
export { THUNK_CONTEXT_MAP, type ThunkContextMap }
|
|
7
|
-
|
|
8
|
-
import ThunkContext from './ThunkContext'
|
|
9
|
-
export { ThunkContext }
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Context as rContext } from 'react'
|
|
2
|
-
import type { ModuleState } from '../states'
|
|
3
|
-
import type { Context } from './types'
|
|
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
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Dispatch, SetStateAction } from 'react'
|
|
2
|
-
import type { ModuleState, State } from '../states'
|
|
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,13 +0,0 @@
|
|
|
1
|
-
import type { ActionFunc } from '../action'
|
|
2
|
-
import { init, remove, update } from '../defaultThunks'
|
|
3
|
-
|
|
4
|
-
export const DEFAULT_DO_MODULE: defaultDoModule = {
|
|
5
|
-
init,
|
|
6
|
-
update,
|
|
7
|
-
remove,
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type defaultDoModule = {
|
|
11
|
-
// biome-ignore lint/suspicious/noExplicitAny: defaultDoModule can be any type.
|
|
12
|
-
[action: string]: ActionFunc<any>
|
|
13
|
-
}
|
package/src/thunkModule/index.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { ThunkFunc } from '../action'
|
|
2
|
-
import type { State } from '../states'
|
|
3
|
-
import { DEFAULT_DO_MODULE, type defaultDoModule } from './defaultDoModule'
|
|
4
|
-
export { DEFAULT_DO_MODULE, type defaultDoModule }
|
|
5
|
-
|
|
6
|
-
export interface doModule<S extends State> {
|
|
7
|
-
[action: string]: ThunkFunc<S>
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// This is used as the parameter for useThunk.
|
|
11
|
-
export type ThunkModule<S extends State> = {
|
|
12
|
-
name: string
|
|
13
|
-
defaultState: S
|
|
14
|
-
|
|
15
|
-
// The rest of the variables are doModule.
|
|
16
|
-
// Specifying index-signatures to include all the variables.
|
|
17
|
-
[action: string]: ThunkFunc<S> | string | S
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// biome-ignore lint/suspicious/noExplicitAny: ok for type utility functions.
|
|
21
|
-
export type toDoModule<T extends ThunkModule<any>> = Omit<T, 'name' | 'default' | 'defaultState'>
|
package/src/useThunk/index.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react'
|
|
2
|
-
import { createReducer } from '../reducer'
|
|
3
|
-
import { constructSetMap, type setMap, type setMapByModuleMap } from '../set'
|
|
4
|
-
import type { ModuleState, State } from '../states'
|
|
5
|
-
import type { doModule, ThunkModule } 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 doModule<S>> = [Readonly<ModuleState<S>>, setMap<S, R>]
|
|
12
|
-
|
|
13
|
-
/**********
|
|
14
|
-
* useThunk
|
|
15
|
-
**********/
|
|
16
|
-
export default <S extends State, R extends doModule<S>>(theDo: ThunkModule<S>): UseThunk<S, R> => {
|
|
17
|
-
const { name } = theDo
|
|
18
|
-
|
|
19
|
-
// 1. It requires shared nodes for the same module to have the same setMap.
|
|
20
|
-
const isFirstTime = !SET_MAP_BY_MODULE[name]
|
|
21
|
-
if (isFirstTime) {
|
|
22
|
-
SET_MAP_BY_MODULE[name] = {}
|
|
23
|
-
}
|
|
24
|
-
const setMap = SET_MAP_BY_MODULE[name] as setMap<S, R>
|
|
25
|
-
|
|
26
|
-
// 2. reducer.
|
|
27
|
-
// theReducer is different for different useThunk,
|
|
28
|
-
// even within the same module.
|
|
29
|
-
// However, because theReducer is a pure function by
|
|
30
|
-
// having ModuleState as the input. It is ok to have
|
|
31
|
-
// different reducers within the same module.
|
|
32
|
-
const theReducer = useMemo(() => createReducer<S>(), [])
|
|
33
|
-
|
|
34
|
-
// 3. useThunkReducer
|
|
35
|
-
const [moduleState, set] = useThunkReducer(theReducer, name)
|
|
36
|
-
|
|
37
|
-
const ret: UseThunk<S, R> = useMemo(() => {
|
|
38
|
-
return [moduleState, setMap]
|
|
39
|
-
}, [moduleState, setMap])
|
|
40
|
-
|
|
41
|
-
if (!isFirstTime) {
|
|
42
|
-
return ret
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
constructSetMap(theDo, set, setMap)
|
|
46
|
-
|
|
47
|
-
return ret
|
|
48
|
-
}
|
|
@@ -1,100 +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'
|
|
6
|
-
import { upsert } from '../defaultThunks/upsert'
|
|
7
|
-
import type { dispatch } from '../dispatch'
|
|
8
|
-
import type { get, getOrNull } from '../get'
|
|
9
|
-
import type { Reducer } from '../reducer'
|
|
10
|
-
import type { set } from '../set'
|
|
11
|
-
import { getStateByModule, getStateOrNullByModule, type ModuleState, type State } from '../states'
|
|
12
|
-
import { THUNK_CONTEXT_MAP } from '../thunkContext'
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* useThunkReducer
|
|
16
|
-
*
|
|
17
|
-
* Augments React's useReducer() hook so that the action
|
|
18
|
-
* setter (dispatcher) supports thunks.
|
|
19
|
-
*/
|
|
20
|
-
export default <S extends State>(reducer: Reducer<S>, moduleName: string): [ModuleState<S>, set<S>] => {
|
|
21
|
-
const { context } = THUNK_CONTEXT_MAP.theMap[moduleName]
|
|
22
|
-
|
|
23
|
-
const { refModuleState, setModuleState: setModuleState_c } = useContext(context)
|
|
24
|
-
const getModuleState = useCallback(() => {
|
|
25
|
-
return refModuleState.current
|
|
26
|
-
}, [refModuleState]) as () => ModuleState<S>
|
|
27
|
-
|
|
28
|
-
const setModuleState = useCallback(
|
|
29
|
-
(newModuleState: ModuleState<S>) => {
|
|
30
|
-
refModuleState.current = newModuleState
|
|
31
|
-
setModuleState_c(newModuleState)
|
|
32
|
-
},
|
|
33
|
-
[refModuleState, setModuleState_c],
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
const getOrNull: getOrNull<S> = useCallback(
|
|
37
|
-
(id?: string) => {
|
|
38
|
-
const moduleState = getModuleState()
|
|
39
|
-
const state = getStateOrNullByModule(moduleState, id)
|
|
40
|
-
return state
|
|
41
|
-
},
|
|
42
|
-
[getModuleState],
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
const get: get<S> = useCallback(
|
|
46
|
-
(id?: string) => {
|
|
47
|
-
const moduleState = getModuleState()
|
|
48
|
-
const state = getStateByModule(moduleState, id)
|
|
49
|
-
return state
|
|
50
|
-
},
|
|
51
|
-
[getModuleState],
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
const reduce = useCallback(
|
|
55
|
-
(action: BaseAction): ModuleState<S> => {
|
|
56
|
-
const moduleState = getModuleState()
|
|
57
|
-
const newModuleState = reducer(moduleState, action)
|
|
58
|
-
return newModuleState
|
|
59
|
-
},
|
|
60
|
-
[reducer, getModuleState],
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
const dispatch: dispatch<S> = useCallback(
|
|
64
|
-
(action) => {
|
|
65
|
-
if (typeof action === 'function') {
|
|
66
|
-
// action is Thunk<S, A>
|
|
67
|
-
action(set, get, getOrNull, dispatch, getModuleState)
|
|
68
|
-
return
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// action is not function. so action is BaseAction
|
|
72
|
-
const newModuleState = reduce(action)
|
|
73
|
-
setModuleState(newModuleState)
|
|
74
|
-
},
|
|
75
|
-
[getModuleState, setModuleState, reduce],
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
const set: set<S> = useCallback(
|
|
79
|
-
(actionOrID, data) => {
|
|
80
|
-
if (typeof actionOrID === 'string') {
|
|
81
|
-
// actionOrID is id
|
|
82
|
-
if (!data) {
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// we have the data, we can do upsert.
|
|
87
|
-
const action = upsert(actionOrID, data)
|
|
88
|
-
const newModuleState = reduce(action)
|
|
89
|
-
setModuleState(newModuleState)
|
|
90
|
-
return
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// actionOrID is action
|
|
94
|
-
dispatch(actionOrID)
|
|
95
|
-
},
|
|
96
|
-
[getModuleState, setModuleState, reduce],
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
return [refModuleState.current, set]
|
|
100
|
-
}
|
package/src/utils/deepCopy.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
// biome-ignore lint/suspicious/noExplicitAny: obj in deepCopy can be any type.
|
|
2
|
-
const deepCopy = (obj: any): any => {
|
|
3
|
-
// Handle primitives, functions, null, or undefined
|
|
4
|
-
if (obj === null || typeof obj !== 'object') {
|
|
5
|
-
return obj
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
// Handle Date objects
|
|
9
|
-
if (obj instanceof Date) {
|
|
10
|
-
return new Date(obj.getTime())
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Handle Arrays
|
|
14
|
-
if (Array.isArray(obj)) {
|
|
15
|
-
return obj.map((item) => deepCopy(item))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Handle Objects
|
|
19
|
-
// biome-ignore lint/suspicious/noExplicitAny: clonedObj can be any type.
|
|
20
|
-
const clonedObj: any = {}
|
|
21
|
-
for (const key in obj) {
|
|
22
|
-
if (obj.hasOwnProperty(key)) {
|
|
23
|
-
clonedObj[key] = deepCopy(obj[key])
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return clonedObj
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default deepCopy
|
package/src/utils/genID.ts
DELETED
package/src/utils/index.ts
DELETED
package/types/dispatch.d.ts
DELETED
package/types/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { getModuleState, Thunk, ThunkFunc } from './action';
|
|
2
|
-
import { type InitParams, init, remove, setDefaultID, update, upsert } from './defaultThunks';
|
|
3
|
-
import type { dispatch } from './dispatch';
|
|
4
|
-
import type { get, getOrNull } from './get';
|
|
5
|
-
import registerThunk from './registerThunk';
|
|
6
|
-
import type { set, setMap } from './set';
|
|
7
|
-
import { getDefaultID, getNodeOrNull, getState, getStateByModule, getStateOrNullByModule, type ModuleState, type State } from './states';
|
|
8
|
-
import { ThunkContext } from './thunkContext';
|
|
9
|
-
import type { doModule, ThunkModule, toDoModule } from './thunkModule';
|
|
10
|
-
import useThunk, { type UseThunk } from './useThunk';
|
|
11
|
-
import { genID } from './utils';
|
|
12
|
-
export { type Thunk, type set, type get, type getOrNull, type dispatch, type getModuleState, registerThunk, useThunk, type UseThunk, ThunkContext, type State, getState, type ModuleState, type ThunkModule, type toDoModule, type doModule, type ThunkFunc, init, type InitParams, update, upsert, remove, genID, type setMap, setDefaultID, getNodeOrNull, getDefaultID, getStateOrNullByModule, getStateByModule, };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{types → dist}/get.d.ts
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|