alexayan / dva-ssr

dva server side render (dva 服务器端渲染库)
30 stars 3 forks source link

block.check()是否存在count判断的问题 #1

Closed xhubert closed 7 years ago

xhubert commented 7 years ago

场景

  1. 对仅包括IndexPagedva项目进行服务端渲染改造,服务启动后,浏览器访问一直处于加载中。
  2. 测试项目的model中无effect,空项目。

pull了源码引用后,发现了一个问题:

// ../src/block.js
  check: () => {
    for (let [id, callback] of callbacks) {
      const count = container.get(id);
      if (count === 0) {
        container.delete(id);
        callback();
      }
    }
  },

这段代码,在执行的时候,count始终为undefined,所以导致callback()无法执行到。

alexayan commented 7 years ago

@idpocky 目前的实现上

onEffect: function (effect, { put }, model, actionType) {
      const temp = [];
      return function* (...args) {
        if (filter(args[0])) {
          block.lock(key);
        }
        yield effect(...args);
        if (filter(args[0])) {
          block.release(key);
        }
      }
    }

项目中需要有 effect, 配置中必须添加 asyncActions: ['topic/fetchTopic', ...], 名字取的有问题,列表中为 effect 的名字。

xhubert commented 7 years ago

刚才加了个effect测试了一下,还是老样子。

export default ssr.runtimeSSRMiddle({
  routes,
  createApp,
  initialState: {}, // custom initialState
  asyncActions: ['topic/fetchTopics'], // async actions in dva
  renderFullPage,
  onRenderSuccess,
});

我通过修改block.check()中针对count的判断后,可以正常显示页面。但是发现asyncActions中的action没有被执行啊。 是否dvamodel定义需要有什么特殊的写法?

alexayan commented 7 years ago

@idpocky

effects: {
    fetchTopic: [function* ({ payload: { refresh, init, lastTopic } }, { put, call, select }) {
      try {
        const topic = yield call(
            fetchTopics,
        );
        yield put({ type: 'saveTopic', payload: { topic } });
      } catch (e) {
        yield put({ type: 'fetchTopicFailed', payload: { error: e } });
      }
    }, { type: 'takeLatest' }]
}
xhubert commented 7 years ago

现在可以了,原来对应的effect同样是需要subscribe的。

alexayan commented 7 years ago

@idpocky OK

xhubert commented 7 years ago

感谢! 另外,对于dva-ssr这种方式,打包发布,有什么好的实践方案可以分享一下么?

alexayan commented 7 years ago

@idpocky 目前的项目的项目经验: 前端与服务器端在同一个项目中,都使用 typescript 进行编写,使用 webpack 进行打包。服务器端可以复用前端代码。

xhubert commented 7 years ago

方便分享一个脚手架代码来学习学习么?

alexayan commented 7 years ago

@idpocky Sorry,目前只用在公司的项目上,暂时不方便分享。

xhubert commented 7 years ago

ok,多谢。

alexayan commented 7 years ago

@idpocky 发布了新版本 0.2.0,支持同步渲染,不用配置 asyncActions,并且项目不一定要存在 effect

xhubert commented 7 years ago

@alexayan ok!