I am using saga eventChannel
to listen to events that are being triggered(maybe WebSocket in the actual app), and then I am updating my Redux Store. In component, I am calling an API action. Then an interval(polling), which is being handled by a saga. I am attaching my event listener once API succeeded for the first time.
After 2nd API call my eventChannel
is terminated somehow.
EventListner:
function* countDownSaga(value) {
const chan = yield call(countdown, value)
try {
while (true) {
// take(END) will cause the saga to terminate by jumping to the finally block
let seconds = yield take(chan)
console.log(`countdown: ${seconds}`)
}
} finally {
console.log('countdown terminated')
}
}
Api saga:
var countDownStarted = false
// Function to be called by saga taking action CALL_FAKE_API
function* fetchData() {
// Simulate some server delay
yield delay(1500)
// Call a function
// redux-saga "call" effect allows you to call a function
const result = yield call(getUserData)
yield put({ type: RECORD_USER, result })
if(!countDownStarted) {
yield fork(countDownSaga, 100)
countDownStarted= true
}
}
Jsfiddle: https://jsfiddle.net/2d9L8fse/2/