メインコンテンツへ移動

サーガを外部入出力に接続

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() { ... },
)

詳しくは、APIドキュメントチャネルデモをご覧ください。