Skip to content

Commit b08f358

Browse files
authored
Release 1.3.5 (#98)
* docs: model define * chore: typo * fix: utility Type ExtractIModelDispatcher* * chore: new version * chore: lint * chore: typo
1 parent 8833e97 commit b08f358

File tree

9 files changed

+70
-27
lines changed

9 files changed

+70
-27
lines changed

docs/recipes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function Button() {
9292
}
9393
```
9494

95-
## Get the latest sate of the model
95+
## Get the latest state of the model
9696

9797
In some scenarios, you may want to get the latest state of the model.
9898

docs/upgrade-guidelines.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,28 +133,46 @@ We will remove the deprecated API in future versions.
133133
#### 1.0.0
134134

135135
```js
136-
const todos = {
136+
const counter = {
137+
state: {
138+
value: 0,
139+
},
137140
actions: {
138-
increment:(prevState) => prevState + 1,
139-
async incrementAsync(state, payload, actions) {
141+
increment:(state) => ({ value: state.value + 1 }),
142+
async incrementAsync(state, payload, actions, globalActions) {
143+
console.log(state); // 0
140144
await delay(1000);
141145
actions.increment();
142-
}
146+
globalActions.todo.refresh();
147+
},
148+
async decrementAsync(state) {
149+
await delay(1000);
150+
return { value: state.value - 1 };
151+
},
143152
}
144153
}
145154
```
146155

147156
#### 1.3.0
148157

149158
```js
150-
const todos = {
159+
const counter = {
160+
state: {
161+
value: 0,
162+
},
151163
reducers: {
152-
increment:(prevState) => prevState + 1,
164+
increment:(prevState) => ({ value: prevState.value + 1 }),
153165
},
154-
effects: () => ({
155-
async incrementAsync() {
166+
effects: (dispatch) => ({
167+
async incrementAsync(payload, rootState) {
168+
console.log(rootState.counter); // 0
156169
await delay(1000);
157170
this.increment();
171+
dispatch.todo.refresh();
172+
},
173+
async decrementAsync(payload, rootState) {
174+
await delay(1000);
175+
this.setState({ value: rootState.counter.value - 1 }); // setState is a built-in reducer
158176
},
159177
}),
160178
}

docs/upgrade-guidelines.zh-CN.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,46 @@ export default withModelDispatchers('todos')(TodoList);
137137
#### 1.0.0
138138

139139
```js
140-
const todos = {
140+
const counter = {
141+
state: {
142+
value: 0,
143+
},
141144
actions: {
142-
increment:(prevState) => prevState + 1,
143-
async incrementAsync(state, payload, actions) {
145+
increment:(state) => ({ value: state.value + 1 }),
146+
async incrementAsync(state, payload, actions, globalActions) {
147+
console.log(state); // 0
144148
await delay(1000);
145149
actions.increment();
146-
}
150+
globalActions.todo.refresh();
151+
},
152+
async decrementAsync(state) {
153+
await delay(1000);
154+
return { value: state.value - 1 };
155+
},
147156
}
148157
}
149158
```
150159

151160
#### 1.3.0
152161

153162
```js
154-
const todos = {
163+
const counter = {
164+
state: {
165+
value: 0,
166+
},
155167
reducers: {
156-
increment:(prevState) => prevState + 1,
168+
increment:(prevState) => ({ value: prevState.value + 1 }),
157169
},
158-
effects: () => ({
159-
async incrementAsync() {
170+
effects: (dispatch) => ({
171+
async incrementAsync(payload, rootState) {
172+
console.log(rootState.counter); // 0
160173
await delay(1000);
161174
this.increment();
175+
dispatch.todo.refresh();
176+
},
177+
async decrementAsync(payload, rootState) {
178+
await delay(1000);
179+
this.setState({ value: rootState.counter.value - 1 }); // setState 是一个内置的 reducer
162180
},
163181
}),
164182
}

examples/todos/src/components/Todos.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default function Todos() {
1515

1616
useEffect(() => {
1717
refresh();
18+
19+
// eslint-disable-next-line
1820
}, []);
1921

2022
const noTaskView = <div>no task</div>;

examples/todos/src/components/User.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default function UserApp() {
1111

1212
useEffect(() => {
1313
login();
14+
15+
// eslint-disable-next-line
1416
}, []);
1517

1618
console.debug('UserApp rending...');

examples/todos/src/models/todos.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { delay } from '../utils';
2-
import store, { RootDispatch } from '../store';
2+
import store, { RootDispatch, RootState } from '../store';
33

44
export interface Todo {
55
name: string;
@@ -32,7 +32,8 @@ const model = {
3232
},
3333
effects: (dispatch: RootDispatch) => ({
3434
// this will run after "add" reducer finished
35-
add(todo: Todo) {
35+
add(todo: Todo, rootState: RootState) {
36+
console.log(rootState.todos);
3637
dispatch.user.setTodos(store.getModelState('todos').dataSource.length);
3738
},
3839
async refresh() {

examples/todos/src/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default store;
88
export type Models = typeof models;
99
export type Store = typeof store;
1010
export type RootDispatch = IcestoreDispatch<Models>;
11-
export type iRootState = IcestoreRootState<Models>;
11+
export type RootState = IcestoreRootState<Models>;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ice/store",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "Simple and friendly state for React",
55
"main": "lib/index.js",
66
"files": [
@@ -64,4 +64,4 @@
6464
"redux": "^4.0.5",
6565
"redux-thunk": "^2.3.0"
6666
}
67-
}
67+
}

src/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ type ExtractIModelDispatcherAsyncFromEffect<
3737
? IcestoreDispatcherAsync<void, void, R>
3838
: E extends (payload: infer P) => Promise<infer R>
3939
? IcestoreDispatcherAsync<P, void, R>
40-
: E extends (payload: infer P, meta: infer M) => Promise<infer R>
41-
? IcestoreDispatcherAsync<P, M, R>
42-
: IcestoreDispatcherAsync<any, any, any>
40+
: E extends (payload: infer P, rootState: any) => Promise<infer R>
41+
? IcestoreDispatcherAsync<P, void, R>
42+
: E extends (payload: infer P, rootState: any, meta: infer M) => Promise<infer R>
43+
? IcestoreDispatcherAsync<P, M, R>
44+
: IcestoreDispatcherAsync<any, any, any>
4345

4446
type ExtractIModelDispatchersFromEffectsObject<
4547
effects extends ModelEffects<any>
@@ -64,9 +66,9 @@ type ExtractIModelDispatcherFromReducer<R> = R extends () => any
6466
? IcestoreDispatcher<void, void>
6567
: R extends (state: infer S) => infer S
6668
? IcestoreDispatcher<void, void>
67-
: R extends (state: infer S, payload: infer P) => infer S
69+
: R extends (state: infer S, payload: infer P) => (infer S | void)
6870
? IcestoreDispatcher<P, void>
69-
: R extends (state: infer S, payload: infer P, meta: infer M) => infer S
71+
: R extends (state: infer S, payload: infer P, meta: infer M) => (infer S | void)
7072
? IcestoreDispatcher<P, M>
7173
: IcestoreDispatcher<any, any>
7274

0 commit comments

Comments
 (0)