other api

center

(action,centerUtils) => void

mutationcenters中会用到 center 方法。

const mutation = {
  namespace: 'test',
  initialState: null,
  centers: {
    centerOne(action, centerUtils) {},
    centerTwo(action, centerUtils) {},
  },
};

参数

  1. action (object)

    dispatch(action)中的action,详细可看redux actions

  2. 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,是为了兼容 dvaredux-sagaselect

      如果你没用过这个,可以直接使用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 版的 setTimeoutresovle(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,兼容 dvaredux-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 一样。

    返回值

    不需要返回值。

    configCreateStoreoptions 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);

    参数

    1. originalCenter (function)

      (action, centerUtils) => any

      dispatch 或者 put 命中的 center

    2. centerUtils (object)

      请参考上面 center 的参数 centerUtils

    3. currentMutation (object)

      originalCenter 所属的mutation。

    4. 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 的效果是一样的。

    参数

    1. originalReducer (function)

      (state, action) => state

      即将运行的 reducerredux 对内只有一个 reducercomposeReducers 会返回一个reducer)。

    返回值

    返回 reducer 函数。

Last updated

Was this helpful?