@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/dist/ThunkContext.d.ts
DELETED
package/dist/action.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { ClassState, State } from './stateTypes';
|
|
2
|
-
import type { ActionOrThunk as rActionOrThunk, Thunk as rThunk } from './useThunkReducer';
|
|
3
|
-
export interface BaseAction {
|
|
4
|
-
myID: string;
|
|
5
|
-
type: string;
|
|
6
|
-
[key: string]: unknown;
|
|
7
|
-
}
|
|
8
|
-
export type Thunk<S extends State> = rThunk<S, BaseAction>;
|
|
9
|
-
export type ActionOrThunk<S extends State> = rActionOrThunk<S, BaseAction>;
|
|
10
|
-
export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>;
|
|
11
|
-
export type GetClassState<S extends State> = () => ClassState<S>;
|
package/dist/createReducer.d.ts
DELETED
package/dist/createThunk.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from './action';
|
|
2
|
-
import type { State } from './stateTypes';
|
|
3
|
-
import type { ThunkModule, ThunkModuleFunc } from './thunk';
|
|
4
|
-
import { type DefaultThunkModuleFuncMap } from './thunkModuleFuncMap';
|
|
5
|
-
import type { Thunk as rThunk } from './useThunkReducer';
|
|
6
|
-
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
|
|
7
|
-
export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
8
|
-
[action in keyof T]: VoidReturnType<T[action]>;
|
|
9
|
-
} & Omit<DefaultDispatchFuncMap, keyof T>;
|
|
10
|
-
export type DefaultDispatchFuncMap = {
|
|
11
|
-
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
|
|
12
|
-
};
|
|
13
|
-
export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
14
|
-
[className: string]: DispatchFuncMap<S, T>;
|
|
15
|
-
}
|
|
16
|
-
export declare const DISPATCH_FUNC_MAP_BY_CLASS_MAP: DispatchFuncMapByClassMap<any, any>;
|
|
17
|
-
export declare const constructDispatchMap: <S extends State, T extends ThunkModuleFunc<S>, A extends BaseAction>(theDo: ThunkModule<S>, dispatch: (action: A | rThunk<S, A>) => void, dispatchMap: DispatchFuncMap<S, T>) => DispatchFuncMap<S, T>;
|
|
18
|
-
export {};
|
package/dist/genUUID.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const genUUID: (myuuidv4?: () => string) => string;
|
package/dist/init.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { BaseAction, Thunk } from './action';
|
|
2
|
-
import type { ClassState, 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>;
|
|
8
|
-
interface InitAction<S extends State> extends BaseAction {
|
|
9
|
-
parentID?: string;
|
|
10
|
-
doParent?: DispatchFuncMap;
|
|
11
|
-
parentClass?: string;
|
|
12
|
-
state: S;
|
|
13
|
-
}
|
|
14
|
-
export declare const INIT = "@chhsiao1981/use-thunk/INIT";
|
|
15
|
-
export declare const reduceInit: <S extends State>(classState: ClassState<S>, action: InitAction<S>) => ClassState<S>;
|
|
16
|
-
export {};
|
package/dist/reduceMap.d.ts
DELETED
package/dist/reducer.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Reducer as rReducer } from 'react';
|
|
2
|
-
import type { BaseAction } from './action';
|
|
3
|
-
import type { ClassState, State } from './stateTypes';
|
|
4
|
-
export type Reducer<S extends State> = rReducer<ClassState<S>, BaseAction>;
|
|
5
|
-
export type ReduceFunc<S extends State> = (state: ClassState<S>, action: BaseAction) => ClassState<S>;
|
package/dist/remove.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { BaseAction, Thunk } from './action';
|
|
2
|
-
import type { ClassState, State } from './stateTypes';
|
|
3
|
-
export declare const remove: <S extends State>(myID: string) => Thunk<S>;
|
|
4
|
-
export declare const REMOVE = "@chhsiao1981/use-thunk/REMOVE";
|
|
5
|
-
export declare const reduceRemove: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
|
package/dist/setDefaultID.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from './action';
|
|
2
|
-
import type { ClassState, State } from './stateTypes';
|
|
3
|
-
export declare const SET_DEFAULT_ID = "@chhsiao1981/use-thunk/SET_DEFAULT_ID";
|
|
4
|
-
export declare const setDefaultID: (myID: string) => BaseAction;
|
|
5
|
-
export declare const reduceSetDefaultID: <S extends State>(classState: ClassState<S>, action: BaseAction) => ClassState<S>;
|
package/dist/stateTypes.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export interface State {
|
|
2
|
-
[key: string]: unknown;
|
|
3
|
-
}
|
|
4
|
-
export type NodeState<S extends State> = {
|
|
5
|
-
id: string;
|
|
6
|
-
state: S;
|
|
7
|
-
};
|
|
8
|
-
export type NodeStateMap<S extends State> = {
|
|
9
|
-
[key: string]: NodeState<S>;
|
|
10
|
-
};
|
|
11
|
-
export type ClassState<S extends State> = {
|
|
12
|
-
myClass: string;
|
|
13
|
-
defaultID?: string | null;
|
|
14
|
-
nodes: NodeStateMap<S>;
|
|
15
|
-
defaultState: S;
|
|
16
|
-
};
|
package/dist/states.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { DispatchFuncMap } from './dispatchFuncMap';
|
|
2
|
-
import type { ClassState, NodeState, State } from './stateTypes';
|
|
3
|
-
import type { ThunkModuleFunc } from './thunk';
|
|
4
|
-
import type { UseThunk } from './useThunk';
|
|
5
|
-
export declare const getDefaultID: <S extends State>(classState: ClassState<S>) => string;
|
|
6
|
-
export declare const getNode: <S extends State>(classState: ClassState<S>, myID?: string) => NodeState<S> | null;
|
|
7
|
-
export declare const getState: <S extends State>(classState: ClassState<S>, myID?: string) => S | null;
|
|
8
|
-
export declare const mustGetState: <S extends State>(classState: ClassState<S>, myID?: string) => S;
|
|
9
|
-
export declare const mustGetStateByThunk: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, DispatchFuncMap<S, R>, string];
|
package/dist/thunk.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ActionFunc } from './action';
|
|
2
|
-
import type { Reducer } from './reducer';
|
|
3
|
-
import type { State } from './stateTypes';
|
|
4
|
-
export interface ThunkModuleBase<S extends State> {
|
|
5
|
-
[idx: string]: ActionFunc<S> | string | Reducer<S> | S | undefined;
|
|
6
|
-
}
|
|
7
|
-
export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
|
|
8
|
-
[action: string]: ActionFunc<S>;
|
|
9
|
-
}
|
|
10
|
-
export type ThunkModule<S extends State> = {
|
|
11
|
-
myClass: string;
|
|
12
|
-
default?: Reducer<S>;
|
|
13
|
-
defaultState: S;
|
|
14
|
-
} & ThunkModuleBase<S>;
|
|
15
|
-
export type ThunkModuleToFunc<T> = Omit<T, 'myClass' | 'default' | 'defaultState'>;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Context as rContext } from 'react';
|
|
2
|
-
import type { ClassState } from './stateTypes';
|
|
3
|
-
import type { Context } from './thunkContextTypes';
|
|
4
|
-
export type ThunkContextMap = {
|
|
5
|
-
theMap: {
|
|
6
|
-
[classname: string]: {
|
|
7
|
-
context: rContext<Context<any>>;
|
|
8
|
-
refClassState: {
|
|
9
|
-
current: ClassState<any>;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
theList: string[];
|
|
14
|
-
};
|
|
15
|
-
export declare const THUNK_CONTEXT_MAP: ThunkContextMap;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import type { ClassState, State } from './stateTypes';
|
|
3
|
-
export type Context<S extends State> = {
|
|
4
|
-
refClassState: {
|
|
5
|
-
current: ClassState<S>;
|
|
6
|
-
};
|
|
7
|
-
setClassState: Dispatch<SetStateAction<ClassState<S>>>;
|
|
8
|
-
};
|
|
@@ -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/action/baseAction.ts
DELETED
package/src/action/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { ModuleState, State } from '../states'
|
|
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
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { dispatch } from '../dispatch'
|
|
2
|
-
import type { get, getOrNull } from '../get'
|
|
3
|
-
import type { set } from '../set'
|
|
4
|
-
import type { ModuleState, State } from '../states'
|
|
5
|
-
|
|
6
|
-
// Thunk
|
|
7
|
-
export type Thunk<S extends State> = (
|
|
8
|
-
set: set<S>,
|
|
9
|
-
get: get<S>,
|
|
10
|
-
getOrNull: getOrNull<S>,
|
|
11
|
-
dispatch: dispatch<S>,
|
|
12
|
-
getModuleState: () => ModuleState<S>,
|
|
13
|
-
) => void
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// default thunks will be used in thunkModule/defaultDoModule and defaultReduceMap
|
|
2
|
-
import { INIT, type InitParams, init, reduceInit } from './init'
|
|
3
|
-
export { INIT, type InitParams, init, reduceInit }
|
|
4
|
-
|
|
5
|
-
import { reduceUpdate, UPDATE, update } from './update'
|
|
6
|
-
export { reduceUpdate, UPDATE, update }
|
|
7
|
-
|
|
8
|
-
import { REMOVE, reduceRemove, remove } from './remove'
|
|
9
|
-
export { REMOVE, reduceRemove, remove }
|
|
10
|
-
|
|
11
|
-
import { reduceUpsert, UPSERT, upsert } from './upsert'
|
|
12
|
-
export { UPSERT, upsert, reduceUpsert }
|
|
13
|
-
|
|
14
|
-
import { reduceSetDefaultID, SET_DEFAULT_ID, setDefaultID } from './setDefaultID'
|
|
15
|
-
export { SET_DEFAULT_ID, setDefaultID, reduceSetDefaultID }
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { Thunk } from '../../action'
|
|
2
|
-
import type { State } from '../../states'
|
|
3
|
-
import { genID } from '../../utils'
|
|
4
|
-
import { setDefaultID } from '../setDefaultID'
|
|
5
|
-
import initCore, { INIT, reduceInit } from './initCore'
|
|
6
|
-
export { INIT, reduceInit }
|
|
7
|
-
|
|
8
|
-
// InitParams
|
|
9
|
-
export interface InitParams<S extends State> {
|
|
10
|
-
myID?: string
|
|
11
|
-
state: S
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const init = <S extends State>(params: InitParams<S>): Thunk<S> => {
|
|
15
|
-
return (set, _get, _getOrNull, _dispatch, getModuleState) => {
|
|
16
|
-
const myID = params.myID ?? genID()
|
|
17
|
-
|
|
18
|
-
const { state } = params
|
|
19
|
-
set(initCore(myID, state))
|
|
20
|
-
|
|
21
|
-
const { defaultID } = getModuleState()
|
|
22
|
-
|
|
23
|
-
if (!defaultID) {
|
|
24
|
-
set(setDefaultID(myID))
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from '../../action'
|
|
2
|
-
import type { ModuleState, NodeState, NodeStateMap, State } from '../../states'
|
|
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
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from '../action'
|
|
2
|
-
import type { ModuleState, NodeStateMap, State } from '../states'
|
|
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
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from '../action'
|
|
2
|
-
import type { ModuleState, State } from '../states'
|
|
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
|
-
const toUpdate: Partial<ModuleState<S>> = { defaultID: myID, isInitDefaultID: true }
|
|
17
|
-
return Object.assign({}, moduleState, toUpdate)
|
|
18
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from '../action'
|
|
2
|
-
import type { ModuleState, State } from '../states'
|
|
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
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from '../action'
|
|
2
|
-
import type { ModuleState, NodeState, State } from '../states'
|
|
3
|
-
import { deepCopy } from '../utils'
|
|
4
|
-
|
|
5
|
-
export const UPSERT = '@chhsiao1981/use-thunk/UPSERT'
|
|
6
|
-
export const upsert = <S extends State>(myID: string, data: Partial<S>): BaseAction => ({
|
|
7
|
-
myID,
|
|
8
|
-
type: UPSERT,
|
|
9
|
-
data,
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
export const reduceUpsert = <S extends State>(
|
|
13
|
-
moduleState: ModuleState<S>,
|
|
14
|
-
action: BaseAction,
|
|
15
|
-
): ModuleState<S> => {
|
|
16
|
-
const { myID, data } = action
|
|
17
|
-
|
|
18
|
-
const myNode = moduleState.nodes[myID]
|
|
19
|
-
const node: NodeState<S> = myNode ?? { id: myID, state: deepCopy(moduleState.defaultState) }
|
|
20
|
-
|
|
21
|
-
const newState = Object.assign({}, node.state, data)
|
|
22
|
-
const newNode = Object.assign({}, node, { state: newState })
|
|
23
|
-
const newNodes = Object.assign({}, moduleState.nodes, { [myID]: newNode })
|
|
24
|
-
const newModuleState = Object.assign({}, moduleState, { nodes: newNodes })
|
|
25
|
-
|
|
26
|
-
return newModuleState
|
|
27
|
-
}
|
package/src/dispatch.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'
|
|
4
|
-
import type { State } from './states'
|
|
5
|
-
|
|
6
|
-
// dispatch
|
|
7
|
-
export type dispatch<S extends State> = Dispatch<ActionOrThunk<S>>
|
package/src/get.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,58 +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 {
|
|
8
|
-
getDefaultID,
|
|
9
|
-
getNodeOrNull,
|
|
10
|
-
getState,
|
|
11
|
-
getStateByModule,
|
|
12
|
-
getStateOrNullByModule,
|
|
13
|
-
type ModuleState,
|
|
14
|
-
type State,
|
|
15
|
-
} from './states'
|
|
16
|
-
import { ThunkContext } from './thunkContext'
|
|
17
|
-
import type { doModule, ThunkModule, toDoModule } from './thunkModule'
|
|
18
|
-
import useThunk, { type UseThunk } from './useThunk'
|
|
19
|
-
import { genID } from './utils'
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
// thunk definition.
|
|
23
|
-
type Thunk,
|
|
24
|
-
type set,
|
|
25
|
-
type get,
|
|
26
|
-
type getOrNull,
|
|
27
|
-
type dispatch,
|
|
28
|
-
type getModuleState,
|
|
29
|
-
// registerThunk / useThunk / ThunkContext
|
|
30
|
-
registerThunk,
|
|
31
|
-
useThunk,
|
|
32
|
-
type UseThunk,
|
|
33
|
-
ThunkContext,
|
|
34
|
-
// state related.
|
|
35
|
-
type State,
|
|
36
|
-
getState,
|
|
37
|
-
type ModuleState,
|
|
38
|
-
// module related.
|
|
39
|
-
type ThunkModule,
|
|
40
|
-
type toDoModule,
|
|
41
|
-
type doModule,
|
|
42
|
-
type ThunkFunc,
|
|
43
|
-
// default thunks.
|
|
44
|
-
init,
|
|
45
|
-
type InitParams,
|
|
46
|
-
update,
|
|
47
|
-
upsert,
|
|
48
|
-
remove,
|
|
49
|
-
// genID
|
|
50
|
-
genID,
|
|
51
|
-
// advanced usage.
|
|
52
|
-
type setMap,
|
|
53
|
-
setDefaultID,
|
|
54
|
-
getNodeOrNull,
|
|
55
|
-
getDefaultID,
|
|
56
|
-
getStateOrNullByModule,
|
|
57
|
-
getStateByModule,
|
|
58
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { BaseAction } from '../action'
|
|
2
|
-
import type { ModuleState, State } from '../states'
|
|
3
|
-
import { DEFAULT_REDUCE_MAP } from './defaultReduceMap'
|
|
4
|
-
import type { Reducer } from './index'
|
|
5
|
-
|
|
6
|
-
export const createReducer = <S extends State>(): Reducer<S> => {
|
|
7
|
-
return (moduleState: ModuleState<S>, action: BaseAction): ModuleState<S> => {
|
|
8
|
-
if (!action) {
|
|
9
|
-
return moduleState
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (DEFAULT_REDUCE_MAP[action.type]) {
|
|
13
|
-
return DEFAULT_REDUCE_MAP[action.type](moduleState, action)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return moduleState
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
INIT,
|
|
3
|
-
REMOVE,
|
|
4
|
-
reduceInit,
|
|
5
|
-
reduceRemove,
|
|
6
|
-
reduceSetDefaultID,
|
|
7
|
-
reduceUpdate,
|
|
8
|
-
reduceUpsert,
|
|
9
|
-
SET_DEFAULT_ID,
|
|
10
|
-
UPDATE,
|
|
11
|
-
UPSERT,
|
|
12
|
-
} from '../defaultThunks'
|
|
13
|
-
import type { ReduceMap } from './reduceMap'
|
|
14
|
-
|
|
15
|
-
// biome-ignore lint/suspicious/noExplicitAny: DEFAULT_REDUCE_MAP can apply to any States.
|
|
16
|
-
export const DEFAULT_REDUCE_MAP: ReduceMap<any> = {
|
|
17
|
-
[INIT]: reduceInit,
|
|
18
|
-
[UPDATE]: reduceUpdate,
|
|
19
|
-
[REMOVE]: reduceRemove,
|
|
20
|
-
[UPSERT]: reduceUpsert,
|
|
21
|
-
|
|
22
|
-
// setDefaultID.
|
|
23
|
-
// Typically we don't need this in programming.
|
|
24
|
-
// The defaultID is automatically determined if defaultID is not set.
|
|
25
|
-
[SET_DEFAULT_ID]: reduceSetDefaultID,
|
|
26
|
-
}
|
package/src/reducer/index.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Reducer as rReducer } from 'react'
|
|
2
|
-
import type { BaseAction } from '../action'
|
|
3
|
-
import type { ModuleState, State } from '../states'
|
|
4
|
-
|
|
5
|
-
import { createReducer } from './createReducer'
|
|
6
|
-
export { createReducer }
|
|
7
|
-
|
|
8
|
-
// Reducer
|
|
9
|
-
export type Reducer<S extends State> = rReducer<ModuleState<S>, BaseAction>
|
|
10
|
-
|
|
11
|
-
// ReduceFunc
|
|
12
|
-
export type ReduceFunc<S extends State> = (state: ModuleState<S>, action: BaseAction) => ModuleState<S>
|
package/src/reducer/reduceMap.ts
DELETED
package/src/registerThunk.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// Reason for registerThunk instead of createThunk:
|
|
2
|
-
// I feel that createThunk needs to return some object
|
|
3
|
-
// as the proof of successful creation.
|
|
4
|
-
// However, we register Thunks to the global state management
|
|
5
|
-
// system and return void.
|
|
6
|
-
import { createContext, type Dispatch, type SetStateAction } from 'react'
|
|
7
|
-
import type { ModuleState, State } from './states'
|
|
8
|
-
import { THUNK_CONTEXT_MAP } from './thunkContext'
|
|
9
|
-
import type { ThunkModule } from './thunkModule'
|
|
10
|
-
|
|
11
|
-
export default <S extends State>(theDo: ThunkModule<S>) => {
|
|
12
|
-
const { name, defaultState } = theDo
|
|
13
|
-
|
|
14
|
-
if (THUNK_CONTEXT_MAP.theMap[name]) {
|
|
15
|
-
console.warn('registerThunk: already init:', name)
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const moduleState: ModuleState<S> = { name, nodes: {}, defaultState }
|
|
20
|
-
const setModuleState: Dispatch<SetStateAction<ModuleState<S>>> = () => {}
|
|
21
|
-
const refModuleState = { current: moduleState }
|
|
22
|
-
const context = createContext({ refModuleState: refModuleState, setModuleState })
|
|
23
|
-
|
|
24
|
-
THUNK_CONTEXT_MAP.theMap[name] = { context, refModuleState }
|
|
25
|
-
const theList = Object.keys(THUNK_CONTEXT_MAP.theMap).sort()
|
|
26
|
-
THUNK_CONTEXT_MAP.theList = theList
|
|
27
|
-
|
|
28
|
-
console.info('registerThunk: done:', name)
|
|
29
|
-
}
|
package/src/set/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// XXX requiring to import directly from action/ActionOrThunk, or it will cause looping.
|
|
2
|
-
import type { ActionOrThunk } from '../action'
|
|
3
|
-
import type { State } from '../states'
|
|
4
|
-
|
|
5
|
-
import { constructSetMap, type DefaultSetMap, type setMap, type setMapByModuleMap } from './setMap'
|
|
6
|
-
export { constructSetMap, type DefaultSetMap, type setMap, type setMapByModuleMap }
|
|
7
|
-
|
|
8
|
-
// set
|
|
9
|
-
export type set<S extends State> = (actionOrID: ActionOrThunk<S> | string, data?: Partial<S>) => void
|