Skip to content

Instantly share code, notes, and snippets.

@brentmulligan
Last active August 27, 2016 16:54
Show Gist options
  • Save brentmulligan/a97a5a8a9d7a9cc025bcb3d903c6fe4a to your computer and use it in GitHub Desktop.
Save brentmulligan/a97a5a8a9d7a9cc025bcb3d903c6fe4a to your computer and use it in GitHub Desktop.
// This watcher is listening for a START_CREATE_USER dispatch.
// It expects that action to have a payload attribute and it passes that attribute along.
function* watchCreateUser() {
// while (true) tells the watcher to watch forever.
while (true) {
// "yield take" = Wait here until a 'START_CREATE_USER' action is dispatched
const { payload } = yield take(START_CREATE_USER)
// "yield fork" = Asyncly call a generator called startCreateUseFlow, pass it the payload.
yield fork(startCreateUserFlow, payload)
// Instead of looping back and waiting for another START_CREATE_USER (which you probably don't
// want to accept at this moment) you can call take with an array of actions that will occur
// before you'd be ok with calling the flow again.
// when you provide an array to TAKE it will match the first action it receives and then move on.
yield take([USER_CREATE_FAIL, USER_LOGOUT])
}
// This is a never ending loop so we return to waiting for another START_CREATE_USER dispatch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment