dva初探

2020-02-09  本文已影响0人  别过经年

dva@2.4.1
getSaga.js

  function* sagaWithCatch(...args) {
    const { __dva_resolve: resolve = noop, __dva_reject: reject = noop } =
      args.length > 0 ? args[0] : {};
    try {
      yield sagaEffects.put({ type: `${key}${NAMESPACE_SEP}@@start` });
      const ret = yield effect(...args.concat(createEffects(model)));//effect就是model中定义的effect,在此执行
      yield sagaEffects.put({ type: `${key}${NAMESPACE_SEP}@@end` });
      resolve(ret);
    } catch (e) {
      onError(e, {
        key,
        effectArgs: args,
      });
      if (!e._dontReject) {
        reject(e);
      }
    }
  }

dva-loading@3.0.14


  function onEffect(effect, { put }, model, actionType) {
    const { namespace } = model;
    if (
      (only.length === 0 && except.length === 0) ||
      (only.length > 0 && only.indexOf(actionType) !== -1) ||
      (except.length > 0 && except.indexOf(actionType) === -1)
    ) {
      return function*(...args) {
        yield put({ type: SHOW, payload: { namespace, actionType } });// 触发显示
        yield effect(...args);// 执行dva-core getSaga.js sagaWithCatch函数
        yield put({ type: HIDE, payload: { namespace, actionType } });// 触发隐藏
      };
    } else {
      return effect;
    }
  }

业务代码:

    *doDeleteAppBatch({ payload: { appIds, projectId } }, { call, put }) {
      yield call(doDeleteApp, appIds);
      yield put({ type: "doGetAppList", payload: projectId });// 删除后获取list
    },
    *doGetAppList({ payload }, { call, put }) {
      const  data = yield call(doGetAppList);
      yield put({ type: "setAppList", payload: data });
    },

因为在doDeleteAppBatch中会调用代码doGetAppList effect,本以为doDeleteAppBatch导致的loading会包括doGetAppList的调用时间,debug后发现其实不是这样子,doDeleteAppBatch执行完后,会触发hide,调用yield put({ type: "doGetAppList", payload: projectId });会触发新的show,并不是因为doDeleteAppBatch调用doGetAppList完成(这里指调用接口完成)后才关闭doDeleteAppBatch的loading。

上一篇下一篇

猜你喜欢

热点阅读