Skip to content

Commit 8836047

Browse files
committed
feat(v3): NextModelType => ModelType
1 parent 103b146 commit 8836047

File tree

6 files changed

+80
-201
lines changed

6 files changed

+80
-201
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__/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,107 +32,107 @@ 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
}
40+
},
41+
increment: params => {
42+
return state => {
43+
state.count += params
44+
}
4645
}
47-
}
46+
},
47+
state: { count: 0 }
4848
}
4949

5050
// v3.0
51-
export const NextCounter: NextModelType<
51+
export const NextCounter: ModelType<
5252
CounterState,
5353
CounterActionParams & ExtraActionParams
5454
> = {
55-
state: { count: 0 },
5655
actions: {
57-
increment: params => {
58-
return state => {
59-
state.count += params
60-
}
61-
},
6256
add: (params, { state }) => {
6357
return {
6458
count: state.count + params
6559
}
60+
},
61+
increment: params => {
62+
return state => {
63+
state.count += params
64+
}
6665
}
67-
}
66+
},
67+
state: { count: 0 }
6868
}
6969

7070
export const Theme: ModelType<ThemeState, ThemeActionParams> = {
71-
state: {
72-
theme: 'dark'
73-
},
7471
actions: {
75-
changeTheme: state => ({
72+
changeTheme: (_, { state }) => ({
7673
theme: state.theme === 'dark' ? 'light' : 'dark'
7774
})
75+
},
76+
state: {
77+
theme: 'dark'
7878
}
7979
}
8080

8181
export const AsyncCounter: ModelType<CounterState, CounterActionParams> = {
82-
state: { count: 0 },
83-
asyncState: async (context: { count?: number }) => ({
84-
count: context ? context.count || 1 : 1
85-
}),
8682
actions: {
87-
increment: (_, __, params) => {
83+
increment: params => {
8884
return state => {
8985
state.count += params
9086
}
9187
}
92-
}
88+
},
89+
asyncState: async (context: { count?: number }) => ({
90+
count: context ? context.count || 1 : 1
91+
}),
92+
state: { count: 0 }
9393
}
9494

9595
export const AsyncNull: ModelType<CounterState, CounterActionParams> = {
96-
state: { count: 0 },
9796
actions: {
98-
increment: (_, __, params) => {
97+
increment: params => {
9998
return state => {
10099
state.count += params
101100
}
102101
}
103-
}
102+
},
103+
state: { count: 0 }
104104
}
105105

106106
export const TimeoutCounter: ModelType<CounterState, CounterActionParams> = {
107-
state: { count: 0 },
108-
asyncState: async () => ({
109-
count: 1
110-
}),
111107
actions: {
112-
increment: async (_, __, params) => {
108+
increment: async (params, { state: _ }) => {
113109
await timeout(4000, {})
114110
return (state: typeof _) => {
115111
state.count += params
116112
}
117113
}
118-
}
114+
},
115+
asyncState: async () => ({
116+
count: 1
117+
}),
118+
state: { count: 0 }
119119
}
120120

121121
export const ErrorCounter: ModelType<CounterState, CounterActionParams> = {
122-
state: { count: 0 },
123122
actions: {
124123
increment: async () => {
125124
throw 'error'
126125
}
127-
}
126+
},
127+
state: { count: 0 }
128128
}
129129

130130
const delayMiddleware: Middleware = async (context, restMiddlewares) => {
131131
await timeout(2000, {})
132132
context.next(restMiddlewares)
133133
}
134134

135-
const nextCounterModel: NextModelType<CounterState, NextCounterActionParams> = {
135+
const nextCounterModel: ModelType<CounterState, NextCounterActionParams> = {
136136
actions: {
137137
add: num => {
138138
return state => {

package.json

Lines changed: 1 addition & 1 deletion
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",

0 commit comments

Comments
 (0)