rohjs / TIL

Today I Learned 365
6 stars 4 forks source link

Introduce basic usage of saga #8

Closed Rokt33r closed 7 years ago

Rokt33r commented 7 years ago

Note: run npm install or yarn after merge this branch

As you see in lib/redux/form/saga.js, each submitting will take 1 sec.

export default function * formSaga () {
  while (true) {
    // Wait untill SUBMIT_FORM dispatch
    yield take(actionTypes.SUBMIT_FORM)
    // Grab form state in redux store
    const form = yield select(state => state.form)
    // Execute async function, which return promise
    // delay is a helper function of redux saga which just wait given amount of time and resolve true
    // We probably need this when we are going to implement http request for submitting
    yield call(delay, 1000)
    // dispatch addPost action
    yield put(postActions.addPost({
      post: {
        title: form.title,
        content: form.content
      }
    }))
    // dispatch resetForm action
    // Actually, dispatching multiple actions considers harmful.
    // At the first, this usage is fine. But, if we have a performance issue, we have to unify these action into a huge single action.
    yield put(actions.resetForm())
  }
}