other api
center
(action,centerUtils) => voidmutation的centers中会用到 center 方法。
const mutation = {
namespace: 'test',
initialState: null,
centers: {
centerOne(action, centerUtils) {},
centerTwo(action, centerUtils) {},
},
};参数
action(object)dispatch(action)中的action,详细可看redux actions。centerUtils(object)centerUtils包括下面的属性,下面的代码使用了jest的测试代码。const { put, select, delay, call, dispatch, getState } = centerUtils;put(action,namepace)promise版的dispatch,同时结合mutation做了一些处理,在当前mutation使用put进行内部交流可以省略namespace。const mutations = [ { namespace: 'other-mutation', initialState: null, centers: { async centerThree() {}, }, }, { namespace: 'current-mutation', initialState: null, centers: { async centerOne(action, { put }) { // 在当前mutation 使用put 进行内部交流可以省略 namespace。 //await put({ type: 'centerTwo' },'current-mutation'); //await put({ type: 'current-mutation/centerTwo' }),这个会报warning await put({ type: 'centerTwo' }); // 在非当前mutation 使用put,需要加上namespace。 await put({ type: 'centerThree' }, 'other-mutation'); //或者这样await put({ type: 'other-mutation/centerThree' }); }, centerTwo(action, centerUtils) {}, }, }, ];select(selector)promise版的getState, 同时支持selector,是为了兼容dva中redux-saga的select。如果你没用过这个,可以直接使用
getState代替。const mutation = { namespace: 'test', initialState: null, centers: { async centerOne(action, { select, getState }) { const state = await select(); const test = await select(state => test); const stateForGetState = getState(); expect(stateForGetState).toEqual(state); expect(test).toEqual(state.test); }, }, };delay(ms)promise版的setTimeout,resovle(ms)。const mutation = { namespace: 'test', initialState: null, centers: { async centerOne(action, { delay }) { const ms = await delay(1000); expect(ms).toEqual(1000); }, }, };call(fn, ...args)异步版的
call,兼容dva中redux-sagacall用法。如果你用过这个可以忽略。const mutation = { namespace: 'test', initialState: null, centers: { async centerOne(action, { call }) { await call(fetch, '/data.json'); //等价于 fetch('/data.json') }, }, };dispatch(action)跟 redux dispatch 一样。
getState()跟 redux getState 一样。
返回值
不需要返回值。
在
configCreateStore的optionsshouldRunReducer = false请不要返回true,否则会导致reudcer不运行。centerEnhancer
(originalCenter, centerUtils, currentMutation, actionType) => (...args) => originalCenter(...args);centerEnhancer是在applayPluin中使用,centerEnhancer只要在命中的情况下运行。参数跟dva中的onEffect的效果是一样的。const pluginOne = { centerEnhancer: function(originalCenter, centerUtils, currentMutation, actionType) { return async (...args) => { return originalCenter(...args); }; }, }; const store = createMutationStore(applyPlugin(pluginOne))(mutations);参数
originalCenter(function)(action, centerUtils) => anydispatch或者put命中的center。centerUtils(object)请参考上面
center的参数centerUtils。currentMutation(object)originalCenter所属的mutation。actionType(string)action对象的type属性,在redux-mutation中只可以是字符串。const action = { type: 'test' }; const actionType = action.type;
返回值
返回
center函数。reducerEnhancer
originalReducer => (...args) => originalReducer(...args);reducerEnhancer是在applayPluin中使用,只要dispatch就会触发reducerEnhancer。参数跟dva中的onReducer的效果是一样的。参数
originalReducer(function)(state, action) => state即将运行的
reducer,redux对内只有一个reducer(composeReducers会返回一个reducer)。
返回值
返回
reducer函数。
Last updated
Was this helpful?