Created
January 26, 2019 00:10
-
-
Save therealparmesh/01108de3aa4035687a1d3200493ae411 to your computer and use it in GitHub Desktop.
Redux Saga helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { call, cancel, fork, take } from 'redux-saga/effects'; | |
export function takeLatestPerKey(patternOrChannel, worker, keySelector, ...args) { | |
return fork(function*() { | |
const tasks = {}; | |
while (true) { | |
const action = yield take(patternOrChannel); | |
const key = yield call(keySelector, action); | |
if (tasks[key]) { | |
yield cancel(tasks[key]); | |
} | |
tasks[key] = yield fork(worker, ...args, action); | |
} | |
}); | |
} | |
export function takeLeadingPerKey(patternOrChannel, worker, keySelector, ...args) { | |
return fork(function*() { | |
const tasks = {}; | |
while (true) { | |
const action = yield take(patternOrChannel); | |
const key = yield call(keySelector, action); | |
if (!(tasks[key] && tasks[key].isRunning())) { | |
tasks[key] = yield fork(worker, ...args, action); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment