Redux-saga: Detecting when a Saga started

Created on 29 Mar 2016  路  3Comments  路  Source: redux-saga/redux-saga

Is there any way to detect when a saga has started? I'm looking for a way to add a loading dialog for all async requests such that when a saga starts, the loading dialog displays and when it is finished the loading dialog stops.

Right now I'm placing yield put(actions.setLoading(true)) at the top of every saga which seems less than ideal.

Most helpful comment

You can also simply create a saga to encapsulate the full logic

function* wrapSaga(saga, ...args) {
   yield put(setLoading(true))
   try {    
    return yield call(saga, ...args)
   } finally {
      yield put(setLoading(false))
   }
}

// used with fork
yield fork(wrapSaga, myAsyncTask, somearg)

All 3 comments

Each saga effect actually emit events. For more details look in sagaMonitor implementation.

You can also simply create a saga to encapsulate the full logic

function* wrapSaga(saga, ...args) {
   yield put(setLoading(true))
   try {    
    return yield call(saga, ...args)
   } finally {
      yield put(setLoading(false))
   }
}

// used with fork
yield fork(wrapSaga, myAsyncTask, somearg)

I ended up going with @yelouafi suggestion, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidwparker picture davidwparker  路  3Comments

tobyl picture tobyl  路  3Comments

Moeriki picture Moeriki  路  3Comments

brunolemos picture brunolemos  路  3Comments

mpyw picture mpyw  路  3Comments