Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support saga effects put.resolve(action) #1038

Closed
wants to merge 1 commit into from

Conversation

kerryChen95
Copy link

No description provided.

@coveralls
Copy link

coveralls commented Jul 5, 2017

Coverage Status

Coverage decreased (-1.2%) to 93.913% when pulling 6dd597f on kerryChen95:put-resolve into 4e76edd on dvajs:master.

@sorrycc
Copy link
Member

sorrycc commented Jul 6, 2017

put.resolve 在啥情况下使用? 感觉用不到,有 call 就够了。

@kerryChen95
Copy link
Author

kerryChen95 commented Jul 12, 2017

  1. call 的话,使用别的 namespaceeffects 时,需要 call(anotherModel.effects.methodName) ,也就是需要确保先将 anotherModelimport 进来(不管是同步还是异步的),拿到 anotherModel 的引用,增加模块依赖的复杂度,增加做按需加载的复杂度。而用 put.resolve(type: 'anotherNamespace/methodName') 可以不需要拿到 anotherModel 的引用。
  2. put.resolve() 会在 redux 中留下一条 action 记录,而 call 不会,这非常便于 debug ,尤其是各种异步操作交织在一起的时候,看 action 发生的记录能很清晰的知道各个异步操作发生的顺序等信息,比如截图中带有 Async 后缀的 action ,我能知道它们发生的顺序,每次发生时携带的信息,出问题时方便找到原因。
    image

@nihgwu
Copy link
Member

nihgwu commented Jul 12, 2017

我觉得应该可以支持注入 effects #886 (comment) #886 (comment)

当然现在也可以直接传入自定义的 effects,但是配合 namespace 的时候不灵活

@sorrycc
Copy link
Member

sorrycc commented Jul 19, 2017

你是要实现 put 的同步执行吗? 但 put.resolve 解决的是 Promise action 的问题,put.resolve 另一个 model 的 effect,并不会等那个 effect 执行完之后再执行下一步的。

@kerryChen95
Copy link
Author

@sorrycc 对,是想实现同步执行。

本来以为 put.resolve(type: 'anotherNamespace/methodName') 能实现,但确实如你所说,并不能。

而用 call(anotherModel.effects.methodName) 的话,如我前面所说,有增加依赖复杂度的问题。

但我现在又开始觉得用 call(anotherModel.effects.methodName) 或许是个好方案了,因为在一个模块中,类似于 put.resolve(type: 'anotherNamespace/methodName') 的代码能按预期执行,暗含着一个前提,就是 anotherNamespace 所在的模块已经加载。
所以,使用 call(anotherModel.effects.methodName) 这个方案能非常明确地让依赖的模块预先加载好,破除了隐式依赖的问题,反而降低依赖复杂度。

@sorrycc
Copy link
Member

sorrycc commented Aug 18, 2017

可以试试 put + take 的组合:

yield put({ type: 'addDelay', payload: { amount: 2 } });
yield take('addDelay/@@end');

@kerryChen95
Copy link
Author

put + take 的组合也是一种可行的方案,但不够健壮,比如

effects: {
  * doSth ({ payload }, { put, take }) {
    yield put({ type: 'addDelay', payload: { amount: 2 } })
    yield take('addDelay/@@end')
  }
}

如果短时间内大量调用 doSth() ,等待到的 addDelay/@@end 事件,不一定是同一个 doSth() 内部的 addDelay 内部触发的,当然也有完善措施,比如使用 saga 的 channel 或加 id,但就复杂度剧增了。

@sorrycc
Copy link
Member

sorrycc commented Sep 2, 2017

先关了,put.resolve 并不能达到预期目的。

@sorrycc sorrycc closed this Sep 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants