question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I use ducks modules i my projects. I think that sagas must be a part of ducks module. But there is a problem with watchers-sagas that we must run. I write lite saga-manager class, which help me to create root saga. Its usage looks like this:

/* 
* auth module
*/

// reducer, actions....

// sagas
function *fetchAuthSaga () { ... }
function *loginSaga () { ... }
function *logoutSaga () { ... }

sagasManager.addSagaToRoot(function *watcher() {
  yield [
    takeEvery(FETCH, fetchAuthSaga),
    takeEvery(LOGOUT, logoutSaga),
    takeEvery(LOGIN, loginSaga),
  ];
});

I do the same things in all modules with sagas. Then i just run root saga:

// index.js
const store = configureStore();

store.runSaga(sagasManager.getRootSaga());

I think that sagasManager is missing part of redux-saga library even if you dont use ducks modules. In a large project there are a lot of sagas in separate files.

Here my implementation of sagasManager:

import { fork } from 'redux-saga/effects';

class SagasManager {
  constructor() {
    this.sagasWithArguments = [];
  }

  addSagaToRoot(...sagaWithArguments) {
    this.sagasWithArguments.push([...sagaWithArguments]);
  }

  getRootSaga() {
    const self = this;

    return function *rootSaga() {
      yield self.sagasWithArguments.map((sagaWithArguments) => fork(...sagaWithArguments));
    };
  }
}

export default new SagasManager;

PS: Sorry my poor English

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Andaristcommented, Jun 17, 2017

@kamleshchandnani just use this in ur split bundle

sagaMiddleware.run(function *watcher() {
  yield all([
    takeEvery(FETCH, fetchAuthSaga),
    takeEvery(LOGOUT, logoutSaga),
    takeEvery(LOGIN, loginSaga),
  ]);
})

or depending on how much involved in this sagasManager you are you may need to tweak it a little bit. i.e. like this:

import { all, flush, fork, take } from 'redux-saga/effects';
import { channel } from 'redux-saga';

class SagasManager {
  constructor() {
    this.sagasWithArguments = channel();
  }

  addSagaToRoot(...sagaWithArguments) {
    this.sagasWithArguments.put([...sagaWithArguments]);
  }

  getRootSaga() {
    const sagasChannel = this.sagasWithArguments;

    return function *rootSaga() {
      const initialSagas = yield flush(sagasChannel);
      yield all(initialSagas.map((initialSagaWithArguments) => fork(...initialSagaWithArguments)));
      
      while (true) {
        const dynamicSaga = yield take(sagasChannel)
        yield fork(...dynamicSaga)
      }
    };
  }
}

export default new SagasManager;
1reaction
Andaristcommented, Jun 17, 2017

Inspect with a debugger this line const initialSagas = yield flush(sagasChannel); - somehow sagasChannel is referencing loginSaga (or some similar one), when it should be a channel’s instance. Should be easily fixable - I’ve written this tweaked code in a rush and ofc didnt test it properly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

saga-duck - npm
extensible and composable duck for redux-saga, support typescript 3+.. Latest version: 3.4.8, last published: 16 days ago.
Read more >
React + Re-ducks + Redux + Saga | limkopi.me
I like the idea of separating presentational components and smart components (containers). The idea is dead simple but this React pattern has a...
Read more >
Scaling your Redux App with ducks - freeCodeCamp
To represent chained operations you need a redux middleware to enhance the dispatch function. Some popular examples are: redux-thunk, redux-saga ...
Read more >
Redux Ducks Pattern as an ES6 Class for REST APIs
The redux application is contained within a JavaScript ES6 class. · The constructor only takes 2 arguments, the name of the app and...
Read more >
redux-saga ducks counter - CodeSandbox
redux-saga ducks counter ... components. containers. pages. sagas. store. index.js. styles.css. package.json. Dependencies.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found