Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to compose sagas #737

Closed
nakamorichi opened this issue Jan 1, 2017 · 4 comments
Closed

Recommended way to compose sagas #737

nakamorichi opened this issue Jan 1, 2017 · 4 comments
Labels

Comments

@nakamorichi
Copy link

By reading the documentation, I couldn't figure out the preferred way to compose sagas. In my reducer files, I have defined watchers in the following way:

export const reducerXWatchers = function* () {
	yield takeLatest(A_GET_REQUEST, aGetWorker);
	yield takeLatest(A_POST_REQUEST, aPostWorker);
	yield takeLatest(B_GET_REQUEST, bGetWorker);
};

My root saga:

export const rootSaga = function* () {
	yield [
		fork(reducerXWatchers),
		fork(reducerYWatchers)
	];
};

Previously I had defined my watchers in separate generator functions using yield*, but now it seems to be possible to have them inside the same generator function using yield. Is this correct and the recommended approach, and is it possible to simplify things even further?

@nakamorichi nakamorichi changed the title Recommended way to create compose sagas Recommended way to compose sagas Jan 1, 2017
@mykyta-shulipa
Copy link

@Kitanotori
We are using the same case as your first, but in a slightly different way:

export function* reducerXWatchers() {
  yield [
    takeLatest(A_GET_REQUEST, aGetWorker),
    takeLatest(A_POST_REQUEST, aPostWorker),
    takeLatest(B_GET_REQUEST, bGetWorker),
  ];
}

@Andarist
Copy link
Member

Andarist commented Jan 1, 2017

Starting from v0.14.0 you can import those helpers from redux-saga/effects and that is a recommended way now

yield takeLatest(...)
yield takeLatest(...)

vs

yield [
  takeLatest(...)
  takeLatest(...)
]

Not much of a difference. Possibly second might be A LITTLE bit faster, especially if you need to use regenerator-runtime transform, as after each task processed it needs to go back to the originating saga and grab next one to run, when yielding arrays all tasks in it are started sequentially and their descriptors will get passed in to the saga afterwards. But overally chose whichever one you want.

@fzaninotto
Copy link

@Andarist If I understand correctly, this advice only applies to yielding helpers from Sagas. What about composing several sagas from different domains?

@Andarist
Copy link
Member

In the other thread I've tried to explain (maybe poorly) that the same comment applies somewhat to the part of your question.

Helpers and your sagas are not completely different things (only that helpers are a built-in). You want to call both of them with fork effect (and helpers are wrapped by it internally for you, so you dont have to worry about it and do it manually). So only if you are wondering about difference between:

yield fork(saga1)
yield fork(saga2)

vs

yield [
  fork(saga1),
  fork(saga2)
]

the same comment as above applies. Those are basically the same thing and you can chose whichever you want.

The interesting part is what will happen if you yield array of other things. Each of them will get run in parallel and the whole yield will be resolved only once EACH of the things in the array ends. And as you can also yield array of forks (cause... why not?) this makes it only a special case of the parallel effect (which will resolve synchronously as forks are non-blocking).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants