@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.
Files changed (101) hide show
  1. package/README.md +211 -122
  2. package/{types → dist}/action/ActionOrThunk.d.ts +1 -1
  3. package/{types → dist}/action/index.d.ts +2 -2
  4. package/dist/action/thunk.d.ts +5 -0
  5. package/dist/defaultThunks/index.d.ts +10 -0
  6. package/dist/defaultThunks/init/index.d.ts +9 -0
  7. package/{types → dist/defaultThunks}/init/initCore.d.ts +2 -2
  8. package/{types → dist/defaultThunks}/remove.d.ts +2 -2
  9. package/{types → dist/defaultThunks}/setDefaultID.d.ts +2 -2
  10. package/{types → dist/defaultThunks}/update.d.ts +2 -2
  11. package/dist/defaultThunks/upsert.d.ts +5 -0
  12. package/dist/dispatch.d.ts +3 -3
  13. package/dist/get.d.ts +3 -0
  14. package/dist/index.d.ts +11 -14
  15. package/dist/index.js +173 -0
  16. package/dist/index.umd.cjs +1 -0
  17. package/dist/reducer/createReducer.d.ts +3 -0
  18. package/dist/reducer/defaultReduceMap.d.ts +2 -0
  19. package/{types/reducer.d.ts → dist/reducer/index.d.ts} +4 -2
  20. package/dist/reducer/reduceMap.d.ts +5 -0
  21. package/{types/createThunk.d.ts → dist/registerThunk.d.ts} +1 -1
  22. package/dist/set/index.d.ts +5 -0
  23. package/dist/set/setMap.d.ts +16 -0
  24. package/dist/states/index.d.ts +10 -0
  25. package/{types/stateTypes.d.ts → dist/states/types.d.ts} +5 -4
  26. package/dist/thunkContext/ThunkContext.d.ts +7 -0
  27. package/dist/thunkContext/index.d.ts +6 -0
  28. package/{types → dist/thunkContext}/thunkContextMap.d.ts +2 -2
  29. package/{types/thunkContextTypes.d.ts → dist/thunkContext/types.d.ts} +1 -1
  30. package/dist/thunkModule/defaultDoModule.d.ts +5 -0
  31. package/dist/thunkModule/index.d.ts +13 -0
  32. package/dist/useThunk/index.d.ts +9 -0
  33. package/{types → dist/useThunk}/useThunkReducer.d.ts +3 -3
  34. package/dist/utils/deepCopy.d.ts +2 -0
  35. package/dist/utils/genID.d.ts +1 -0
  36. package/dist/utils/index.d.ts +3 -0
  37. package/package.json +35 -30
  38. package/dist/ThunkContext.d.ts +0 -7
  39. package/dist/action.d.ts +0 -11
  40. package/dist/createReducer.d.ts +0 -4
  41. package/dist/createThunk.d.ts +0 -4
  42. package/dist/dispatchFuncMap.d.ts +0 -18
  43. package/dist/genUUID.d.ts +0 -1
  44. package/dist/init.d.ts +0 -16
  45. package/dist/reduceMap.d.ts +0 -6
  46. package/dist/reducer.d.ts +0 -5
  47. package/dist/remove.d.ts +0 -5
  48. package/dist/setDefaultID.d.ts +0 -5
  49. package/dist/stateTypes.d.ts +0 -16
  50. package/dist/states.d.ts +0 -9
  51. package/dist/thunk.d.ts +0 -15
  52. package/dist/thunkContextMap.d.ts +0 -15
  53. package/dist/thunkContextTypes.d.ts +0 -8
  54. package/dist/thunkModuleFuncMap.d.ts +0 -7
  55. package/dist/update.d.ts +0 -6
  56. package/dist/useThunk.d.ts +0 -12
  57. package/dist/useThunkReducer.d.ts +0 -17
  58. package/src/ThunkContext.tsx +0 -46
  59. package/src/action/ActionOrThunk.ts +0 -5
  60. package/src/action/baseAction.ts +0 -6
  61. package/src/action/index.ts +0 -16
  62. package/src/action/thunk.ts +0 -9
  63. package/src/createReducer.ts +0 -24
  64. package/src/createThunk.ts +0 -24
  65. package/src/genUUID.ts +0 -38
  66. package/src/get.ts +0 -3
  67. package/src/index.ts +0 -42
  68. package/src/init/index.ts +0 -26
  69. package/src/init/initCore.ts +0 -31
  70. package/src/reduceMap.ts +0 -22
  71. package/src/reducer.ts +0 -9
  72. package/src/remove.ts +0 -35
  73. package/src/set.ts +0 -7
  74. package/src/setDefaultID.ts +0 -17
  75. package/src/setMap.ts +0 -65
  76. package/src/stateTypes.ts +0 -22
  77. package/src/states.ts +0 -50
  78. package/src/thunkContextMap.ts +0 -22
  79. package/src/thunkContextTypes.ts +0 -8
  80. package/src/thunkModule/defaultThunkModuleFuncMap.ts +0 -15
  81. package/src/thunkModule/index.ts +0 -20
  82. package/src/update.ts +0 -26
  83. package/src/useThunk.ts +0 -50
  84. package/src/useThunkReducer.ts +0 -70
  85. package/types/ThunkContext.d.ts +0 -7
  86. package/types/action/thunk.d.ts +0 -3
  87. package/types/createReducer.d.ts +0 -4
  88. package/types/dispatch.d.ts +0 -4
  89. package/types/dispatchFuncMap.d.ts +0 -17
  90. package/types/genUUID.d.ts +0 -1
  91. package/types/get.d.ts +0 -2
  92. package/types/index.d.ts +0 -16
  93. package/types/init/index.d.ts +0 -7
  94. package/types/reduceMap.d.ts +0 -6
  95. package/types/set.d.ts +0 -4
  96. package/types/setMap.d.ts +0 -17
  97. package/types/states.d.ts +0 -9
  98. package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +0 -5
  99. package/types/thunkModule/index.d.ts +0 -15
  100. package/types/useThunk.d.ts +0 -9
  101. /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>;
@@ -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;
@@ -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,5 +0,0 @@
1
- import type { State } from '../stateTypes'
2
- import type BaseAction from './baseAction'
3
- import type { Thunk } from './thunk'
4
-
5
- export type ActionOrThunk<S extends State> = BaseAction | Thunk<S>
@@ -1,6 +0,0 @@
1
- // BaseAction contains only object-based actions, no thunk-based actions.
2
- export default interface BaseAction {
3
- myID: string
4
- type: string
5
- [key: string]: unknown
6
- }
@@ -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>
@@ -1,9 +0,0 @@
1
- import type { set } from '../set'
2
- import type { ModuleState, State } from '../stateTypes'
3
-
4
- // Thunk
5
- export type Thunk<S extends State> = (
6
- set: set<S>,
7
- get: (id?: string) => S | null,
8
- getModuleState: () => ModuleState<S>,
9
- ) => void
@@ -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
- }
@@ -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
@@ -1,3 +0,0 @@
1
- import type { State } from './stateTypes'
2
-
3
- export type get = <S extends State>(id: string) => S
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
- }
@@ -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>>
@@ -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
- }
@@ -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
- }
@@ -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
- }
@@ -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'>