@chhsiao1981/use-thunk 11.0.0 → 13.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 +166 -99
- package/package.json +10 -16
- package/src/action/ActionOrThunk.ts +1 -1
- package/src/action/index.ts +3 -3
- package/src/action/thunk.ts +10 -2
- package/src/defaultThunks/index.ts +15 -0
- package/src/defaultThunks/init/index.ts +27 -0
- package/src/{init → defaultThunks/init}/initCore.ts +2 -2
- package/src/{remove.ts → defaultThunks/remove.ts} +2 -2
- package/src/{setDefaultID.ts → defaultThunks/setDefaultID.ts} +4 -3
- package/src/{update.ts → defaultThunks/update.ts} +2 -7
- package/src/defaultThunks/upsert.ts +27 -0
- package/src/dispatch.ts +7 -0
- package/src/get.ts +4 -0
- package/src/index.ts +43 -31
- package/src/reducer/createReducer.ts +18 -0
- package/src/reducer/defaultReduceMap.ts +26 -0
- package/src/{reducer.ts → reducer/index.ts} +5 -2
- package/src/reducer/reduceMap.ts +6 -0
- package/src/{createThunk.ts → registerThunk.ts} +11 -13
- package/src/set/index.ts +9 -0
- package/src/{setMap.ts → set/setMap.ts} +12 -18
- package/src/states/index.ts +84 -0
- package/src/{stateTypes.ts → states/types.ts} +7 -7
- package/src/thunkContext/index.ts +9 -0
- package/src/{thunkContextMap.ts → thunkContext/thunkContextMap.ts} +2 -2
- package/src/{thunkContextTypes.ts → thunkContext/types.ts} +1 -1
- package/src/thunkModule/defaultDoModule.ts +13 -0
- package/src/thunkModule/index.ts +12 -12
- package/src/{useThunk.ts → useThunk/index.ts} +7 -10
- package/src/{useThunkReducer.ts → useThunk/useThunkReducer.ts} +50 -10
- package/src/utils/deepCopy.ts +30 -0
- package/src/utils/genID.ts +6 -0
- package/src/utils/index.ts +3 -0
- package/types/action/ActionOrThunk.d.ts +1 -1
- package/types/action/index.d.ts +2 -2
- package/types/action/thunk.d.ts +4 -2
- package/types/defaultThunks/index.d.ts +10 -0
- package/types/defaultThunks/init/index.d.ts +9 -0
- package/types/{init → defaultThunks/init}/initCore.d.ts +2 -2
- package/types/{remove.d.ts → defaultThunks/remove.d.ts} +2 -2
- package/types/{setDefaultID.d.ts → defaultThunks/setDefaultID.d.ts} +2 -2
- package/types/{update.d.ts → defaultThunks/update.d.ts} +2 -3
- package/types/defaultThunks/upsert.d.ts +5 -0
- package/types/dispatch.d.ts +4 -4
- package/types/get.d.ts +3 -0
- package/types/index.d.ts +11 -20
- package/types/reducer/createReducer.d.ts +3 -0
- package/types/reducer/defaultReduceMap.d.ts +2 -0
- package/types/{reducer.d.ts → reducer/index.d.ts} +4 -2
- package/types/reducer/reduceMap.d.ts +5 -0
- package/types/{createThunk.d.ts → registerThunk.d.ts} +1 -2
- package/types/set/index.d.ts +5 -0
- package/types/set/setMap.d.ts +16 -0
- package/types/states/index.d.ts +10 -0
- package/types/{stateTypes.d.ts → states/types.d.ts} +6 -6
- package/types/thunkContext/index.d.ts +6 -0
- package/types/{thunkContextMap.d.ts → thunkContext/thunkContextMap.d.ts} +2 -2
- package/types/{thunkContextTypes.d.ts → thunkContext/types.d.ts} +1 -1
- package/types/thunkModule/defaultDoModule.d.ts +5 -0
- package/types/thunkModule/index.d.ts +8 -11
- package/types/useThunk/index.d.ts +9 -0
- package/types/{useThunkReducer.d.ts → useThunk/useThunkReducer.d.ts} +3 -3
- package/types/utils/deepCopy.d.ts +2 -0
- package/types/utils/genID.d.ts +1 -0
- package/types/utils/index.d.ts +3 -0
- package/src/createReducer.ts +0 -24
- package/src/genUUID.ts +0 -38
- package/src/init/index.ts +0 -26
- package/src/reduceMap.ts +0 -22
- package/src/set.ts +0 -7
- package/src/states.ts +0 -47
- package/src/thunkModule/defaultThunkModuleFuncMap.ts +0 -16
- package/types/createReducer.d.ts +0 -4
- package/types/dispatchFuncMap.d.ts +0 -17
- package/types/genUUID.d.ts +0 -1
- 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/useThunk.d.ts +0 -9
- /package/src/{ThunkContext.tsx → thunkContext/ThunkContext.tsx} +0 -0
- /package/types/{ThunkContext.d.ts → thunkContext/ThunkContext.d.ts} +0 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { INIT, type InitParams, init, reduceInit } from './init';
|
|
2
|
+
export { INIT, type InitParams, init, reduceInit };
|
|
3
|
+
import { reduceUpdate, UPDATE, update } from './update';
|
|
4
|
+
export { reduceUpdate, UPDATE, update };
|
|
5
|
+
import { REMOVE, reduceRemove, remove } from './remove';
|
|
6
|
+
export { REMOVE, reduceRemove, remove };
|
|
7
|
+
import { reduceUpsert, UPSERT, upsert } from './upsert';
|
|
8
|
+
export { UPSERT, upsert, reduceUpsert };
|
|
9
|
+
import { reduceSetDefaultID, SET_DEFAULT_ID, setDefaultID } from './setDefaultID';
|
|
10
|
+
export { SET_DEFAULT_ID, setDefaultID, reduceSetDefaultID };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Thunk } from '../../action';
|
|
2
|
+
import type { State } from '../../states';
|
|
3
|
+
import { INIT, reduceInit } from './initCore';
|
|
4
|
+
export { INIT, reduceInit };
|
|
5
|
+
export interface InitParams<S extends State> {
|
|
6
|
+
myID?: string;
|
|
7
|
+
state: S;
|
|
8
|
+
}
|
|
9
|
+
export declare const init: <S extends State>(params: InitParams<S>) => Thunk<S>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type BaseAction from '
|
|
2
|
-
import type { ModuleState, State } from '
|
|
1
|
+
import type { BaseAction } from '../../action';
|
|
2
|
+
import type { ModuleState, State } from '../../states';
|
|
3
3
|
export interface InitAction<S extends State> extends BaseAction {
|
|
4
4
|
state: S;
|
|
5
5
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type BaseAction from '
|
|
2
|
-
import type { ModuleState, State } from '
|
|
1
|
+
import type { BaseAction } from '../action';
|
|
2
|
+
import type { ModuleState, State } from '../states';
|
|
3
3
|
export declare const REMOVE = "@chhsiao1981/use-thunk/REMOVE";
|
|
4
4
|
export declare const remove: (myID: string) => BaseAction;
|
|
5
5
|
export declare const reduceRemove: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type BaseAction from '
|
|
2
|
-
import type { ModuleState, State } from '
|
|
1
|
+
import type { BaseAction } from '../action';
|
|
2
|
+
import type { ModuleState, State } from '../states';
|
|
3
3
|
export declare const SET_DEFAULT_ID = "@chhsiao1981/use-thunk/SET_DEFAULT_ID";
|
|
4
4
|
export declare const setDefaultID: (myID: string) => BaseAction;
|
|
5
5
|
export declare const reduceSetDefaultID: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type BaseAction from '
|
|
2
|
-
import type { ModuleState, State } from '
|
|
1
|
+
import type { BaseAction } from '../action';
|
|
2
|
+
import type { ModuleState, State } from '../states';
|
|
3
3
|
export declare const UPDATE = "@chhsiao1981/use-thunk/UPDATE";
|
|
4
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
5
|
export declare const reduceUpdate: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseAction } from '../action';
|
|
2
|
+
import type { ModuleState, State } from '../states';
|
|
3
|
+
export declare const UPSERT = "@chhsiao1981/use-thunk/UPSERT";
|
|
4
|
+
export declare const upsert: <S extends State>(myID: string, data: Partial<S>) => BaseAction;
|
|
5
|
+
export declare const reduceUpsert: <S extends State>(moduleState: ModuleState<S>, action: BaseAction) => ModuleState<S>;
|
package/types/dispatch.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Dispatch
|
|
2
|
-
import type { ActionOrThunk } from './action
|
|
3
|
-
import type { State } from './
|
|
4
|
-
export type
|
|
1
|
+
import type { Dispatch } from 'react';
|
|
2
|
+
import type { ActionOrThunk } from './action';
|
|
3
|
+
import type { State } from './states';
|
|
4
|
+
export type dispatch<S extends State> = Dispatch<ActionOrThunk<S>>;
|
package/types/get.d.ts
ADDED
package/types/index.d.ts
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import type { set } from './set';
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import type { ModuleState, State } from './stateTypes';
|
|
11
|
-
import ThunkContext from './ThunkContext';
|
|
12
|
-
import type { ThunkModule, ThunkModuleToFunc } from './thunkModule';
|
|
13
|
-
import { setData, update } from './update';
|
|
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';
|
|
14
10
|
import useThunk, { type UseThunk } from './useThunk';
|
|
15
|
-
|
|
16
|
-
useThunk, type UseThunk, ThunkContext, type State, type ModuleState, type
|
|
17
|
-
type GetModuleState, type GetModuleState as GetClassState, // to deprecate
|
|
18
|
-
type Thunk, type ThunkModule, type ThunkModuleToFunc, type set as Dispatch, // to deprecate
|
|
19
|
-
type set, type setMap as DispatchFuncMap, // to deprecate
|
|
20
|
-
type setMap, getNode, getDefaultID, getState, mustGetState, mustGetStateByThunk, init, type InitParams, setData, // to deprecate
|
|
21
|
-
update, remove, setDefaultID, genUUID, };
|
|
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, };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Reducer as rReducer } from 'react';
|
|
2
|
-
import type BaseAction from '
|
|
3
|
-
import type { ModuleState, State } from '
|
|
2
|
+
import type { BaseAction } from '../action';
|
|
3
|
+
import type { ModuleState, State } from '../states';
|
|
4
|
+
import { createReducer } from './createReducer';
|
|
5
|
+
export { createReducer };
|
|
4
6
|
export type Reducer<S extends State> = rReducer<ModuleState<S>, BaseAction>;
|
|
5
7
|
export type ReduceFunc<S extends State> = (state: ModuleState<S>, action: BaseAction) => ModuleState<S>;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { State } from './
|
|
1
|
+
import type { State } from './states';
|
|
2
2
|
import type { ThunkModule } from './thunkModule';
|
|
3
3
|
declare const _default: <S extends State>(theDo: ThunkModule<S>) => void;
|
|
4
4
|
export default _default;
|
|
5
|
-
export declare const registerThunk: <S extends State>(theDo: ThunkModule<S>) => void;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ActionOrThunk } from '../action';
|
|
2
|
+
import type { State } from '../states';
|
|
3
|
+
import { constructSetMap, type DefaultSetMap, type setMap, type setMapByModuleMap } from './setMap';
|
|
4
|
+
export { constructSetMap, type DefaultSetMap, type setMap, type setMapByModuleMap };
|
|
5
|
+
export type set<S extends State> = (actionOrID: ActionOrThunk<S> | string, data?: Partial<S>) => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { set } from '../set';
|
|
2
|
+
import type { State } from '../states';
|
|
3
|
+
import type { doModule, ThunkModule } from '../thunkModule';
|
|
4
|
+
import { type defaultDoModule } from '../thunkModule';
|
|
5
|
+
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
|
|
6
|
+
export type setMap<S extends State, T extends doModule<S>> = {
|
|
7
|
+
[action in keyof T]: VoidReturnType<T[action]>;
|
|
8
|
+
} & Omit<DefaultSetMap, keyof T>;
|
|
9
|
+
export type DefaultSetMap = {
|
|
10
|
+
[action in keyof defaultDoModule]: VoidReturnType<defaultDoModule[action]>;
|
|
11
|
+
};
|
|
12
|
+
export interface setMapByModuleMap<S extends State, T extends doModule<S>> {
|
|
13
|
+
[moduleMap: string]: setMap<S, T>;
|
|
14
|
+
}
|
|
15
|
+
export declare const constructSetMap: <S extends State, T extends doModule<S>>(theDo: ThunkModule<S>, set: set<S>, setMap: setMap<S, T>) => setMap<S, T>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { setMap } from '../set';
|
|
2
|
+
import type { doModule } from '../thunkModule';
|
|
3
|
+
import type { UseThunk } from '../useThunk';
|
|
4
|
+
import type { ModuleState, NodeState, NodeStateMap, State } from './types';
|
|
5
|
+
export type { ModuleState, NodeState, State, NodeStateMap };
|
|
6
|
+
export declare const getDefaultID: <S extends State>(moduleState: ModuleState<S>) => string;
|
|
7
|
+
export declare const getNodeOrNull: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<NodeState<S> | null>;
|
|
8
|
+
export declare const getStateOrNullByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<S | null>;
|
|
9
|
+
export declare const getStateByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => Readonly<S>;
|
|
10
|
+
export declare const getState: <S extends State, R extends doModule<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [Readonly<S>, setMap<S, R>, string];
|
|
@@ -5,13 +5,13 @@ export type NodeState<S extends State> = {
|
|
|
5
5
|
id: string;
|
|
6
6
|
state: S;
|
|
7
7
|
};
|
|
8
|
-
export type NodeStateMap<S extends State> = {
|
|
9
|
-
[key: string]: NodeState<S>;
|
|
10
|
-
};
|
|
11
8
|
export type ModuleState<S extends State> = {
|
|
12
|
-
name
|
|
13
|
-
myClass?: string;
|
|
14
|
-
defaultID?: string | null;
|
|
9
|
+
name: string;
|
|
15
10
|
nodes: NodeStateMap<S>;
|
|
16
11
|
defaultState: S;
|
|
12
|
+
defaultID?: string | null;
|
|
13
|
+
isInitDefaultID?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type NodeStateMap<S extends State> = {
|
|
16
|
+
[key: string]: NodeState<S>;
|
|
17
17
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Context } from './types';
|
|
2
|
+
export type { Context };
|
|
3
|
+
import { THUNK_CONTEXT_MAP, type ThunkContextMap } from './thunkContextMap';
|
|
4
|
+
export { THUNK_CONTEXT_MAP, type ThunkContextMap };
|
|
5
|
+
import ThunkContext from './ThunkContext';
|
|
6
|
+
export { ThunkContext };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Context as rContext } from 'react';
|
|
2
|
-
import type { ModuleState } from '
|
|
3
|
-
import type { Context } from './
|
|
2
|
+
import type { ModuleState } from '../states';
|
|
3
|
+
import type { Context } from './types';
|
|
4
4
|
export type ThunkContextMap = {
|
|
5
5
|
theMap: {
|
|
6
6
|
[moduleName: string]: {
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import type { ThunkFunc } from '../action';
|
|
2
|
-
import type {
|
|
3
|
-
import type
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
|
|
2
|
+
import type { State } from '../states';
|
|
3
|
+
import { DEFAULT_DO_MODULE, type defaultDoModule } from './defaultDoModule';
|
|
4
|
+
export { DEFAULT_DO_MODULE, type defaultDoModule };
|
|
5
|
+
export interface doModule<S extends State> {
|
|
8
6
|
[action: string]: ThunkFunc<S>;
|
|
9
7
|
}
|
|
10
8
|
export type ThunkModule<S extends State> = {
|
|
11
|
-
name
|
|
12
|
-
myClass?: string;
|
|
13
|
-
default?: Reducer<S>;
|
|
9
|
+
name: string;
|
|
14
10
|
defaultState: S;
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
[action: string]: ThunkFunc<S> | string | S;
|
|
12
|
+
};
|
|
13
|
+
export type toDoModule<T extends ThunkModule<any>> = Omit<T, 'name' | 'default' | 'defaultState'>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type setMap } from '../set';
|
|
2
|
+
import type { ModuleState, State } from '../states';
|
|
3
|
+
import type { doModule, ThunkModule } from '../thunkModule';
|
|
4
|
+
export type UseThunk<S extends State, R extends doModule<S>> = [Readonly<ModuleState<S>>, setMap<S, R>];
|
|
5
|
+
/**********
|
|
6
|
+
* useThunk
|
|
7
|
+
**********/
|
|
8
|
+
declare const _default: <S extends State, R extends doModule<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
|
|
9
|
+
export default _default;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Reducer } from '
|
|
2
|
-
import type { set } from '
|
|
3
|
-
import type
|
|
1
|
+
import type { Reducer } from '../reducer';
|
|
2
|
+
import type { set } from '../set';
|
|
3
|
+
import { type ModuleState, type State } from '../states';
|
|
4
4
|
/**
|
|
5
5
|
* useThunkReducer
|
|
6
6
|
*
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const genID: () => string;
|
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/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/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/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/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/states.ts
DELETED
|
@@ -1,47 +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 getState = <S extends State>(moduleState: ModuleState<S>, myID?: string): S | null => {
|
|
23
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
24
|
-
if (!theID) {
|
|
25
|
-
return null
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const me = moduleState.nodes[theID]
|
|
29
|
-
if (!me) {
|
|
30
|
-
return null
|
|
31
|
-
}
|
|
32
|
-
return me.state
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const mustGetState = <S extends State>(moduleState: ModuleState<S>, myID?: string): S => {
|
|
36
|
-
return getState(moduleState, myID) || moduleState.defaultState
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export const mustGetStateByThunk = <S extends State, R extends ThunkModuleFunc<S>>(
|
|
40
|
-
theUseThunk: UseThunk<S, R>,
|
|
41
|
-
myID?: string,
|
|
42
|
-
): [S, setMap<S, R>, string] => {
|
|
43
|
-
const [moduleState, theDo] = theUseThunk
|
|
44
|
-
const theID = myID ? myID : getDefaultID(moduleState)
|
|
45
|
-
const state = mustGetState(moduleState, theID)
|
|
46
|
-
return [state, theDo, theID]
|
|
47
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { ActionFunc } from '../action'
|
|
2
|
-
import { init } from '../init'
|
|
3
|
-
import { remove } from '../remove'
|
|
4
|
-
import { setData, update } from '../update'
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_THUNK_MODULE_FUNC_MAP: DefaultThunkModuleFuncMap = {
|
|
7
|
-
init,
|
|
8
|
-
update,
|
|
9
|
-
setData,
|
|
10
|
-
remove,
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type DefaultThunkModuleFuncMap = {
|
|
14
|
-
// biome-ignore lint/suspicious/noExplicitAny: DefaultThunkModuleFuncMap can be any type.
|
|
15
|
-
[action: string]: ActionFunc<any>
|
|
16
|
-
}
|
package/types/createReducer.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Dispatch } from './dispatch';
|
|
2
|
-
import type { State } from './stateTypes';
|
|
3
|
-
import type { ThunkModule, ThunkModuleFunc } from './thunkModule';
|
|
4
|
-
import { type DefaultThunkModuleFuncMap } from './thunkModule/defaultThunkModuleFuncMap';
|
|
5
|
-
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
|
|
6
|
-
export type DispatchFuncMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
7
|
-
[action in keyof T]: VoidReturnType<T[action]>;
|
|
8
|
-
} & Omit<DefaultDispatchFuncMap, keyof T>;
|
|
9
|
-
export type DefaultDispatchFuncMap = {
|
|
10
|
-
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
|
|
11
|
-
};
|
|
12
|
-
export interface DispatchFuncMapByClassMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
13
|
-
[className: string]: DispatchFuncMap<S, T>;
|
|
14
|
-
}
|
|
15
|
-
export declare const DISPATCH_FUNC_MAP_BY_CLASS_MAP: DispatchFuncMapByClassMap<any, any>;
|
|
16
|
-
export declare const constructDispatchMap: <S extends State, T extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>, dispatch: Dispatch<S>, dispatchMap: DispatchFuncMap<S, T>) => DispatchFuncMap<S, T>;
|
|
17
|
-
export {};
|
package/types/genUUID.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const genUUID: (myuuidv4?: () => string) => string;
|
package/types/init/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Thunk } from '../action';
|
|
2
|
-
import type { State } from '../stateTypes';
|
|
3
|
-
export interface InitParams<S extends State> {
|
|
4
|
-
myID?: string;
|
|
5
|
-
state: S;
|
|
6
|
-
}
|
|
7
|
-
export declare const init: <S extends State>(params: InitParams<S>, myuuidv4?: () => string) => Thunk<S>;
|
package/types/reduceMap.d.ts
DELETED
package/types/set.d.ts
DELETED
package/types/setMap.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { set } from './set';
|
|
2
|
-
import type { State } from './stateTypes';
|
|
3
|
-
import type { ThunkModule, ThunkModuleFunc } from './thunkModule';
|
|
4
|
-
import { type DefaultThunkModuleFuncMap } from './thunkModule/defaultThunkModuleFuncMap';
|
|
5
|
-
type VoidReturnType<T extends (...params: any[]) => unknown> = (...params: Parameters<T>) => void;
|
|
6
|
-
export type setMap<S extends State, T extends ThunkModuleFunc<S>> = {
|
|
7
|
-
[action in keyof T]: VoidReturnType<T[action]>;
|
|
8
|
-
} & Omit<DefaultSetMap, keyof T>;
|
|
9
|
-
export type DefaultSetMap = {
|
|
10
|
-
[action in keyof DefaultThunkModuleFuncMap]: VoidReturnType<DefaultThunkModuleFuncMap[action]>;
|
|
11
|
-
};
|
|
12
|
-
export interface setMapByModuleMap<S extends State, T extends ThunkModuleFunc<S>> {
|
|
13
|
-
[name: string]: setMap<S, T>;
|
|
14
|
-
}
|
|
15
|
-
export declare const SET_MAP_BY_MODULE_MAP: setMapByModuleMap<any, any>;
|
|
16
|
-
export declare const constructSetMap: <S extends State, T extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>, set: set<S>, setMap: setMap<S, T>) => setMap<S, T>;
|
|
17
|
-
export {};
|
package/types/states.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { setMap } from './setMap';
|
|
2
|
-
import type { ModuleState, NodeState, State } from './stateTypes';
|
|
3
|
-
import type { ThunkModuleFunc } from './thunkModule';
|
|
4
|
-
import type { UseThunk } from './useThunk';
|
|
5
|
-
export declare const getDefaultID: <S extends State>(moduleState: ModuleState<S>) => string;
|
|
6
|
-
export declare const getNode: <S extends State>(moduleState: ModuleState<S>, myID?: string) => NodeState<S> | null;
|
|
7
|
-
export declare const getState: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S | null;
|
|
8
|
-
export declare const mustGetState: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S;
|
|
9
|
-
export declare const mustGetStateByThunk: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, setMap<S, R>, string];
|
package/types/useThunk.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type setMap } from './setMap';
|
|
2
|
-
import type { ModuleState, State } from './stateTypes';
|
|
3
|
-
import type { ThunkModule, ThunkModuleFunc } from './thunkModule';
|
|
4
|
-
export type UseThunk<S extends State, R extends ThunkModuleFunc<S>> = [ModuleState<S>, setMap<S, R>];
|
|
5
|
-
/**********
|
|
6
|
-
* useThunk
|
|
7
|
-
**********/
|
|
8
|
-
declare const _default: <S extends State, R extends ThunkModuleFunc<S>>(theDo: ThunkModule<S>) => UseThunk<S, R>;
|
|
9
|
-
export default _default;
|
|
File without changes
|
|
File without changes
|