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

Custom action matchers in Saga #16

Closed
tomsdev opened this issue May 22, 2016 · 5 comments
Closed

Custom action matchers in Saga #16

tomsdev opened this issue May 22, 2016 · 5 comments

Comments

@tomsdev
Copy link

tomsdev commented May 22, 2016

Custom action matchers are available in the Updater but not in Saga. As I understand it, at the moment the saga can only take() an action that is local to the component (e.g. wrapped).

One use case would be a global 'REFRESH' action fired in the app that would need each component saga to fetch their data again.

@tomkis
Copy link
Collaborator

tomkis commented May 22, 2016

Custom action matchers are available in the Updater but not in Saga. As I understand it, at the moment the saga can only take() an action that is local to the component (e.g. wrapped).

Actually take or takeEvery can take predicate as an argument therefore this custom logic can be implemented in Sagas as well... the question is though, should this be part of the framework? Since I believe that this should live in userland.

One use case would be a global 'REFRESH' action fired in the app that would need each component saga to fetch their data again.

I don't think this would be possible even with Custom matcher, maybe it wouldn't even make sense to implement it this way. I am in favour of explicit rather than implicit action wrapping in Sagas.

@tomsdev
Copy link
Author

tomsdev commented May 23, 2016

I'm not sure what you mean exactly by "explicit rather than implicit action wrapping in Sagas". In Elm, the side effects are declared in the Updater so the way to take() actions that involve side effects is exactly the same as actions that just change the state. Even if in redux-elm the side effects are now declared outside of the Updater (e.g. in local sagas), I'd still expect consistency (and as much power) in the way I can take() actions and in the way they are wrapped or unwrapped.That's why I'm proposing to have the same custom action matchers this library provide for the Updater.

@tomkis
Copy link
Collaborator

tomkis commented May 23, 2016

Could you provide some code snippet about what you have envisioned?

@tomkis
Copy link
Collaborator

tomkis commented Aug 9, 2016

Since 3.x will support RxJS Sagas and action matching is totally separated from rest of the architecture in favour of #4 I can imagine we could nicely re-use the matching even in Sagas.

const initialState = {
  incremented: 0,
  decremented: 0
};

const reducer = new MatchingReducerFactory(initialState)
  .case('Incremented', model => ({...model, incremented: model.incremented + 1}))
  .case('Decremented', model => ({...model, decremented: model.decremented + 1}))
  .toReducer();

export default action$ => action$
  .scan(reducer)
  .filter(state => state.incremented >= 3 && state.decremented >= 3)
  .map(() => ({ type: 'ShowPopup' }))
  .take(1);

@tomkis
Copy link
Collaborator

tomkis commented Oct 25, 2016

Closing, solved by Saga & matchers decoupling in v3.

@tomkis tomkis closed this as completed Oct 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants