@chhsiao1981/use-thunk 11.0.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/package.json +1 -1
- package/src/action/thunk.ts +5 -1
- package/src/createThunk.ts +2 -9
- package/src/get.ts +3 -0
- package/src/index.ts +7 -11
- package/src/init/index.ts +1 -1
- package/src/stateTypes.ts +1 -2
- package/src/states.ts +8 -5
- package/src/thunkModule/defaultThunkModuleFuncMap.ts +1 -2
- package/src/thunkModule/index.ts +2 -3
- package/src/update.ts +0 -5
- package/src/useThunk.ts +1 -2
- package/src/useThunkReducer.ts +12 -2
- package/types/action/thunk.d.ts +1 -1
- package/types/createThunk.d.ts +0 -1
- package/types/get.d.ts +2 -0
- package/types/index.d.ts +5 -10
- package/types/stateTypes.d.ts +1 -2
- package/types/states.d.ts +3 -3
- package/types/thunkModule/index.d.ts +2 -3
- package/types/update.d.ts +0 -1
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.
|
package/package.json
CHANGED
package/src/action/thunk.ts
CHANGED
|
@@ -2,4 +2,8 @@ import type { set } from '../set'
|
|
|
2
2
|
import type { ModuleState, State } from '../stateTypes'
|
|
3
3
|
|
|
4
4
|
// Thunk
|
|
5
|
-
export type Thunk<S extends State> = (
|
|
5
|
+
export type Thunk<S extends State> = (
|
|
6
|
+
set: set<S>,
|
|
7
|
+
get: (id?: string) => S | null,
|
|
8
|
+
getModuleState: () => ModuleState<S>,
|
|
9
|
+
) => void
|
package/src/createThunk.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { createContext, type Dispatch, type SetStateAction } from 'react'
|
|
2
|
-
import { createThunk } from '.'
|
|
3
2
|
import type { ModuleState, State } from './stateTypes'
|
|
4
3
|
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
5
4
|
import type { ThunkModule } from './thunkModule'
|
|
6
5
|
|
|
7
6
|
export default <S extends State>(theDo: ThunkModule<S>) => {
|
|
8
|
-
const { name
|
|
9
|
-
const name = (propsName ? propsName : myClass) || ''
|
|
7
|
+
const { name, defaultState } = theDo
|
|
10
8
|
|
|
11
9
|
if (THUNK_CONTEXT_MAP.theMap[name]) {
|
|
12
10
|
console.warn('createThunk: already init:', name)
|
|
13
11
|
return
|
|
14
12
|
}
|
|
15
13
|
|
|
16
|
-
const moduleState: ModuleState<S> = {
|
|
14
|
+
const moduleState: ModuleState<S> = { name, nodes: {}, defaultState }
|
|
17
15
|
const setModuleState: Dispatch<SetStateAction<ModuleState<S>>> = () => {}
|
|
18
16
|
const refModuleState = { current: moduleState }
|
|
19
17
|
const context = createContext({ refModuleState: refModuleState, setModuleState })
|
|
@@ -24,8 +22,3 @@ export default <S extends State>(theDo: ThunkModule<S>) => {
|
|
|
24
22
|
|
|
25
23
|
console.info('createThunk: done:', name)
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
export const registerThunk = <S extends State>(theDo: ThunkModule<S>) => {
|
|
29
|
-
console.warn('registerThunk will be deprecated in the next version.')
|
|
30
|
-
return createThunk(theDo)
|
|
31
|
-
}
|
package/src/get.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -1,44 +1,40 @@
|
|
|
1
1
|
import type { GetModuleState, Thunk } from './action'
|
|
2
|
-
import createThunk
|
|
2
|
+
import createThunk from './createThunk'
|
|
3
3
|
import { genUUID } from './genUUID'
|
|
4
|
+
import type { get } from './get'
|
|
4
5
|
import { type InitParams, init } from './init'
|
|
5
6
|
import { remove } from './remove'
|
|
6
7
|
import type { set } from './set'
|
|
7
8
|
import { setDefaultID } from './setDefaultID'
|
|
8
9
|
import type { setMap } from './setMap'
|
|
9
|
-
import { getDefaultID, getNode, getState,
|
|
10
|
+
import { getDefaultID, getNode, getState, getStateByModule, getStateOrNullByModule } from './states'
|
|
10
11
|
import type { ModuleState, State } from './stateTypes'
|
|
11
12
|
import ThunkContext from './ThunkContext'
|
|
12
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunkModule'
|
|
13
|
-
import {
|
|
14
|
+
import { update } from './update'
|
|
14
15
|
import useThunk, { type UseThunk } from './useThunk'
|
|
15
16
|
|
|
16
17
|
export {
|
|
17
18
|
createThunk,
|
|
18
|
-
registerThunk, // to deprecate
|
|
19
19
|
useThunk,
|
|
20
20
|
type UseThunk,
|
|
21
21
|
ThunkContext,
|
|
22
22
|
type State,
|
|
23
23
|
type ModuleState,
|
|
24
|
-
type ModuleState as ClassState, // to deprecate
|
|
25
24
|
type GetModuleState,
|
|
26
|
-
type GetModuleState as GetClassState, // to deprecate
|
|
27
25
|
type Thunk,
|
|
28
26
|
type ThunkModule,
|
|
29
27
|
type ThunkModuleToFunc,
|
|
30
|
-
type set as Dispatch, // to deprecate
|
|
31
28
|
type set,
|
|
32
|
-
type setMap as DispatchFuncMap, // to deprecate
|
|
33
29
|
type setMap,
|
|
30
|
+
type get,
|
|
34
31
|
getNode,
|
|
35
32
|
getDefaultID,
|
|
33
|
+
getStateOrNullByModule,
|
|
34
|
+
getStateByModule,
|
|
36
35
|
getState,
|
|
37
|
-
mustGetState,
|
|
38
|
-
mustGetStateByThunk,
|
|
39
36
|
init,
|
|
40
37
|
type InitParams,
|
|
41
|
-
setData, // to deprecate
|
|
42
38
|
update,
|
|
43
39
|
remove,
|
|
44
40
|
setDefaultID,
|
package/src/init/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface InitParams<S extends State> {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export const init = <S extends State>(params: InitParams<S>, myuuidv4?: () => string): Thunk<S> => {
|
|
14
|
-
return (set, getModuleState) => {
|
|
14
|
+
return (set, _, getModuleState) => {
|
|
15
15
|
const myID = params.myID ?? genUUID(myuuidv4)
|
|
16
16
|
|
|
17
17
|
const { state } = params
|
package/src/stateTypes.ts
CHANGED
|
@@ -15,8 +15,7 @@ export type NodeStateMap<S extends State> = {
|
|
|
15
15
|
|
|
16
16
|
// ModuleState
|
|
17
17
|
export type ModuleState<S extends State> = {
|
|
18
|
-
name
|
|
19
|
-
myClass?: string // XXX to deprecated
|
|
18
|
+
name: string
|
|
20
19
|
defaultID?: string | null
|
|
21
20
|
nodes: NodeStateMap<S>
|
|
22
21
|
defaultState: S
|
package/src/states.ts
CHANGED
|
@@ -19,7 +19,10 @@ export const getNode = <S extends State>(
|
|
|
19
19
|
return moduleState.nodes[theID] || null
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export const
|
|
22
|
+
export const getStateOrNullByModule = <S extends State>(
|
|
23
|
+
moduleState: ModuleState<S>,
|
|
24
|
+
myID?: string,
|
|
25
|
+
): S | null => {
|
|
23
26
|
const theID = myID ? myID : getDefaultID(moduleState)
|
|
24
27
|
if (!theID) {
|
|
25
28
|
return null
|
|
@@ -32,16 +35,16 @@ export const getState = <S extends State>(moduleState: ModuleState<S>, myID?: st
|
|
|
32
35
|
return me.state
|
|
33
36
|
}
|
|
34
37
|
|
|
35
|
-
export const
|
|
36
|
-
return
|
|
38
|
+
export const getStateByModule = <S extends State>(moduleState: ModuleState<S>, myID?: string): S => {
|
|
39
|
+
return getStateOrNullByModule(moduleState, myID) || moduleState.defaultState
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
export const
|
|
42
|
+
export const getState = <S extends State, R extends ThunkModuleFunc<S>>(
|
|
40
43
|
theUseThunk: UseThunk<S, R>,
|
|
41
44
|
myID?: string,
|
|
42
45
|
): [S, setMap<S, R>, string] => {
|
|
43
46
|
const [moduleState, theDo] = theUseThunk
|
|
44
47
|
const theID = myID ? myID : getDefaultID(moduleState)
|
|
45
|
-
const state =
|
|
48
|
+
const state = getStateByModule(moduleState, theID)
|
|
46
49
|
return [state, theDo, theID]
|
|
47
50
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { ActionFunc } from '../action'
|
|
2
2
|
import { init } from '../init'
|
|
3
3
|
import { remove } from '../remove'
|
|
4
|
-
import {
|
|
4
|
+
import { update } from '../update'
|
|
5
5
|
|
|
6
6
|
export const DEFAULT_THUNK_MODULE_FUNC_MAP: DefaultThunkModuleFuncMap = {
|
|
7
7
|
init,
|
|
8
8
|
update,
|
|
9
|
-
setData,
|
|
10
9
|
remove,
|
|
11
10
|
}
|
|
12
11
|
|
package/src/thunkModule/index.ts
CHANGED
|
@@ -12,10 +12,9 @@ export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
|
|
|
12
12
|
|
|
13
13
|
// This is used as the parameter for useThunk.
|
|
14
14
|
export type ThunkModule<S extends State> = {
|
|
15
|
-
name
|
|
16
|
-
myClass?: string // XXX to deprecate
|
|
15
|
+
name: string
|
|
17
16
|
default?: Reducer<S>
|
|
18
17
|
defaultState: S
|
|
19
18
|
} & ThunkModuleBase<S>
|
|
20
19
|
|
|
21
|
-
export type ThunkModuleToFunc<T> = Omit<T, 'name' | '
|
|
20
|
+
export type ThunkModuleToFunc<T> = Omit<T, 'name' | 'default' | 'defaultState'>
|
package/src/update.ts
CHANGED
|
@@ -8,11 +8,6 @@ export const update = <S extends State>(myID: string, data: Partial<S>): BaseAct
|
|
|
8
8
|
data,
|
|
9
9
|
})
|
|
10
10
|
|
|
11
|
-
export const setData = <S extends State>(myID: string, data: Partial<S>): BaseAction => {
|
|
12
|
-
console.warn('setData will be deprecated in the next version.')
|
|
13
|
-
return update(myID, data)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
11
|
export const reduceUpdate = <S extends State>(
|
|
17
12
|
moduleState: ModuleState<S>,
|
|
18
13
|
action: BaseAction,
|
package/src/useThunk.ts
CHANGED
|
@@ -16,8 +16,7 @@ export type UseThunk<S extends State, R extends ThunkModuleFunc<S>> = [ModuleSta
|
|
|
16
16
|
export default <S extends State, R extends ThunkModuleFunc<S>>(
|
|
17
17
|
theDo: ThunkModule<S>,
|
|
18
18
|
): UseThunk<S, R> => {
|
|
19
|
-
const { name
|
|
20
|
-
const name = (propsName ? propsName : myClass) || ''
|
|
19
|
+
const { name } = theDo
|
|
21
20
|
|
|
22
21
|
// 1. It requires shared nodes for the same module to have the same setMap.
|
|
23
22
|
const isFirstTime = !SET_MAP_BY_MODULE[name]
|
package/src/useThunkReducer.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { useCallback, useContext } from 'react'
|
|
|
5
5
|
import type BaseAction from './action/baseAction'
|
|
6
6
|
import type { Reducer } from './reducer'
|
|
7
7
|
import type { set } from './set'
|
|
8
|
+
import { getStateOrNullByModule } from './states'
|
|
8
9
|
import type { ModuleState, State } from './stateTypes'
|
|
9
10
|
import { THUNK_CONTEXT_MAP } from './thunkContextMap'
|
|
10
11
|
|
|
@@ -20,7 +21,7 @@ export default <S extends State>(reducer: Reducer<S>, moduleName: string): [Modu
|
|
|
20
21
|
const { refModuleState, setModuleState: setModuleState_c } = useContext(context)
|
|
21
22
|
const getModuleState = useCallback(() => {
|
|
22
23
|
return refModuleState.current
|
|
23
|
-
}, [refModuleState])
|
|
24
|
+
}, [refModuleState]) as () => ModuleState<S>
|
|
24
25
|
|
|
25
26
|
const setModuleState = useCallback(
|
|
26
27
|
(newModuleState: ModuleState<S>) => {
|
|
@@ -30,6 +31,15 @@ export default <S extends State>(reducer: Reducer<S>, moduleName: string): [Modu
|
|
|
30
31
|
[refModuleState, setModuleState_c],
|
|
31
32
|
)
|
|
32
33
|
|
|
34
|
+
const get = useCallback(
|
|
35
|
+
(id?: string) => {
|
|
36
|
+
const moduleState = getModuleState()
|
|
37
|
+
const state = getStateOrNullByModule(moduleState, id)
|
|
38
|
+
return state
|
|
39
|
+
},
|
|
40
|
+
[getModuleState],
|
|
41
|
+
)
|
|
42
|
+
|
|
33
43
|
// 5. reducer.
|
|
34
44
|
const reduce = useCallback(
|
|
35
45
|
(action: BaseAction): ModuleState<S> => {
|
|
@@ -45,7 +55,7 @@ export default <S extends State>(reducer: Reducer<S>, moduleName: string): [Modu
|
|
|
45
55
|
(action) => {
|
|
46
56
|
if (typeof action === 'function') {
|
|
47
57
|
// action is Thunk<S, A>
|
|
48
|
-
action(set, getModuleState)
|
|
58
|
+
action(set, get, getModuleState)
|
|
49
59
|
return
|
|
50
60
|
}
|
|
51
61
|
|
package/types/action/thunk.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { set } from '../set';
|
|
2
2
|
import type { ModuleState, State } from '../stateTypes';
|
|
3
|
-
export type Thunk<S extends State> = (set: set<S>, getModuleState: () => ModuleState<S>) => void;
|
|
3
|
+
export type Thunk<S extends State> = (set: set<S>, get: (id?: string) => S | null, getModuleState: () => ModuleState<S>) => void;
|
package/types/createThunk.d.ts
CHANGED
|
@@ -2,4 +2,3 @@ import type { State } from './stateTypes';
|
|
|
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;
|
package/types/get.d.ts
ADDED
package/types/index.d.ts
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
import type { GetModuleState, Thunk } from './action';
|
|
2
|
-
import createThunk
|
|
2
|
+
import createThunk from './createThunk';
|
|
3
3
|
import { genUUID } from './genUUID';
|
|
4
|
+
import type { get } from './get';
|
|
4
5
|
import { type InitParams, init } from './init';
|
|
5
6
|
import { remove } from './remove';
|
|
6
7
|
import type { set } from './set';
|
|
7
8
|
import { setDefaultID } from './setDefaultID';
|
|
8
9
|
import type { setMap } from './setMap';
|
|
9
|
-
import { getDefaultID, getNode, getState,
|
|
10
|
+
import { getDefaultID, getNode, getState, getStateByModule, getStateOrNullByModule } from './states';
|
|
10
11
|
import type { ModuleState, State } from './stateTypes';
|
|
11
12
|
import ThunkContext from './ThunkContext';
|
|
12
13
|
import type { ThunkModule, ThunkModuleToFunc } from './thunkModule';
|
|
13
|
-
import {
|
|
14
|
+
import { update } from './update';
|
|
14
15
|
import useThunk, { type UseThunk } from './useThunk';
|
|
15
|
-
export { createThunk,
|
|
16
|
-
useThunk, type UseThunk, ThunkContext, type State, type ModuleState, type ModuleState as ClassState, // to deprecate
|
|
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, };
|
|
16
|
+
export { createThunk, useThunk, type UseThunk, ThunkContext, type State, type ModuleState, type GetModuleState, type Thunk, type ThunkModule, type ThunkModuleToFunc, type set, type setMap, type get, getNode, getDefaultID, getStateOrNullByModule, getStateByModule, getState, init, type InitParams, update, remove, setDefaultID, genUUID, };
|
package/types/stateTypes.d.ts
CHANGED
|
@@ -9,8 +9,7 @@ export type NodeStateMap<S extends State> = {
|
|
|
9
9
|
[key: string]: NodeState<S>;
|
|
10
10
|
};
|
|
11
11
|
export type ModuleState<S extends State> = {
|
|
12
|
-
name
|
|
13
|
-
myClass?: string;
|
|
12
|
+
name: string;
|
|
14
13
|
defaultID?: string | null;
|
|
15
14
|
nodes: NodeStateMap<S>;
|
|
16
15
|
defaultState: S;
|
package/types/states.d.ts
CHANGED
|
@@ -4,6 +4,6 @@ import type { ThunkModuleFunc } from './thunkModule';
|
|
|
4
4
|
import type { UseThunk } from './useThunk';
|
|
5
5
|
export declare const getDefaultID: <S extends State>(moduleState: ModuleState<S>) => string;
|
|
6
6
|
export declare const getNode: <S extends State>(moduleState: ModuleState<S>, myID?: string) => NodeState<S> | null;
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const
|
|
7
|
+
export declare const getStateOrNullByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S | null;
|
|
8
|
+
export declare const getStateByModule: <S extends State>(moduleState: ModuleState<S>, myID?: string) => S;
|
|
9
|
+
export declare const getState: <S extends State, R extends ThunkModuleFunc<S>>(theUseThunk: UseThunk<S, R>, myID?: string) => [S, setMap<S, R>, string];
|
|
@@ -8,9 +8,8 @@ export interface ThunkModuleFunc<S extends State> extends ThunkModuleBase<S> {
|
|
|
8
8
|
[action: string]: ThunkFunc<S>;
|
|
9
9
|
}
|
|
10
10
|
export type ThunkModule<S extends State> = {
|
|
11
|
-
name
|
|
12
|
-
myClass?: string;
|
|
11
|
+
name: string;
|
|
13
12
|
default?: Reducer<S>;
|
|
14
13
|
defaultState: S;
|
|
15
14
|
} & ThunkModuleBase<S>;
|
|
16
|
-
export type ThunkModuleToFunc<T> = Omit<T, 'name' | '
|
|
15
|
+
export type ThunkModuleToFunc<T> = Omit<T, 'name' | 'default' | 'defaultState'>;
|
package/types/update.d.ts
CHANGED
|
@@ -2,5 +2,4 @@ import type BaseAction from './action/baseAction';
|
|
|
2
2
|
import type { ModuleState, State } from './stateTypes';
|
|
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>;
|