Skip to content

Commit ffb11fa

Browse files
ArrayZoneYourcatee
authored andcommitted
feat(action): add return value
1 parent 642da03 commit ffb11fa

File tree

8 files changed

+194
-37
lines changed

8 files changed

+194
-37
lines changed

__test__/Model/return.spec.ts

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/// <reference path="../index.d.ts" />
2+
import { renderHook, act } from '@testing-library/react-hooks'
3+
import { RetTester } from '..'
4+
import { Model } from '../../src'
5+
6+
describe('action return value', () => {
7+
test('return object value', async () => {
8+
let actions: any
9+
const { useStore } = Model(RetTester)
10+
renderHook(() => {
11+
;[, actions] = useStore()
12+
})
13+
await act(async () => {
14+
const retVal = await actions.add(5)
15+
expect(retVal).toEqual({ count: 5 })
16+
const retVal_2 = await actions.add(5)
17+
expect(retVal).toEqual({ count: 5 })
18+
expect(retVal_2).toEqual({ count: 10 })
19+
})
20+
})
21+
22+
test('return promise value', async () => {
23+
let actions: any
24+
const { useStore } = Model(RetTester)
25+
renderHook(() => {
26+
;[, actions] = useStore()
27+
})
28+
await act(async () => {
29+
const retVal = await actions.asyncAdd(5)
30+
expect(retVal).toEqual({ count: 5 })
31+
const retVal_2 = await actions.asyncAdd(5)
32+
expect(retVal).toEqual({ count: 5 })
33+
expect(retVal_2).toEqual({ count: 10 })
34+
})
35+
})
36+
37+
test('return produce function', async () => {
38+
const asyncPrototype = Object.getPrototypeOf(async () => {})
39+
const isAsync = (input: unknown) => {
40+
return Object.getPrototypeOf(input) === asyncPrototype
41+
}
42+
let actions: any
43+
const { useStore } = Model(RetTester)
44+
renderHook(() => {
45+
;[, actions] = useStore()
46+
})
47+
await act(async () => {
48+
const retVal = await actions.produceAdd(5)
49+
expect(isAsync(retVal)).toBe(true)
50+
const retVal_2 = await actions.produceAdd(5)
51+
expect(isAsync(retVal)).toBe(true)
52+
expect(isAsync(retVal_2)).toBe(true)
53+
})
54+
})
55+
56+
test('return async produce function', async () => {
57+
const asyncPrototype = Object.getPrototypeOf(async () => {})
58+
const isAsync = (input: unknown) => {
59+
return Object.getPrototypeOf(input) === asyncPrototype
60+
}
61+
let actions: any
62+
const { useStore } = Model(RetTester)
63+
renderHook(() => {
64+
;[, actions] = useStore()
65+
})
66+
await act(async () => {
67+
const retVal = await actions.asyncProduceAdd(5)
68+
expect(isAsync(retVal)).toBe(true)
69+
const retVal_2 = await actions.asyncProduceAdd(5)
70+
expect(isAsync(retVal)).toBe(true)
71+
expect(isAsync(retVal_2)).toBe(true)
72+
})
73+
})
74+
75+
test('return action', async () => {
76+
let actions: any
77+
const { useStore } = Model(RetTester)
78+
renderHook(() => {
79+
;[, actions] = useStore()
80+
})
81+
await act(async () => {
82+
const retVal = await actions.hocAdd(5)
83+
expect(retVal).toEqual({ count: 5 })
84+
const retVal_2 = await actions.hocAdd(5)
85+
expect(retVal).toEqual({ count: 5 })
86+
expect(retVal_2).toEqual({ count: 10 })
87+
})
88+
})
89+
90+
test('return async action', async () => {
91+
let actions: any
92+
const { useStore } = Model(RetTester)
93+
renderHook(() => {
94+
;[, actions] = useStore()
95+
})
96+
await act(async () => {
97+
const retVal = await actions.asyncHocAdd(5)
98+
expect(retVal).toEqual({ count: 5 })
99+
const retVal_2 = await actions.asyncHocAdd(5)
100+
expect(retVal).toEqual({ count: 5 })
101+
expect(retVal_2).toEqual({ count: 10 })
102+
})
103+
})
104+
})

__test__/index.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ type CounterState = {
22
count: number
33
}
44

