Skip to content

Instantly share code, notes, and snippets.

@tricoder42
Last active June 13, 2017 08:08
Show Gist options
  • Save tricoder42/9b8bef5bd1e4be3466344a2fb0260ac7 to your computer and use it in GitHub Desktop.
Save tricoder42/9b8bef5bd1e4be3466344a2fb0260ac7 to your computer and use it in GitHub Desktop.
2017/06/13 [Medium] redux-saga factories and decorators
export function* checkConnection () {
let interval = 1
// eslint-disable-next-line no-constant-condition
while (true) {
// race == wait for effect which comes first
const { timeout, up } = yield race({
// ping triggered manually
// e.g: 'reconnect now' button
ping: take(action.networkPing),
// sth recovered network
up: take(action.networkUp),
// wait before next ping
timeout: call(delay, interval * 1000)
})
// nothing happened, trigger ping and wait for next
if (timeout) {
yield put(action.networkPing())
interval = Math.min(60, interval * 2)
} else if (up) {
break
}
}
}
export function* ping () {
const pingTimeout = 5000
const { ping } = yield race({
ping: call(pingRequest),
timeout: call(delay, pingTimeout)
})
if (ping) {
yield put(action.networkUp())
}
}
export default function* () {
yield takeLatest(action.networkPing, ping)
yield takeLatest(action.networkDown, checkConnection)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment