@chhsiao1981/use-thunk 10.4.0 → 12.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 +63 -59
- package/dist/ThunkContext.d.ts +7 -0
- package/dist/createReducer.d.ts +4 -0
- package/dist/createThunk.d.ts +4 -0
- package/dist/dispatch.d.ts +4 -0
- package/dist/dispatchFuncMap.d.ts +18 -0
- package/dist/genUUID.d.ts +1 -0
- package/dist/index.d.ts +15 -0
- package/{types → dist}/init.d.ts +0 -3
- package/dist/reduceMap.d.ts +6 -0
- package/dist/reducer.d.ts +5 -0
- package/dist/remove.d.ts +5 -0
- package/dist/setDefaultID.d.ts +5 -0
- package/dist/stateTypes.d.ts +16 -0
- package/dist/states.d.ts +9 -0
- package/{types → dist}/thunk.d.ts +6 -3
- package/dist/thunkContextMap.d.ts +15 -0
- package/dist/thunkContextTypes.d.ts +8 -0
- package/{types → dist}/thunkModuleFuncMap.d.ts +1 -0
- package/dist/update.d.ts +6 -0
- package/dist/useThunk.d.ts +12 -0
- package/dist/useThunkReducer.d.ts +17 -0
- package/package.json +1 -1
- package/src/ThunkContext.tsx +17 -17
- package/src/action/ActionOrThunk.ts +5 -0
- package/src/action/baseAction.ts +6 -0
- package/src/action/index.ts +16 -0
- package/src/action/thunk.ts +9 -0
- package/src/createReducer.ts +7 -7
- package/src/createThunk.ts +24 -0
- package/src/get.ts +3 -0
- package/src/index.ts +20 -30
- package/src/init/index.ts +26 -0
- package/src/init/initCore.ts +31 -0
- package/src/reduceMap.ts +3 -4
- package/src/reducer.ts +4 -4
- package/src/remove.ts +10 -25
- package/src/set.ts +7 -0
- package/src/setDefaultID.ts +5 -5
- package/src/setMap.ts +65 -0
- package/src/stateTypes.ts +3 -3
- package/src/states.ts +21 -18
- package/src/thunkContextMap.ts +4 -4
- package/src/thunkContextTypes.ts +4 -4
- package/src/thunkModule/defaultThunkModuleFuncMap.ts +15 -0
- package/src/thunkModule/index.ts +20 -0
- package/src/update.ts +26 -0
- package/src/useThunk.ts +23 -44
- package/src/useThunkReducer.ts +41 -42
- package/types/ThunkContext.d.ts +4 -4
- package/types/action/ActionOrThunk.d.ts +4 -0
- package/types/action/baseAction.d.ts +5 -0
- package/types/action/index.d.ts +8 -0
- package/types/action/thunk.d.ts +3 -0
- package/types/createThunk.d.ts +4 -0
- package/types/dispatch.d.ts +1 -1
- package/types/dispatchFuncMap.d.ts +4 -5
- package/types/get.d.ts +2 -0
- package/types/index.d.ts +10 -9
- package/types/init/index.d.ts +7 -0
- package/types/init/initCore.d.ts +9 -0
- package/types/reducer.d.ts +4 -4
- package/types/remove.d.ts +4 -4
- package/types/set.d.ts +4 -0
- package/types/setDefaultID.d.ts +3 -3
- package/types/setMap.d.ts +17 -0
- package/types/stateTypes.d.ts +2 -2
- package/types/states.d.ts +8 -8
- package/types/thunkContextMap.d.ts +4 -4
- package/types/thunkContextTypes.d.ts +4 -4
- package/types/thunkModule/defaultThunkModuleFuncMap.d.ts +5 -0
- package/types/thunkModule/index.d.ts +15 -0
- package/types/update.d.ts +5 -0
- package/types/useThunk.d.ts +5 -8
- package/types/useThunkReducer.d.ts +5 -11
- package/dist/index.js +0 -1723
- package/dist/index.umd.cjs +0 -50
- package/src/action.ts +0 -20
- package/src/dispatch.ts +0 -6
- package/src/dispatchFuncMap.ts +0 -67
- package/src/init.ts +0 -64
- package/src/registerThunk.ts +0 -42
- package/src/setData.ts +0 -26
- package/src/thunk.ts +0 -20
- package/src/thunkModuleFuncMap.ts +0 -11
- package/types/registerThunk.d.ts +0 -4
- package/types/setData.d.ts +0 -5
- /package/{types → dist}/action.d.ts +0 -0
package/README.md
CHANGED
|
@@ -19,9 +19,15 @@ Please check [demo-use-thunk](https://github.com/chhsiao1981/demo-use-thunk) for
|
|
|
19
19
|
|
|
20
20
|
### Breaking Changes
|
|
21
21
|
|
|
22
|
-
* Starting from `
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
* Starting from [`12.0.0`](https://github.com/chhsiao1981/use-thunk/releases/tag/12.0.0): redefining the `getState` functions.
|
|
23
|
+
1. `getState` => `getStateOrNullByModule`, `mustGetState` => `getStateByModule`.
|
|
24
|
+
2. `mustGetStateByThunk` => `getState`.
|
|
25
|
+
* Starting from [`11.0.0`](https://github.com/chhsiao1981/use-thunk/releases/tag/11.0.0):
|
|
26
|
+
1. `setData` => `update`, `registerThunk` => `createThunk`, `dispatch` => `set`, `ClassState` => `ModuleState`, and `ModuleState.myClass` => `ModuleState.name`.
|
|
27
|
+
2. `Thunk` is `(set, get, getModuleState) => {}`, where `get(id)` directly returns object-level state.
|
|
28
|
+
* Starting from [`10.0.0`](https://github.com/chhsiao1981/use-thunk/releases/tag/10.0.0): The ClassState is shared globally, with `registerThunk` and `ThunkContext`.
|
|
29
|
+
* Starting from [`9.0.0`](https://github.com/chhsiao1981/use-thunk/releases/tag/9.0.0): npm package is renamed as [@chhsiao1981/use-thunk](https://www.npmjs.com/package/%40chhsiao1981/use-thunk)
|
|
30
|
+
* Starting from [`8.0.0`](https://github.com/chhsiao1981/use-thunk/releases/tag/8.0.0): [Totally renamed as `useThunk`](https://github.com/chhsiao1981/use-thunk/issues/105).
|
|
25
31
|
|
|
26
32
|
## Install
|
|
27
33
|
|
|
@@ -32,9 +38,9 @@ Please check [demo-use-thunk](https://github.com/chhsiao1981/demo-use-thunk) for
|
|
|
32
38
|
Thunk module able to do increment (reducers/increment.ts):
|
|
33
39
|
|
|
34
40
|
```ts
|
|
35
|
-
import { init as _init,
|
|
41
|
+
import { init as _init, update, Thunk, type State as rState, genUUID } from '@chhsiao1981/use-thunk'
|
|
36
42
|
|
|
37
|
-
export const
|
|
43
|
+
export const name = 'demo/Increment'
|
|
38
44
|
|
|
39
45
|
export interface State extends rState {
|
|
40
46
|
count: number
|
|
@@ -46,20 +52,20 @@ export const defaultState: State = {
|
|
|
46
52
|
|
|
47
53
|
export const init = (): Thunk<State> => {
|
|
48
54
|
const myID = genUUID()
|
|
49
|
-
return async (
|
|
50
|
-
|
|
55
|
+
return async (set) => {
|
|
56
|
+
set(_init({myID, state: defaultState}))
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
export const increment = (myID: string): Thunk<State> => {
|
|
55
|
-
return async (
|
|
56
|
-
let
|
|
57
|
-
let me = getState(classState, myID)
|
|
61
|
+
return async (set, get) => {
|
|
62
|
+
let me = get(myID)
|
|
58
63
|
if(!me) {
|
|
59
64
|
return
|
|
60
65
|
}
|
|
66
|
+
const {count} = me
|
|
61
67
|
|
|
62
|
-
|
|
68
|
+
set(update(myID, { count: count + 1 }))
|
|
63
69
|
}
|
|
64
70
|
}
|
|
65
71
|
```
|
|
@@ -67,7 +73,7 @@ export const increment = (myID: string): Thunk<State> => {
|
|
|
67
73
|
App.tsx:
|
|
68
74
|
|
|
69
75
|
```tsx
|
|
70
|
-
import { type ThunkModuleToFunc, useThunk,
|
|
76
|
+
import { type ThunkModuleToFunc, useThunk, getState } from '@chhsiao1981/use-thunk'
|
|
71
77
|
import * as DoIncrement from './reducers/increment'
|
|
72
78
|
|
|
73
79
|
type TDoIncrement = ThunkModuleToFunc(typeof DoIncrement)
|
|
@@ -76,17 +82,14 @@ type Props = {
|
|
|
76
82
|
}
|
|
77
83
|
|
|
78
84
|
export default (props: Props) => {
|
|
79
|
-
const
|
|
85
|
+
const useIncrement = useThunk<DoIncrement.State, TDoIncrement>(DoIncrement, StateType.LOCAL)
|
|
86
|
+
const [increment, doIncrement, incrementID] = getState(useIncrement)
|
|
80
87
|
|
|
81
88
|
//init
|
|
82
89
|
useEffect(() => {
|
|
83
90
|
doIncrement.init()
|
|
84
91
|
}, [])
|
|
85
92
|
|
|
86
|
-
// states
|
|
87
|
-
const incrementID = getDefaultID(classStateIncrement)
|
|
88
|
-
const increment = getState(classStateIncrement) || DoIncrement.defaultState
|
|
89
|
-
|
|
90
93
|
// to render
|
|
91
94
|
return (
|
|
92
95
|
<div>
|
|
@@ -99,13 +102,13 @@ export default (props: Props) => {
|
|
|
99
102
|
|
|
100
103
|
main.tsx:
|
|
101
104
|
```tsx
|
|
102
|
-
import {
|
|
105
|
+
import { createThunk, ThunkContext } from "@chhsiao1981/use-thunk";
|
|
103
106
|
import { StrictMode } from "react";
|
|
104
107
|
import { createRoot } from "react-dom/client";
|
|
105
108
|
import * as DoIncrement from './reducers/increment'
|
|
106
109
|
import App from "./App.tsx";
|
|
107
110
|
|
|
108
|
-
|
|
111
|
+
createThunk(DoIncrement)
|
|
109
112
|
|
|
110
113
|
createRoot(document.getElementById("root")!).render(
|
|
111
114
|
<StrictMode>
|
|
@@ -121,13 +124,15 @@ createRoot(document.getElementById("root")!).render(
|
|
|
121
124
|
```ts
|
|
122
125
|
import type { State as rState } from '@chhsiao1981/use-thunk'
|
|
123
126
|
|
|
124
|
-
//
|
|
125
|
-
export const
|
|
127
|
+
// Thunk-module name.
|
|
128
|
+
export const name = ""
|
|
126
129
|
|
|
127
130
|
// state definition of the reducer.
|
|
128
131
|
export interface State extends rState {
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
export const defaultState: State = {}
|
|
135
|
+
|
|
131
136
|
.
|
|
132
137
|
.
|
|
133
138
|
.
|
|
@@ -136,18 +141,16 @@ export interface State extends rState {
|
|
|
136
141
|
### Must Included in a Top-level Component
|
|
137
142
|
|
|
138
143
|
```ts
|
|
139
|
-
import { type ThunkModuleToFunc, useThunk,
|
|
144
|
+
import { type ThunkModuleToFunc, useThunk, getState } from '@chhsiao1981/use-thunk'
|
|
140
145
|
import * as DoModule from '../reducers/module'
|
|
141
146
|
|
|
142
147
|
type TDoModule = ThunkModuleToFunc<typeof DoModule>
|
|
143
148
|
|
|
144
149
|
const Component = () => {
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
const moduleID = getDefaultID(stateModule)
|
|
148
|
-
const theModule = getState(stateModule)
|
|
150
|
+
const useModule = useThunk<DoModule.State, TDoModule>(DoModule)
|
|
151
|
+
const [module, doModule, moduleID] = getState(useModule)
|
|
149
152
|
|
|
150
|
-
|
|
153
|
+
.
|
|
151
154
|
.
|
|
152
155
|
.
|
|
153
156
|
}
|
|
@@ -156,7 +159,8 @@ const Component = () => {
|
|
|
156
159
|
### Must Included in main.tsx
|
|
157
160
|
|
|
158
161
|
```tsx
|
|
159
|
-
|
|
162
|
+
import { createThunk, ThunkContext } from '@chhsiao1981/use-thunk'
|
|
163
|
+
createThunk(...)
|
|
160
164
|
.
|
|
161
165
|
.
|
|
162
166
|
.
|
|
@@ -182,8 +186,8 @@ with the following features:
|
|
|
182
186
|
For example, the example [in the redux link](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape) is represented as:
|
|
183
187
|
|
|
184
188
|
```ts
|
|
185
|
-
|
|
186
|
-
|
|
189
|
+
moduleStatePost = {
|
|
190
|
+
name: 'post',
|
|
187
191
|
nodes: {
|
|
188
192
|
[uuid-post1] : {
|
|
189
193
|
id: uuid-post1,
|
|
@@ -202,7 +206,7 @@ classStatePost = {
|
|
|
202
206
|
}
|
|
203
207
|
}
|
|
204
208
|
},
|
|
205
|
-
defaultID,
|
|
209
|
+
defaultID,s
|
|
206
210
|
defaultState,
|
|
207
211
|
}
|
|
208
212
|
```
|
|
@@ -210,8 +214,8 @@ classStatePost = {
|
|
|
210
214
|
and:
|
|
211
215
|
|
|
212
216
|
```ts
|
|
213
|
-
|
|
214
|
-
myClass: '
|
|
217
|
+
moduleStateComment = {
|
|
218
|
+
myClass: 'module',
|
|
215
219
|
nodes: {
|
|
216
220
|
[uuid-comment1] : {
|
|
217
221
|
id: uuid-comment1,
|
|
@@ -256,8 +260,8 @@ classStateComment = {
|
|
|
256
260
|
|
|
257
261
|
and:
|
|
258
262
|
```ts
|
|
259
|
-
|
|
260
|
-
|
|
263
|
+
moduleStateUser = {
|
|
264
|
+
name: 'user',
|
|
261
265
|
nodes: {
|
|
262
266
|
[uuid-user1] : {
|
|
263
267
|
id: uuid-user1,
|
|
@@ -290,50 +294,50 @@ classStateUser = {
|
|
|
290
294
|
|
|
291
295
|
### Basic
|
|
292
296
|
|
|
293
|
-
##### `
|
|
297
|
+
##### `createThunk(theModule: ThunkModule)`
|
|
294
298
|
|
|
295
|
-
|
|
299
|
+
Create a module state for `theModule`.
|
|
296
300
|
|
|
297
|
-
|
|
301
|
+
##### `<ThunkContext>{children}</ThunkContext>`
|
|
298
302
|
|
|
299
|
-
|
|
303
|
+
Rendering Thunk context.
|
|
300
304
|
|
|
301
|
-
|
|
305
|
+
##### `useThunk(theDo: ThunkModule): UseThunk`
|
|
302
306
|
|
|
303
|
-
|
|
307
|
+
Similar to `React.useReducer`, but we use `useThunk`, and we also bind the actions with set (similar concept as `mapDispatchToProps`).
|
|
304
308
|
|
|
305
|
-
|
|
309
|
+
return: `UseThunk`
|
|
306
310
|
|
|
307
|
-
|
|
311
|
+
### Default Thunk functions.
|
|
308
312
|
|
|
309
|
-
|
|
313
|
+
##### `init({myID, parentID, doParent, state}, myuuidv4?)`
|
|
310
314
|
|
|
311
|
-
|
|
315
|
+
initializing the state.
|
|
312
316
|
|
|
313
|
-
|
|
317
|
+
##### `update(myID, data)`
|
|
314
318
|
|
|
315
|
-
|
|
319
|
+
update the data to myID.
|
|
316
320
|
|
|
317
|
-
##### `
|
|
321
|
+
##### `remove(myID)`
|
|
318
322
|
|
|
319
|
-
|
|
323
|
+
remove the state.
|
|
320
324
|
|
|
321
|
-
##### `
|
|
325
|
+
##### `genUUID(myuuidv4?: () => string): string`
|
|
322
326
|
|
|
323
|
-
|
|
327
|
+
generate uuid for the state.
|
|
324
328
|
|
|
325
|
-
|
|
329
|
+
### State
|
|
326
330
|
|
|
327
|
-
|
|
331
|
+
##### `getState(theUseThunk: UseThunk, myID?: string): [state, doModule, theID]`
|
|
328
332
|
|
|
329
|
-
|
|
333
|
+
Get the state of `myID` by `UseThunk`. Get the state of `defaultID` if `myID` is not present. Return `defaultState` if not available.
|
|
330
334
|
|
|
331
|
-
|
|
335
|
+
return: `[state, doModule, theID]`
|
|
332
336
|
|
|
333
|
-
|
|
337
|
+
##### `getStateOrNullByModule(moduleState: ModuleState, myID?: string): state | null`
|
|
334
338
|
|
|
335
|
-
|
|
339
|
+
Get the state of `myID`. Get the state of `defaultID` if `myID` is not present. Return `null` if not available.
|
|
336
340
|
|
|
337
|
-
##### `
|
|
341
|
+
##### `getStateByModule(moduleState: ModuleState, myID?: string): state`
|
|
338
342
|
|
|
339
|
-
Get the
|
|
343
|
+
Get the state of `myID`. Get the state of `defaultID` if `myID` is not present. Return `defaultState` if not available.
|
|
@@ -0,0 +1,18 @@
|
|
|
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 {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const genUUID: (myuuidv4?: () => string) => string;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { GetClassState, Thunk } from './action';
|
|
2
|
+
import createThunk, { registerThunk } from './createThunk';
|
|
3
|
+
import type { Dispatch } from './dispatch';
|
|
4
|
+
import type { DispatchFuncMap } from './dispatchFuncMap';
|
|
5
|
+
import { genUUID } from './genUUID';
|
|
6
|
+
import { type InitParams, init } from './init';
|
|
7
|
+
import { remove } from './remove';
|
|
8
|
+
import { setDefaultID } from './setDefaultID';
|
|
9
|
+
import { getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk } from './states';
|
|
10
|
+
import type { ClassState, NodeState, NodeStateMap, State } from './stateTypes';
|
|
11
|
+
import ThunkContext from './ThunkContext';
|
|
12
|
+
import type { ThunkModule, ThunkModuleToFunc } from './thunk';
|
|
13
|
+
import { setData, update } from './update';
|
|
14
|
+
import useThunk, { type UseThunk } from './useThunk';
|
|
15
|
+
export { createThunk, registerThunk, useThunk, ThunkContext, type UseThunk, type State, type NodeState, type NodeStateMap, type ClassState, type GetClassState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type Dispatch, type DispatchFuncMap, getDefaultID, getNode, getState, mustGetState, mustGetStateByThunk, init, type InitParams, setData, update, remove, setDefaultID, genUUID, };
|
package/{types → dist}/init.d.ts
RENAMED
|
@@ -2,9 +2,6 @@ import type { BaseAction, Thunk } from './action';
|
|
|
2
2
|
import type { ClassState, State } from './stateTypes';
|
|
3
3
|
export interface InitParams<S extends State> {
|
|
4
4
|
myID?: string;
|
|
5
|
-
parentID?: string;
|
|
6
|
-
doParent?: DispatchFuncMap;
|
|
7
|
-
parentClass?: string;
|
|
8
5
|
state: S;
|
|
9
6
|
}
|
|
10
7
|
export declare const init: <S extends State>(params: InitParams<S>, myuuidv4?: () => string) => Thunk<S>;
|
|
@@ -0,0 +1,5 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
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>;
|
|
@@ -0,0 +1,5 @@
|
|
|
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>;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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];
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import type { ActionFunc } from './action';
|
|
2
2
|
import type { Reducer } from './reducer';
|
|
3
3
|
import type { State } from './stateTypes';
|
|
4
|
-
export interface
|
|
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> {
|
|
5
8
|
[action: string]: ActionFunc<S>;
|
|
6
9
|
}
|
|
7
|
-
export type ThunkModule<S extends State
|
|
10
|
+
export type ThunkModule<S extends State> = {
|
|
8
11
|
myClass: string;
|
|
9
12
|
default?: Reducer<S>;
|
|
10
13
|
defaultState: S;
|
|
11
|
-
} &
|
|
14
|
+
} & ThunkModuleBase<S>;
|
|
12
15
|
export type ThunkModuleToFunc<T> = Omit<T, 'myClass' | 'default' | 'defaultState'>;
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1,8 @@
|
|
|
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,5 +1,6 @@
|
|
|
1
1
|
export declare const DEFAULT_THUNK_MODULE_FUNC_MAP: {
|
|
2
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;
|
|
3
4
|
setData: <S extends import("./stateTypes").State>(myID: string, data: Partial<S>) => import("./action").BaseAction;
|
|
4
5
|
remove: <S extends import("./stateTypes").State>(myID: string) => import("./action").Thunk<S>;
|
|
5
6
|
};
|
package/dist/update.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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>;
|
|
@@ -0,0 +1,12 @@
|
|
|
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;
|
|
@@ -0,0 +1,17 @@
|
|
|
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/package.json
CHANGED
package/src/ThunkContext.tsx
CHANGED
|
@@ -2,42 +2,42 @@ import { type ReactNode, useMemo, useState } from 'react'
|
|
|
2
2
|
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
3
3
|
|
|
4
4
|
type Props = {
|
|
5
|
-
|
|
5
|
+
modules?: string[]
|
|
6
6
|
children?: ReactNode
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
const ThunkContext = (props: Props): ReactNode => {
|
|
10
|
-
const {
|
|
11
|
-
const
|
|
12
|
-
// 0. if there is no Thunk
|
|
13
|
-
if (
|
|
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
14
|
return children
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
// render the 0th
|
|
18
|
-
const
|
|
17
|
+
// render the 0th module.
|
|
18
|
+
const theModule = modules[0]
|
|
19
19
|
|
|
20
|
-
// 1. get the context and
|
|
21
|
-
const { context: Context_m,
|
|
20
|
+
// 1. get the context and moduleState from context map.
|
|
21
|
+
const { context: Context_m, refModuleState } = THUNK_CONTEXT_MAP.theMap[theModule]
|
|
22
22
|
|
|
23
|
-
// 2. setup
|
|
23
|
+
// 2. setup moduleState.
|
|
24
24
|
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
25
|
-
const [
|
|
26
|
-
|
|
25
|
+
const [moduleState, setModuleState] = useState(refModuleState.current)
|
|
26
|
+
refModuleState.current = moduleState
|
|
27
27
|
|
|
28
|
-
// 3. value reset only if
|
|
28
|
+
// 3. value reset only if moduleState is changed.
|
|
29
29
|
// biome-ignore lint/correctness/useHookAtTopLevel: the order is fixed.
|
|
30
30
|
const value = useMemo(
|
|
31
31
|
() => ({
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
refModuleState: refModuleState,
|
|
33
|
+
setModuleState: setModuleState,
|
|
34
34
|
}),
|
|
35
|
-
[
|
|
35
|
+
[moduleState],
|
|
36
36
|
)
|
|
37
37
|
|
|
38
38
|
// 4. get theChildren
|
|
39
39
|
const theChildren =
|
|
40
|
-
|
|
40
|
+
modules.length === 1 ? children : ThunkContext({ modules: modules.slice(1), children })
|
|
41
41
|
|
|
42
42
|
// 5. return context.
|
|
43
43
|
return <Context_m.Provider value={value}>{theChildren}</Context_m.Provider>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ModuleState, State } from '../stateTypes'
|
|
2
|
+
import type { ActionOrThunk } from './ActionOrThunk'
|
|
3
|
+
import type BaseAction from './baseAction'
|
|
4
|
+
import type { Thunk } from './thunk'
|
|
5
|
+
|
|
6
|
+
export type { Thunk, ActionOrThunk, BaseAction }
|
|
7
|
+
|
|
8
|
+
// ActionFunc
|
|
9
|
+
// biome-ignore lint/suspicious/noExplicitAny: params can be any type.
|
|
10
|
+
export type ActionFunc<S extends State> = (...params: any[]) => ActionOrThunk<S>
|
|
11
|
+
|
|
12
|
+
// biome-ignore lint/suspicious/noExplicitAny: params can by any type.
|
|
13
|
+
export type ThunkFunc<S extends State> = (...params: any[]) => Thunk<S>
|
|
14
|
+
|
|
15
|
+
// GetModuleState
|
|
16
|
+
export type GetModuleState<S extends State> = () => ModuleState<S>
|
package/src/createReducer.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type BaseAction from './action/baseAction'
|
|
2
2
|
import { DEFAULT_REDUCE_MAP, type ReduceMap } from './reduceMap'
|
|
3
3
|
import type { Reducer } from './reducer'
|
|
4
4
|
|
|
5
|
-
import type {
|
|
5
|
+
import type { ModuleState, State } from './stateTypes'
|
|
6
6
|
|
|
7
7
|
export const createReducer = <S extends State>(reduceMap?: ReduceMap<S>): Reducer<S> => {
|
|
8
|
-
return (
|
|
8
|
+
return (moduleState: ModuleState<S>, action: BaseAction): ModuleState<S> => {
|
|
9
9
|
if (!action) {
|
|
10
|
-
return
|
|
10
|
+
return moduleState
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
if (reduceMap?.[action.type]) {
|
|
14
|
-
return reduceMap[action.type](
|
|
14
|
+
return reduceMap[action.type](moduleState, action)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
const defaultReduceMap = DEFAULT_REDUCE_MAP<S>()
|
|
18
18
|
if (defaultReduceMap?.[action.type]) {
|
|
19
|
-
return defaultReduceMap[action.type](
|
|
19
|
+
return defaultReduceMap[action.type](moduleState, action)
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
return
|
|
22
|
+
return moduleState
|
|
23
23
|
}
|
|
24
24
|
}
|