Blog>
Snippets

Tracking Down Infinite Loops

Provides an example of using SagaMonitor to identify and debug an infinite loop in a saga, showcasing real-world troubleshooting.
function* watchSagaActions() {
  // Watch for dispatched actions to trigger sagas
  yield takeEvery('*', function* logger(action) {
    console.log('Action Dispatched:', action);
  });
}
This saga watches all actions dispatched to the store and logs them. Useful for identifying actions that may be causing loops by triggering each other.
const sagaMonitor = {
  effectTriggered: (info) => console.log('effectTriggered', info),
  effectResolved: (effectId, result) => console.log('effectResolved', effectId, result),
  effectRejected: (effectId, error) => console.log('effectRejected', effectId, error),
  effectCancelled: (effectId) => console.log('effectCancelled', effectId),
};
Defines a sagaMonitor that logs all saga lifecycle events. This can help identify where in the saga lifecycle things are going awry.
function* rootSaga() {
  // All sagas are started here
  // With sagaMonitor attached to observe saga effects
  yield all([
    spawn(watchSagaActions),
    // add other sagas here
  ], sagaMonitor);
}
The rootSaga initializes all other sagas and includes our sagaMonitor for detailed logging of saga effects. If an infinite loop occurs, the logs can help backtrack the issue.