5+
type RetState = {
6+
count: number
7+
extra: string
8+
}
9+
510
type SSRCounterState = {
611
count: number
712
clientKey: string
@@ -33,6 +38,15 @@ type NextCounterActionParams = {
3338
add: number
3439
}
3540

41+
type RetActionParams = {
42+
add: number
43+
asyncAdd: number
44+
produceAdd: number
45+
asyncProduceAdd: number
46+
hocAdd: number
47+
asyncHocAdd: number
48+
}
49+
3650
type ExtraActionParams = {
3751
add: number
3852
addCaller: undefined

__test__/index.ts

+53-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const ActionsTester: ModelType<ActionTesterState, ActionTesterParams> = {
1717
actions.parse()
1818
},
1919
parse: () => {
20-
return state => {
20+
return (state) => {
2121
state.data = state.response.data
2222
}
2323
}
@@ -43,8 +43,8 @@ export const Counter: ModelType<
4343
addCaller: (_, { actions }) => {
4444
actions.add(5)
4545
},
46-
increment: params => {
47-
return state => {
46+
increment: (params) => {
47+
return (state) => {
4848
state.count += params
4949
}
5050
}
@@ -66,8 +66,8 @@ export const NextCounter: ModelType<
6666
addCaller: (_, { actions }) => {
6767
actions.add(5)
6868
},
69-
increment: params => {
70-
return state => {
69+
increment: (params) => {
70+
return (state) => {
7171
state.count += params
7272
}
7373
}
@@ -77,7 +77,7 @@ export const NextCounter: ModelType<
7777

7878
// common used case
7979
interface CommonState {
80-
xxx: string,
80+
xxx: string
8181
yyy: number
8282
}
8383

@@ -87,7 +87,7 @@ interface CommonActionParams {
8787

8888
export const State: ModelType<CommonState, CommonActionParams> = {
8989
state: {
90-
xxx: "",
90+
xxx: '',
9191
yyy: -1
9292
},
9393
actions: {
@@ -123,8 +123,8 @@ export const Theme: ModelType<ThemeState, ThemeActionParams> = {
123123

124124
export const AsyncCounter: ModelType<CounterState, CounterActionParams> = {
125125
actions: {
126-
increment: params => {
127-
return state => {
126+
increment: (params) => {
127+
return (state) => {
128128
state.count += params
129129
}
130130
}
@@ -137,8 +137,8 @@ export const AsyncCounter: ModelType<CounterState, CounterActionParams> = {
137137

138138
export const SSRCounter: ModelType<SSRCounterState, CounterActionParams> = {
139139
actions: {
140-
increment: params => {
141-
return state => {
140+
increment: (params) => {
141+
return (state) => {
142142
state.count += params
143143
}
144144
}
@@ -151,8 +151,8 @@ export const SSRCounter: ModelType<SSRCounterState, CounterActionParams> = {
151151

152152
export const AsyncNull: ModelType<CounterState, CounterActionParams> = {
153153
actions: {
154-
increment: params => {
155-
return state => {
154+
increment: (params) => {
155+
return (state) => {
156156
state.count += params
157157
}
158158
}
@@ -175,6 +175,40 @@ const timeoutCounter: ModelType<CounterState, CounterActionParams> = {
175175
state: { count: 0 }
176176
}
177177

178+
export const RetTester: ModelType<RetState, RetActionParams> = {
179+
state: {
180+
count: 0,
181+
extra: 'extra'
182+
},
183+
actions: {
184+
add: (num, { state }) => {
185+
return { count: state.count + num }
186+
},
187+
asyncAdd: async (num, { state }) => {
188+
await timeout(300, {})
189+
return { count: state.count + num }
190+
},
191+
produceAdd: (num) => {
192+
return (state) => {
193+
state.count += num
194+
}
195+
},
196+
asyncProduceAdd: async (num) => {
197+
await timeout(300, {})
198+
return (state) => {
199+
state.count += num
200+
}
201+
},
202+
hocAdd: (num, { actions }) => {
203+
return actions.add(num)
204+
},
205+
asyncHocAdd: async (num, { actions }) => {
206+
await timeout(100, {})
207+
return actions.add(num)
208+
}
209+
}
210+
}
211+
178212
export const TimeoutCounter = Model(timeoutCounter)
179213

180214
export const ErrorCounter: ModelType<CounterState, CounterActionParams> = {
@@ -196,8 +230,8 @@ export const NextCounterModel: ModelType<
196230
NextCounterActionParams
197231
> = {
198232
actions: {
199-
add: num => {
200-
return state => {
233+
add: (num) => {
234+
return (state) => {
201235
state.count += num
202236
}
203237
},
@@ -235,7 +269,10 @@ Array.from(Array(20000).keys()).forEach((_, idx) => {
235269
})
236270
console.timeEnd('create data')
237271

238-
export const ExpensiveModel: ModelType<ExpensiveState, ExpensiveActionParams> = {
272+
export const ExpensiveModel: ModelType<
273+
ExpensiveState,
274+
ExpensiveActionParams
275+
> = {
239276
state: {
240277
moduleList: []
241278
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"module": "./dist/react-model.esm.js",

src/helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const consumerAction = (
3535
params,
3636
type: 'outer'
3737
}
38-
await applyMiddlewares(actionMiddlewares, context)
38+
return await applyMiddlewares(actionMiddlewares, context)
3939
}
4040

4141
const consumerActions = (

src/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type Context<S = {}> = InnerContext<S> & {
9696
modelMiddlewares?: Middleware[]
9797
}
9898

99-
type Middleware<S = {}> = (C: Context<S>, M: Middleware<S>[]) => void
99+
type Middleware<S = {}> = (C: Context<S>, M: Middleware<S>[]) => Promise<void>
100100

101101
type MiddlewareConfig = {
102102
logger: {

src/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ const getActions = (
211211
Global
212212
}
213213
if (Global.Middlewares[modelName]) {
214-
await applyMiddlewares(Global.Middlewares[modelName], context)
214+
return await applyMiddlewares(Global.Middlewares[modelName], context)
215215
} else {
216-
await applyMiddlewares(actionMiddlewares, context)
216+
return await applyMiddlewares(actionMiddlewares, context)
217217
}
218218
})
219219
)

0 commit comments

Comments
 (0)