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.

Unclear 'runSaga' options and subscribe field documentation

See original GitHub issue

The documentation about runSaga options is very unclear.

First: options: Object : https://redux-saga.js.org/docs/api/index.html#runsagaoptions-saga-args

the function signature is runSaga(options, saga, ...args) But then where the rest of the fields belong to? dispatch(output), getState() ? Do they still belong to the options?

Second: subscribe field:

subscribe(callback): Function - A function which accepts a callback and returns an unsubscribe function

callback(input): Function - callback(provided by runSaga) used to subscribe to input events. subscribe must support registering multiple subscriptions.
input: any - argument passed by subscribe to callback (see Notes below)

I am having a really hard time to understand what this means, I suppose the function should look something like this.

subscribe: (callback) => {
    //Subscribe function
    callback(input);

    //Unsubscribe function
    return () => { //some code };
},

How can I use it to resolve a take effects for example?. Would be nice to have some examples also. In the testing section Testing the full Saga, there are some nice example, but without using the subscribe field

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Jan 15, 2019

I’ve updated docs around this API - https://github.com/redux-saga/redux-saga/commit/466c5a4f8e941fb0f30a6e6dfb40310883876856 . Keep in mind that it was adjusted to v1, which we are going to release soon.

Is this now clearer or do you feel something still could get improved?

0reactions
arnaudambrocommented, May 8, 2019

OK regarding my last comment, I improved my code a little bit, it is still not working yet : even if the event emitter works properly, the channel.put is called, but the action is never caught. I think there is a problem with the channel but I don’t see where…

Here is my code:

import { runSaga, channel as stdChannel, take, put, select } from 'redux-saga';
import EventEmitter from  'event-emitter';

const recordSaga = async (saga, initialAction, state, onReceiveSend = [], emitter = new EventEmitter()) => {
  const dispatched = [];
  let actionToEmit = 0;

  const channel = stdChannel()
  emitter.on("action", channel.put)

  const done = await runSaga(
    {
      channel,
      dispatch: action => {
        if (onReceiveSend.length &&
              (onReceiveSend.length <= actionToEmit + 1) &&
                onReceiveSend[actionToEmit].receive === action.type) {
          emitter.emit("action", { type: onReceiveSend[actionToEmit].send })
          actionToEmit++
        }
        dispatched.push(action)
      },
      getState: () => state,
    },
    saga,
    initialAction,
  ).done;

  return {
    dispatched,
    done,
  };
};

function* mySaga() {
  const needToSave = yield select(needToSaveDocument);
  if (needToSave) {
    yield put({ type: 'SAVE_DOCUMENT_REQUEST' } );
    yield take('SAVE_DOCUMENT_SUCCESS');
  }
  yield put(doSomethingElse())
}

describe('mySaga', async () => {
  it('test 1: no need to save', async () => {    
    const state = { needToSave: false }
    const { dispatched } = await recordSaga(mySaga, {}, state);
    expect(dispatched).toEqual([
      doSomethingElse()
    ])
  })
  it('test 2: need to save', async () => {
    const state = { needToSave: true }
    const onReceiveSend = [
      {
         receive: 'SAVE_DOCUMENT_REQUEST',
         send: 'SAVE_DOCUMENT_SUCCESS'
      }
    ]
    const { dispatched } = await recordSaga(mySaga, {}, state, onReceiveSend);
    expect(dispatched).toEqual([
      saveDocument(),
      doSomethingElse()
    ])
  })
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference - Redux-Saga
options : Object - A list of options to pass to the middleware. Currently supported options are: context: Object - initial value of...
Read more >
Redux-saga: Calling a saga outside the redux middleware ...
After reading the redux saga docs looks like I have two options either call store.runSaga or use the runSaga utility provided by redux-saga....
Read more >
Using SAGA tool in PyQGIS standalone script. Output doesn't ...
In my standalone script I want to use the SAGA tool "merge vector layers". According to the docs I ...
Read more >
Clean Water Act Model Pleadings and Discovery from Current ...
Defendant's Second Request for Production of Documents VOLUME III C. Crown Cork ... (g) Plaintiff objects to Interrogatory 3(g) as vague and overly...
Read more >
How to Extend Your Visa in Bali, a Step-by-Step Guide
The immigration officers may be a bit confused as to why you want to ... If that's the case, and you entered Visa...
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