サーガを外部入出力に接続
take
エフェクトは、アクションがストアに送信されるのを待って解決されることがわかりました。また、put
エフェクトは、引数として提供されるアクションを送信することで解決されます。
サーガが(起動時または後で動的に)開始されると、ミドルウェアは、そのtake
/put
をストアに自動的に接続します。 2つのエフェクトは、サーガへの入出力の一種と見なすことができます。
redux-saga
は、Reduxミドルウェア環境の外でサーガを実行し、それをカスタム入出力に接続する方法を提供しています。
import { runSaga, stdChannel } from 'redux-saga'
const emitter = new EventEmitter()
const channel = stdChannel()
emitter.on("action", channel.put)
const myIO = {
// this will be used to orchestrate take and put Effects
channel,
// this will be used to resolve put Effects
dispatch(output) {
emitter.emit("action", output)
},
// this will be used to resolve select Effects
getState() {
return state
}
}
runSaga(
myIO,
function* saga() { ... },
)