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.

Passing dispatch to saga

See original GitHub issue

I have a situation where I’d like to use dispatch within a nested callback of an async ajax response, outside of the typical generator flow. I was wondering if you saw any problems with changing this line in middleware.js from:

sagas.forEach(runSaga)

to:

sagas.forEach(runSaga.bind(null, dispatch))

so dispatch would be received by the root saga(s) and it’d be possible to then pass this along to additional functions which operate outside of the generator runtime.

Issue Analytics

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

github_iconTop GitHub Comments

10reactions
yelouaficommented, Feb 11, 2016

Arguments are passed here.

Using the dynamic running API. Can’t the following solve your issue ?

configureStore.js

import createSagaMiddleware from 'redux-saga'

// export middleware
export const sagaMiddleware = createSagaMiddleware()

export default function createStore() {
   return createStore(reducer, applyMiddleware(sagaMiddleware ))
}

main.js

import createStore, {  sagaMiddleware } from `./configureStore`
import rootSaga from '...'

const store = createStore()
sagaMiddleware.run(rootSaga, store.dispatch))

1reaction
danturucommented, Mar 3, 2016

I’ve just found the way. With the wrapObservable helper from the https://github.com/eggers/async-generator package we can do:

type ResolvePromise<T> = (value: T) => void;
type RejectPromise<T> = (value: any) => void;
type PromiseArguments<T> = [ResolvePromise<T>, RejectPromise<T>];

export function* wrapObservable<T>(observable: Observable<T>): IterableIterator<Promise<T>> {
  let subscription: any;

  try {

    let done: boolean = false;

    const sentPromises: PromiseArguments<T>[] = [];
    const completedPromises: Promise<T>[] = [];

    subscription = observable.subscribe(
      (item: T) => {
        if (sentPromises.length > 0) {
          let [resolve, ] = sentPromises.shift();

          resolve(item);
        } else {
          completedPromises.push(Promise.resolve<T>(item));
        }

      },
      (error: any) => {
        if (sentPromises.length > 0) {
          let [, reject] = sentPromises.shift();

          reject(error);
        } else {
          completedPromises.push(Promise.reject<T>(error));
        }
      },
      () => {
        done = true;
      });

    while (!done || completedPromises.length > 0) {
      if (completedPromises.length > 0) {
        yield completedPromises.shift();
      } else {
        yield new Promise((r: ResolvePromise<T>, e: RejectPromise<T>) => sentPromises.push([r, e]));
      }
    }
  } finally {
    subscription.unsubscribe();
  }
}

function *saga() {
  while (true) {
    yield take(SUBSCRIBE);

    const { observable } = yield select();

    for (let next of wrapObservable(observable)) {
      const { action } = yield race({ action: next, overwise: take(UNSUBSCRIBE) });

      if (action) {
        yield put(...);
      } else {
        break;
      }
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dispatching Actions | Redux-Saga
This way we can test the Generator's dispatch in the same way: by inspecting the yielded Effect and making sure it contains the...
Read more >
Different Ways to Dispatch Actions with Redux - Pluralsight
Passing dispatch method to our component. The dispatch method is available on the store object. An action gets dispatched to trigger an ...
Read more >
Access to store.dispatch in a saga for use with react router redux
Any ideas on how to access store.dispatch within a saga? I know you can pass arguments in the root saga but I don't...
Read more >
Understanding Redux Saga: From action creators to sagas
Since the middleware automatically passes the dispatch function to the function that the action creator returns, there is no difference ...
Read more >
Introduction to Redux Saga | LoginRadius Blog
Redux Thunk, a common alternative to Redux Saga, allows functions to be passed into the Redux store dispatch, which checks to see if...
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