other api
center
(action,centerUtils) => void
mutation
的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-saga
call
用法。如果你用过这个可以忽略。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
的options
shouldRunReducer = 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) => any
dispatch
或者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?