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.

Proposal: Parametric sagas

See original GitHub issue

I’ve run into following problem: Suppose we have two sagas: one daemon, and one callable:

function *daemonSaga() {
  while (true) {
    const action = take(...);

    yield call(callableSaga, action.payload);
  }
}

function *callableSaga(params) {
  // ...
}

Now, suppose that callableSaga becomes dependent on state (in my particular case it depends on locale setting that is stored in the state). We could use getState() in daemonSaga and pass extra state data to callableSaga, but that’s not very convenient: if there is another dependency in callableSaga, we’d have to change code of daemonSaga again; also, if there is another saga that calls callableSaga, we’d have to duplicate code for extracting state data. It seems that it has to be callableSaga’s responsibility to get its state dependencies.

So my first approach was to just pass getState to callableSaga:

function *daemonSaga(getState) {
  // ...
    yield call(callableSaga, action.payload, getState);
  // ...
}

function *callableSaga(params, getState) {
  // ...
}

Although it still has some drawbacks: every saga that calls callableSaga must accept getState argument even if it doesn’t have dependencies on state itself; if nested call level is longer then we have to pass getState from the very top.

Sounds familiar, isn’t it? (Hint: react-redux).

There’s another concern about getState argument for sagas: they become dependent on state shape which breaks encapsulation.

Proposal

  • Allow sagas to be connected to the store, similar to how React components are connected by react-redux. If function decorators were supported, it could look like this:
@connectSaga(state => /* extract params */)
function* connectedSaga(getParams) {
  // ...
}

Decorator returns regular saga with getState argument. Added benefit here is that we can decouple saga from state shape by using selectors.

  • Allow sagas to call connected sagas without having to pass getState:
function *saga() {
  yield run(connectedSaga);
}
  • Allow connected sagas to accept extra parameters. They can be handled in similar way as react-redux handles component props: merge them with result of selector:
@connectSaga(state => ({stateParam: ...}))
function* connectedSaga(getParams) {
  const {stateParam, extraParam} = getParams();
  // ...
}

function *saga() {
  yield run(connectedSaga, {extraParam: ...});
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:26 (25 by maintainers)

github_iconTop GitHub Comments

1reaction
yelouaficommented, Feb 22, 2016
1reaction
yelouaficommented, Feb 16, 2016

@aikoven What do you think of this proposition

@gaearon first proposed to provide a yield getState() effect so we do not pass the getState param around.

@slorber proposed a more general yield select(selector) Effect.

Edit: added link to issue commoent

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parametric Estimating Handbook, 4th Edition - DAU
parametric estimating techniques and tools to support Government proposal cost estimating requirements. The key issues considered included parametrics' ...
Read more >
(PDF) Proposal of prognostic parametric method applied to an ...
The robustness of the proposed technique has been assessed through a simulation test environment, built on the purpose. Such simulation has ...
Read more >
parametric-vm.pdf - Community Code Review
The Saga of the Parametric VM. John Rose and the Project Vahalla team ... to propose a type parameter (or other specialization request),...
Read more >
API Reference - Redux-Saga
Creates a Redux middleware and connects the Sagas to the Redux Store ... The callback accepts two parameters, where the first parameter is...
Read more >
8 spectacular parametric designs generating sustainable ...
8 examples of sustainable architecture with parametric design ... The proposal combines shopping, culture, and recreation by enlarging ...
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