Skip to content

Commit 17c609a

Browse files
Merge branch 'master' into v2
2 parents 0ea86fd + dc3625b commit 17c609a

File tree

7 files changed

+106
-206
lines changed

7 files changed

+106
-206
lines changed

README.md

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const Todo = {
2727
// s is the readonly version of state
2828
// you can also return partial state here but don't need to keep immutable manually
2929
// state is the mutable state
30-
return state => state.items.push(todo)
30+
return state => {
31+
state.items.push(todo)
32+
}
3133
}
3234
}
3335
}
@@ -56,6 +58,8 @@ const TodoList = () => {
5658

5759
[Next.js + react-model work around](https://github.com/byte-fe/react-model-experiment)
5860

61+
[v2 docs](https://github.com/byte-fe/react-model/blob/v2/README.md)
62+
5963
install package
6064

6165
```shell
@@ -83,85 +87,15 @@ npm install react-model
8387
- [How can I add custom middleware](#how-can-i-add-custom-middleware)
8488
- [How can I make persist models](#how-can-i-make-persist-models)
8589
- [How can I deal with local state](#how-can-i-deal-with-local-state)
86-
- [actions throw error from immer.module.js](#actions-throw-error-from-immer.module.js)
87-
- [How can I customize each model's middlewares?](#how-can-i-customize-each-model's-middlewares)
90+
- [actions throw error from immer.module.js](#actions-throw-error-from-immermodulejs)
91+
- [How can I customize each model's middlewares?](#how-can-i-customize-each-models-middlewares)
8892

8993
## Core Concept
9094

9195
### Model
9296

9397
Every model have their own state and actions.
9498

95-
<details>
96-
<summary><del>old model</del> (will be deprecated in v3.0)</summary>
97-
<p>
98-
99-
```typescript
100-
const initialState = {
101-
counter: 0,
102-
light: false,
103-
response: {}
104-
}
105-
106-
interface StateType = {
107-
counter: number
108-
light: boolean
109-
response: {
110-
code?: number
111-
message?: string
112-
}
113-
}
114-
115-
interface ActionsParamType = {
116-
increment: number
117-
openLight: undefined
118-
get: undefined
119-
} // You only need to tag the type of params here !
120-
121-
const model: ModelType<StateType, ActionsParamType> = {
122-
actions: {
123-
increment: async (state, _, params) => {
124-
return {
125-
counter: state.counter + (params || 1)
126-
}
127-
},
128-
openLight: async (state, actions) => {
129-
await actions.increment(1) // You can use other actions within the model
130-
await actions.get() // support async functions (block actions)
131-
actions.get()
132-
await actions.increment(1) // + 1
133-
await actions.increment(1) // + 2
134-
await actions.increment(1) // + 3 as expected !
135-
return { light: !state.light }
136-
},
137-
get: async () => {
138-
await new Promise((resolve, reject) =>
139-
setTimeout(() => {
140-
resolve()
141-
}, 3000)
142-
)
143-
return {
144-
response: {
145-
code: 200,
146-
message: `${new Date().toLocaleString()} open light success`
147-
}
148-
}
149-
}
150-
},
151-
state: initialState
152-
}
153-
154-
export default model
155-
156-
// You can use these types when use Class Components.
157-
// type ConsumerActionsType = getConsumerActionsType<typeof Model.actions>
158-
// type ConsumerType = { actions: ConsumerActionsType; state: StateType }
159-
// type ActionType = ConsumerActionsType
160-
// export { ConsumerType, StateType, ActionType }
161-
```
162-
</p>
163-
</details>
164-
16599
```typescript
166100
const initialState = {
167101
counter: 0,

__test__/actions/unmount.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="../index.d.ts" />
2+
import 'react-testing-library/cleanup-after-each'
3+
import { testHook } from 'react-hooks-testing-library'
4+
import { ActionsTester } from '../index'
5+
import { Model } from '../../src'
6+
7+
describe('actions', () => {
8+
test('call actions in action', async () => {
9+
const { useStore } = Model({ ActionsTester })
10+
let state: any
11+
let actions: any
12+
const { unmount } = testHook(() => {
13+
;[state, actions] = useStore('ActionsTester')
14+
})
15+
await actions.getData()
16+
unmount()
17+
expect(state.data).toEqual({ counter: 1000 })
18+
})
19+
})

__test__/index.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import { timeout } from '../src/helper'
55
import { actionMiddlewares } from '../src/middlewares'
66

77
export const ActionsTester: ModelType<ActionTesterState, ActionTesterParams> = {
8-
state: {
9-
response: {
10-
data: {}
11-
},
12-
data: {}
13-
},
148
actions: {
159
get: async () => {
1610
const response = await timeout(9, { code: 0, data: { counter: 1000 } })
1711
return { response }
1812
},
13+
getData: async (_, { actions }) => {
14+
await actions.get()
15+
actions.parse()
16+
},
1917
parse: () => {
2018
return state => {
2119
state.data = state.response.data
2220
}
23-
},
24-
getData: async (_, actions) => {
25-
await actions.get()
26-
actions.parse()
21+
}
22+
},
23+
state: {
24+
data: {},
25+
response: {
26+
data: {}
2727
}
2828
}
2929
}
@@ -32,113 +32,113 @@ export const Counter: ModelType<
3232
CounterState,
3333
CounterActionParams & ExtraActionParams
3434
> = {
35-
state: { count: 0 },
3635
actions: {
37-
increment: (_, __, params) => {
38-
return state => {
39-
state.count += params
40-
}
41-
},
42-
add: (state, __, params) => {
36+
add: (params, { state }) => {
4337
return {
4438
count: state.count + params
4539
}
4640
},
4741
addCaller: (_, actions) => {
4842
actions.add(5)
43+
},
44+
increment: params => {
45+
return state => {
46+
state.count += params
47+
}
4948
}
50-
}
49+
},
50+
state: { count: 0 }
5151
}
5252

5353
// v3.0
54-
export const NextCounter: NextModelType<
54+
export const NextCounter: ModelType<
5555
CounterState,
5656
CounterActionParams & ExtraActionParams
5757
> = {
58-
state: { count: 0 },
5958
actions: {
60-
increment: params => {
61-
return state => {
62-
state.count += params
63-
}
64-
},
6559
add: (params, { state }) => {
6660
return {
6761
count: state.count + params
6862
}
6963
},
7064
addCaller: (_, { actions }) => {
7165
actions.add(5)
66+
},
67+
increment: params => {
68+
return state => {
69+
state.count += params
70+
}
7271
}
73-
}
72+
},
73+
state: { count: 0 }
7474
}
7575

7676
export const Theme: ModelType<ThemeState, ThemeActionParams> = {
77-
state: {
78-
theme: 'dark'
79-
},
8077
actions: {
81-
changeTheme: state => ({
78+
changeTheme: (_, { state }) => ({
8279
theme: state.theme === 'dark' ? 'light' : 'dark'
8380
})
81+
},
82+
state: {
83+
theme: 'dark'
8484
}
8585
}
8686

8787
export const AsyncCounter: ModelType<CounterState, CounterActionParams> = {
88-
state: { count: 0 },
89-
asyncState: async (context: { count?: number }) => ({
90-
count: context ? context.count || 1 : 1
91-
}),
9288
actions: {
93-
increment: (_, __, params) => {
89+
increment: params => {
9490
return state => {
9591
state.count += params
9692
}
9793
}
98-
}
94+
},
95+
asyncState: async (context: { count?: number }) => ({
96+
count: context ? context.count || 1 : 1
97+
}),
98+
state: { count: 0 }
9999
}
100100

101101
export const AsyncNull: ModelType<CounterState, CounterActionParams> = {
102-
state: { count: 0 },
103102
actions: {
104-
increment: (_, __, params) => {
103+
increment: params => {
105104
return state => {
106105
state.count += params
107106
}
108107
}
109-
}
108+
},
109+
state: { count: 0 }
110110
}
111111

112112
export const TimeoutCounter: ModelType<CounterState, CounterActionParams> = {
113-
state: { count: 0 },
114-
asyncState: async () => ({
115-
count: 1
116-
}),
117113
actions: {
118-
increment: async (_, __, params) => {
114+
increment: async (params, { state: _ }) => {
119115
await timeout(4000, {})
120116
return (state: typeof _) => {
121117
state.count += params
122118
}
123119
}
124-
}
120+
},
121+
asyncState: async () => ({
122+
count: 1
123+
}),
124+
state: { count: 0 }
125125
}
126126

127127
export const ErrorCounter: ModelType<CounterState, CounterActionParams> = {
128-
state: { count: 0 },
129128
actions: {
130129
increment: async () => {
131130
throw 'error'
132131
}
133-
}
132+
},
133+
state: { count: 0 }
134134
}
135135

136136
const delayMiddleware: Middleware = async (context, restMiddlewares) => {
137137
await timeout(2000, {})
138138
context.next(restMiddlewares)
139139
}
140140

141-
const nextCounterModel: NextModelType<CounterState, NextCounterActionParams> = {
141+
const nextCounterModel: ModelType<CounterState, NextCounterActionParams> = {
142142
actions: {
143143
add: num => {
144144
return state => {

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-model",
3-
"version": "2.7.1",
3+
"version": "3.0.0-alpha.1",
44
"description": "The State management library for React",
55
"main": "./dist/react-model.js",
66
"umd:main": "./dist/react-model.umd.js",
@@ -17,7 +17,7 @@
1717
"author": "ArrayZoneYour <hustliyidong@gmail.com>",
1818
"license": "MIT",
1919
"dependencies": {
20-
"immer": "^2.0.0"
20+
"immer": "^3.0.0"
2121
},
2222
"peerDependencies": {
2323
"react": "^16.3.0",
@@ -26,8 +26,10 @@
2626
"devDependencies": {
2727
"@commitlint/cli": "^7.5.2",
2828
"@commitlint/config-conventional": "^7.5.0",
29+
"@types/babel__core": "^7.1.1",
30+
"@types/babel__template": "^7.0.2",
2931
"@types/jest": "^24.0.5",
30-
"@types/node": "^11.9.4",
32+
"@types/node": "^12.0.0",
3133
"@types/react": "^16.8.2",
3234
"@types/react-dom": "^16.8.0",
3335
"commitizen": "^3.0.7",
@@ -37,8 +39,9 @@
3739
"prettier": "^1.16.4",
3840
"react": "^16.8.4",
3941
"react-dom": "^16.8.4",
40-
"react-hooks-testing-library": "^0.4.0",
41-
"react-testing-library": "^6.0.0",
42+
"react-hooks-testing-library": "^0.5.0",
43+
"react-test-renderer": "^16.8.6",
44+
"react-testing-library": "^7.0.0",
4245
"remark-cli": "^6.0.1",
4346
"remark-lint": "^6.0.4",
4447
"remark-preset-lint-recommended": "^3.0.2",

0 commit comments

Comments
 (0)