跳至主要內容

將傳奇連結到外部輸入/輸出

我們發現 take 效果是由等待動作發放到儲存體中來解決的。而且 put 效果是由發放作為引數提供的動作來解決的。

當一個傳奇開始(在啟動時或稍後動態地),中介軟體自動將其 take/put 連接到 Store。這 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 文件頻道示範