Skip to content

Commit

Permalink
Release 1.3.5 (#98)
Browse files Browse the repository at this point in the history
* docs: model define

* chore: typo

* fix: utility Type  ExtractIModelDispatcher*

* chore: new version

* chore: lint

* chore: typo
  • Loading branch information
alvinhui authored Apr 11, 2020
1 parent 8833e97 commit b08f358
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function Button() {
}
```

## Get the latest sate of the model
## Get the latest state of the model

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

Expand Down
34 changes: 26 additions & 8 deletions docs/upgrade-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,46 @@ We will remove the deprecated API in future versions.
#### 1.0.0

```js
const todos = {
const counter = {
state: {
value: 0,
},
actions: {
increment:(prevState) => prevState + 1,
async incrementAsync(state, payload, actions) {
increment:(state) => ({ value: state.value + 1 }),
async incrementAsync(state, payload, actions, globalActions) {
console.log(state); // 0
await delay(1000);
actions.increment();
}
globalActions.todo.refresh();
},
async decrementAsync(state) {
await delay(1000);
return { value: state.value - 1 };
},
}
}
```

#### 1.3.0

```js
const todos = {
const counter = {
state: {
value: 0,
},
reducers: {
increment:(prevState) => prevState + 1,
increment:(prevState) => ({ value: prevState.value + 1 }),
},
effects: () => ({
async incrementAsync() {
effects: (dispatch) => ({
async incrementAsync(payload, rootState) {
console.log(rootState.counter); // 0
await delay(1000);
this.increment();
dispatch.todo.refresh();
},
async decrementAsync(payload, rootState) {
await delay(1000);
this.setState({ value: rootState.counter.value - 1 }); // setState is a built-in reducer
},
}),
}
Expand Down
34 changes: 26 additions & 8 deletions docs/upgrade-guidelines.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,46 @@ export default withModelDispatchers('todos')(TodoList);
#### 1.0.0

```js
const todos = {
const counter = {
state: {
value: 0,
},
actions: {
increment:(prevState) => prevState + 1,
async incrementAsync(state, payload, actions) {
increment:(state) => ({ value: state.value + 1 }),
async incrementAsync(state, payload, actions, globalActions) {
console.log(state); // 0
await delay(1000);
actions.increment();
}
globalActions.todo.refresh();
},
async decrementAsync(state) {
await delay(1000);
return { value: state.value - 1 };
},
}
}
```

#### 1.3.0

```js
const todos = {
const counter = {
state: {
value: 0,
},
reducers: {
increment:(prevState) => prevState + 1,
increment:(prevState) => ({ value: prevState.value + 1 }),
},
effects: () => ({
async incrementAsync() {
effects: (dispatch) => ({
async incrementAsync(payload, rootState) {
console.log(rootState.counter); // 0
await delay(1000);
this.increment();
dispatch.todo.refresh();
},
async decrementAsync(payload, rootState) {
await delay(1000);
this.setState({ value: rootState.counter.value - 1 }); // setState 是一个内置的 reducer
},
}),
}
Expand Down
2 changes: 2 additions & 0 deletions examples/todos/src/components/Todos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function Todos() {

useEffect(() => {
refresh();

// eslint-disable-next-line
}, []);

const noTaskView = <div>no task</div>;
Expand Down
2 changes: 2 additions & 0 deletions examples/todos/src/components/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default function UserApp() {

useEffect(() => {
login();

// eslint-disable-next-line
}, []);

console.debug('UserApp rending...');
Expand Down
5 changes: 3 additions & 2 deletions examples/todos/src/models/todos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { delay } from '../utils';
import store, { RootDispatch } from '../store';
import store, { RootDispatch, RootState } from '../store';

export interface Todo {
name: string;
Expand Down Expand Up @@ -32,7 +32,8 @@ const model = {
},
effects: (dispatch: RootDispatch) => ({
// this will run after "add" reducer finished
add(todo: Todo) {
add(todo: Todo, rootState: RootState) {
console.log(rootState.todos);
dispatch.user.setTodos(store.getModelState('todos').dataSource.length);
},
async refresh() {
Expand Down
2 changes: 1 addition & 1 deletion examples/todos/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default store;
export type Models = typeof models;
export type Store = typeof store;
export type RootDispatch = IcestoreDispatch<Models>;
export type iRootState = IcestoreRootState<Models>;
export type RootState = IcestoreRootState<Models>;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/store",
"version": "1.3.4",
"version": "1.3.5",
"description": "Simple and friendly state for React",
"main": "lib/index.js",
"files": [
Expand Down Expand Up @@ -64,4 +64,4 @@
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
}
}
}
12 changes: 7 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ type ExtractIModelDispatcherAsyncFromEffect<
? IcestoreDispatcherAsync<void, void, R>
: E extends (payload: infer P) => Promise<infer R>
? IcestoreDispatcherAsync<P, void, R>
: E extends (payload: infer P, meta: infer M) => Promise<infer R>
? IcestoreDispatcherAsync<P, M, R>
: IcestoreDispatcherAsync<any, any, any>
: E extends (payload: infer P, rootState: any) => Promise<infer R>
? IcestoreDispatcherAsync<P, void, R>
: E extends (payload: infer P, rootState: any, meta: infer M) => Promise<infer R>
? IcestoreDispatcherAsync<P, M, R>
: IcestoreDispatcherAsync<any, any, any>

type ExtractIModelDispatchersFromEffectsObject<
effects extends ModelEffects<any>
Expand All @@ -64,9 +66,9 @@ type ExtractIModelDispatcherFromReducer<R> = R extends () => any
? IcestoreDispatcher<void, void>
: R extends (state: infer S) => infer S
? IcestoreDispatcher<void, void>
: R extends (state: infer S, payload: infer P) => infer S
: R extends (state: infer S, payload: infer P) => (infer S | void)
? IcestoreDispatcher<P, void>
: R extends (state: infer S, payload: infer P, meta: infer M) => infer S
: R extends (state: infer S, payload: infer P, meta: infer M) => (infer S | void)
? IcestoreDispatcher<P, M>
: IcestoreDispatcher<any, any>

Expand Down

0 comments on commit b08f358

Please sign in to comment.