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

Redux middlewares? #113

Closed
JacobOscarson opened this Issue Jul 4, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@JacobOscarson

JacobOscarson commented Jul 4, 2016

Hello, me again I'm afraid (just a question this time),

Is there a way to run redux middlewares, and in that case how? (I saw a mention of a file redux-middlewares.js in the source code, took a chance an created one in my redux/ directory of my Roc project, but it didn't seem to be imported). I'm looking at running redux-saga in my project, and the docs tells me to install the middleware in the createStore() call for Redux.

@dlmr dlmr added the question label Jul 4, 2016

@dlmr

This comment has been minimized.

Member

dlmr commented Jul 4, 2016

You can add Redux middlewares as you tried to do but the file you mentioned, redux-middlewares.js, will be resolved against the root of the project. You can change this by setting the following in the settings.

Example

settings: {
  build: {
    reduxMiddlewares: 'redux/middlewares.js'
  }
}

However, that will not work with redux-saga since it requires some additional things to function as you mentions. Currently will you need to do the following in your project to go around this and we are working on making this better in a coming release of roc-package-web-app-react and you can use the branch in the meantime as an alternative but it is not fully completed yet and your milage may vary.

@JacobOscarson

This comment has been minimized.

JacobOscarson commented Jul 6, 2016

It's possible, also with saga, I just made a successful end-to-end ("some assembly required" as you'll see):

  • I added a settings entry as you described.
  • My redux/middlewares.js looks like this:
import createSagaMiddleware from 'redux-saga'

const sagaMiddleware = createSagaMiddleware();

const middlewares = [sagaMiddleware];

export { sagaMiddleware }; // <- this will make the middleware reachable

export default function() {
  return middlewares;
}
  • In a component where I want to trigger sagas, I install & start the saga once using Lodash once(), and triggers that in the components render() method:
import { once } from 'lodash';

import * as saga from 'redux-saga'

import * as effect from 'redux-saga/effects';

// From export in prev. code, middleware accessible as middleware.sagaMiddleware now:
import * as middlewares from '../../redux/middlewares.js';

// ...

function doFetching(param) {
  console.log('SAGA doFetching..', param);
  return Promise.resolve('*P*');
}

function *doAuth(action) {
  console.log('SAGA doAuth REQUESTED.', action);
  const res = yield effect.call(doFetching, '*param*')
  console.log('FAKE FETCHING RESPONSE', res);
}

function *mySaga() {
  yield saga.takeEvery('auth!', doAuth);
}

function installSaga() {
  console.log('Installing saga middleware..');
  middlewares.sagaMiddleware.run(mySaga)
}

const installSagaOnce = once(installSaga);

// ...

    render() {
      // ...
      installSagaOnce();
  • After that, I was able to send a Redux action that triggered the proof-of-concept saga (a no-op for starters)
  • When triggered an action along the lines of {type: 'auth!', ...} could see the following in my DevTools console:
SAGA doAuth REQUESTED.
Object {type: "auth!", ... }
SAGA doFetching.. *param*
FAKE FETCHING RESPONSE *P*

\o/

@dlmr dlmr added the extensions label Sep 20, 2016

@dlmr

This comment has been minimized.

Member

dlmr commented Sep 20, 2016

Awesome that you found a way to solve this! 👍

I will close this issue for now and we should soon add/look into add first class support for redux-saga in Roc now that we are almost ready to publish the new version.

@dlmr dlmr closed this Sep 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment