Skip to content

Commit 67ffffc

Browse files
authored
Merge pull request #126 from byte-fe/develop
feat(model): remove v2 warning and support v3 schema as model
2 parents d4ce323 + aa33c1a commit 67ffffc

File tree

6 files changed

+47
-17
lines changed

6 files changed

+47
-17
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,14 @@ actions: {
822822
823823
### How can I customize each model's middlewares?
824824
825-
If you are using NextModel, you can customize each model's middlewares.
825+
You can customize each model's middlewares.
826826
827827
```typescript
828828
import { actionMiddlewares, Model } from 'react-model'
829-
import { delayMiddleware } from './middlewares'
829+
const delayMiddleware: Middleware = async (context, restMiddlewares) => {
830+
await timeout(1000, {})
831+
context.next(restMiddlewares)
832+
}
830833

831834
const nextCounterModel: NextModelType<CounterState, NextCounterActionParams> = {
832835
actions: {

__test__/Model/mixed.spec.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import { Model } from '../../src'
55

66
describe('useStore', () => {
77
test('create by single model definition', async () => {
8-
let state: any
9-
let actions: any
8+
let state: any, nextState: any
9+
let actions: any, nextActions: any
1010
let count = 0
11+
let nextCount = 0
1112
const Home = Model(NextCounter)
12-
const { useStore, subscribe, unsubscribe } = Model({ Home })
13+
const { useStore, subscribe, unsubscribe } = Model({ Home, NextCounter })
1314
renderHook(() => {
1415
;[state, actions] = useStore('Home')
16+
;[nextState, nextActions] = useStore('NextCounter')
1517
})
18+
19+
// Home
1620
expect(state).toEqual({ count: 0 })
1721
await actions.increment(3)
1822
expect(state).toEqual({ count: 3 })
@@ -26,5 +30,20 @@ describe('useStore', () => {
2630
await actions.increment(3)
2731
expect(state.count).toBe(10)
2832
expect(count).toBe(1)
33+
34+
// NextCounter
35+
expect(nextState).toEqual({ count: 0 })
36+
await nextActions.increment(3)
37+
expect(nextState).toEqual({ count: 3 })
38+
// test subscribe
39+
subscribe('NextCounter', 'increment', () => (nextCount += 1))
40+
await nextActions.increment(4)
41+
expect(nextCount).toBe(1)
42+
expect(nextState.count).toBe(7)
43+
// test unsubscribe
44+
unsubscribe('NextCounter', 'increment')
45+
await nextActions.increment(3)
46+
expect(nextState.count).toBe(10)
47+
expect(nextCount).toBe(1)
2948
})
3049
})

__test__/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const AsyncNull: ModelType<CounterState, CounterActionParams> = {
124124
state: { count: 0 }
125125
}
126126

127-
export const TimeoutCounter: ModelType<CounterState, CounterActionParams> = {
127+
const timeoutCounter: ModelType<CounterState, CounterActionParams> = {
128128
actions: {
129129
increment: async (params, { state: _ }) => {
130130
await timeout(4000, {})
@@ -139,6 +139,8 @@ export const TimeoutCounter: ModelType<CounterState, CounterActionParams> = {
139139
state: { count: 0 }
140140
}
141141

142+
export const TimeoutCounter = Model(timeoutCounter)
143+
142144
export const ErrorCounter: ModelType<CounterState, CounterActionParams> = {
143145
actions: {
144146
increment: async () => {
@@ -149,11 +151,14 @@ export const ErrorCounter: ModelType<CounterState, CounterActionParams> = {
149151
}
150152

151153
const delayMiddleware: Middleware = async (context, restMiddlewares) => {
152-
await timeout(2000, {})
154+
await timeout(1000, {})
153155
context.next(restMiddlewares)
154156
}
155157

156-
const nextCounterModel: ModelType<CounterState, NextCounterActionParams> = {
158+
export const NextCounterModel: ModelType<
159+
CounterState,
160+
NextCounterActionParams
161+
> = {
157162
actions: {
158163
add: num => {
159164
return state => {
@@ -170,5 +175,3 @@ const nextCounterModel: ModelType<CounterState, NextCounterActionParams> = {
170175
count: 0
171176
}
172177
}
173-
174-
export const NextCounterModel = Model(nextCounterModel)

__test__/middlewares/model.spec.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import { Model } from '../../src'
77

88
describe('NextModel', () => {
99
test("allows you to customize model's middleware", async () => {
10-
let actions: any
11-
let state: any
12-
const { useStore, getActions } = Model({ NextCounterModel })
10+
let actions: any, nextActions: any
11+
let state: any, nextState: any
12+
const WrapperModel = Model(NextCounterModel)
13+
const { useStore, getActions } = Model({ NextCounterModel, WrapperModel })
1314
const beginTime = Date.now()
1415
renderHook(() => {
1516
;[state, actions] = useStore('NextCounterModel')
17+
;[nextState, nextActions] = useStore('WrapperModel')
1618
})
1719
await actions.increment(2)
1820
await getActions('NextCounterModel').increment(1)
1921
expect(Date.now() - beginTime > 300)
2022
expect(state.count).toBe(3)
23+
await nextActions.increment(2)
24+
await getActions('WrapperModel').increment(1)
25+
expect(nextState.count).toBe(3)
2126
})
2227
})

package.json

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

src/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ function Model<M extends Models, MT extends ModelType, E>(
6565
extContext && (Global.Context['__global'] = extContext)
6666
Object.entries(models).forEach(([name, model]) => {
6767
if (!isAPI(model)) {
68-
console.warn(
69-
'we recommend you to use NextModel now, document link: https://github.com/byte-fe/react-model#model'
70-
)
7168
if (!Global.State[name]) {
7269
Global.State[name] = model.state
7370
}
71+
if (model.middlewares) {
72+
Global.Middlewares[name] = model.middlewares
73+
}
7474
Global.Actions[name] = model.actions
7575
Global.AsyncState[name] = model.asyncState
7676
} else {

0 commit comments

Comments
 (0)