Skip to content

Commit

Permalink
docs: add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Dec 26, 2019
1 parent 82a4e0f commit da666c4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
6 changes: 5 additions & 1 deletion demo/store/mpstore.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,11 @@ function () {
this.state = deepFreeze(mergeModule(this.state, partialState));
}

asyncUpdate(this, 'restoreCallbacks', callback);
asyncUpdate(this, 'restoreCallbacks', function () {
if (typeof callback === 'function') {
callback(partialState);
}
});
}
}, {
key: "forceUpdate",
Expand Down
6 changes: 5 additions & 1 deletion dist/mpstore.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,11 @@ function () {
this.state = deepFreeze(mergeModule(this.state, partialState));
}

asyncUpdate(this, 'restoreCallbacks', callback);
asyncUpdate(this, 'restoreCallbacks', function () {
if (typeof callback === 'function') {
callback(partialState);
}
});
}
}, {
key: "forceUpdate",
Expand Down
6 changes: 5 additions & 1 deletion dist/mpstore.es6m.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,11 @@ class Store {
} else {
this.state = deepFreeze(mergeModule(this.state, partialState));
}
asyncUpdate(this, 'restoreCallbacks', callback);
asyncUpdate(this, 'restoreCallbacks', () => {
if (typeof callback === 'function') {
callback(partialState);
}
});
}
forceUpdate () {
asyncUpdate(this, null, COMMONACTION);
Expand Down
6 changes: 5 additions & 1 deletion dist/mpstore.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,11 @@ function () {
this.state = deepFreeze(mergeModule(this.state, partialState));
}

asyncUpdate(this, 'restoreCallbacks', callback);
asyncUpdate(this, 'restoreCallbacks', function () {
if (typeof callback === 'function') {
callback(partialState);
}
});
}
}, {
key: "forceUpdate",
Expand Down
2 changes: 1 addition & 1 deletion dist/mpstore.min.js

Large diffs are not rendered by default.

63 changes: 61 additions & 2 deletions docs/store.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ const store = createStore(() => {}, {})
### API
+ `store.add`
+ `store.dispatch`
+ `store.setNamespace`
+ `store.restore`
+ `store.forceUpdate`
+ `store.use`
+ `store.setNamespace`
+ `store.getModule`
+ `store.addModule`

Expand Down Expand Up @@ -52,7 +54,7 @@ const store = createStore(() => {}, {})
console.log(store.state.name) // 'imtaotao'
```

#### dispatch(action: string | symbol, payload: any, callback?: () => void) : void
#### dispatch(action: string | symbol, payload: any, callback?: destPayload => void) : void
`dispatch` 方法用于触发一个 `action`,他会调用所有的中间件,最后在调用 `reducer``setter` 函数,你将不能在 `setter` 函数中调用 `dispatch` 方法,以下的写法将会报错。这样做的愿意是为了避免逻辑过于混乱,但你可以在中间件中调用 `dispatch`
```js
store.add('action', {
Expand All @@ -68,6 +70,42 @@ const store = createStore(() => {}, {})
store.dispatch('action', 'imtaotao')
```

`dispatch` 这个行为触发时,对 store 的状态更改是同步的,对视图的更新是异步的。mpStore 对视图的更新是批量的
```js
store.add('action', {
partialState: {
name: 'chen',
},
setter (state, payload) {
return { name: payload }
},
})

Page({
storeConfig: {
useState (store) {
return ({
name: state => state.name,
})
},
},

onLoad() {
this.data.global.name // 'chen'
this.store.dispatch('action', 'tao', () => {
// 或者在回调里面拿到最新的值
this.data.global.name // 'tao'
})
this.data.global.name // 'chen',store 的更新是同步的
this.store.state.name // 'tao'
// 由于视图的更新是异步的,所以可以这样
setTimeout(() => {
this.data.global.name // 'chen'
})
}
})
```

`callback` 方法会在 `store.state` 改变后,所有依赖的组件更新后(包括视图)调用,因为**中间件中可能会处理异步行为**,所以这个 `callback` 的存在是必要的
```js
store.add('action', {
Expand All @@ -92,6 +130,27 @@ const store = createStore(() => {}, {})
console.log(store.state.name) // 'tao'
```

#### restore(action: string | symbol, callback: initState => void) : void
`restore` 会将定义的 reducer 恢复到初始状态。通常的情况下,我们需要在组件卸载时,及时清空相应的状态,为此而定义对应的 reducer,这一步完全可以由 mpStore 来做,所以这也是 `restore` 这个 api 出现的原因。`restore` 恢复为初始状态时,也会去异步更新依赖的组件,第二个参数与 `dispatch` 方法中的 callback 基本一致,除了接受的参数为初始状态之外,用法如下:
```js
store.add('action', {
partialState: { name: 'tao' },
setter (state, payload) {
return { name: payload }
}
})
store.dispatch('action', 'chen', () => {
store.state.name // 'chen'
store.restore('action', initState => {
initState // { name: 'tao' }
store.state.name // 'tao'
})
})
```

#### forceUpdate() : void
`forceUpdate` 会强制所有依赖的组件走一遍 diff -> patch 的过程,从而更新视图,当你用的这个方法时,99% 都是你自己的代码写的有问题。

#### setNamespace(namespace: string) : void
store 默认会在组件的 data 中添加 `global` 来接受用到的全局状态,如果需要更改,可以通过此方法。需要注意的是你必须在 `App` 初始化之前调用
```js
Expand Down
6 changes: 5 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ export class Store {
}

// update components
asyncUpdate(this, 'restoreCallbacks', callback)
asyncUpdate(this, 'restoreCallbacks', () => {
if (typeof callback === 'function') {
callback(partialState)
}
})
}

forceUpdate () {
Expand Down
3 changes: 2 additions & 1 deletion test/script/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ describe('Update', () => {
expect(store.state.a.b.name).toBe('imtaotao')
expect(cm.dom.textContent).toBe('imtaotao')
expect(isError(() => store.restore())).toBeTrue()
store.restore('action', () => {
store.restore('action', initState => {
expect(store.state.a.b.name).toBe('tao')
expect(cm.dom.textContent).toBe('tao')
expect(initState).toEqual({name: 'tao'})
done()
})
expect(store.state.a.b.name).toBe('tao')
Expand Down

0 comments on commit da666c4

Please sign in to comment.