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.

Middleware should be passing EFFECT actions?

See original GitHub issue

Hi, I’m currently building a performance-critical app:

  • that fires many actions per second (~x60 per second)
  • that has many react-redux connect()ed components
  • that has many sagas running in parallel to take care of effects (~x5 effects per action)

Since I’m also relying heavily on multiple sagas to take care of effects, I get many saga-related actions running through my redux reducers (EFFECT_TRIGGERED, EFFECT_RESOLVED, EFFECT_REJECTED), but I don’t think it is necessary for them to go normal redux reducers, because they are dealt only by sagas anyway.

So I implemented something like:

const no_saga_actions = () => next => action => {
    if (action.type != 'EFFECT_TRIGGERED' &&
        action.type != 'EFFECT_RESOLVED' &&
        action.type != 'EFFECT_REJECTED') return next(action);
};

where my redux configuration store looks like:

const store  = createStore(
    combined_reducer,
    applyMiddleware(
        createSagaMiddleware(...combined_saga),
        no_saga_actions,
    )
);

I know I have to minimize the number of effects and connect()ed components to increase speed eventually, but this has resulted in much greater performance already, simply because my redux store is receiving less actions and that results in less connect()ed wrapper renders.

I wonder if passing saga-related actions is by design, or if there should be an option somewhere to prevent it from flowing to redux reducers (or if you would like me to submit a PR for that)…?

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
joonhyubleecommented, Apr 8, 2016

Just to add, it can be difficult to use existing redux monitoring tools to see all the actions that are being passed to the store, simply because there are too many actions being fired per second and devtools designed to scrutinize individual actions can’t keep up. So I wrote a simple aggregate analytics middleware to use in conjunction with react perf addon: image of aggregate analytics

const make_analytics = () => {
    const perf = React.addons.Perf;
    let stats = {};
    let is_counting = false;

    const count_action = action => {
        if (is_counting) {
            if (!stats[action.type]) stats[action.type] = 1;
            else stats[action.type] = stats[action.type] + 1;
        }
    };

    const start = () => {
        is_counting = true;
        perf.start();
    };

    const stop = () => {
        is_counting = false;
        perf.stop();
        console.log(stats);
        perf.printWasted();
        stats = {};
    };

    const get_stats = () => {
        console.log(stats);
    };

    return {
        count_action,
        start,
        stop,
        get_stats
    };
};

window.analytics = make_analytics();

const analytics_middleware = store => next => action => {
    window.analytics.count_action(action);
    return next(action);
};

and using it looks like:

store  = createStore(
    combined_reducer,
    applyMiddleware(
        analytics_middleware,
        createSagaMiddleware(...combined_saga),
        ignore_middleware
    )
);

in case you are wondering what my ignore_middlware looks like:

const ignore_middleware = store => next => action => {
    if (action.type != 'REQUEST_NAVIGATION') return next(action);
};

@yelouafi sorry, this is not really redux-saga specific, but thought I’d share since somebody asked… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Middleware - Redux
* If the promise is resolved, its result will be dispatched as an action. * The promise is returned from `dispatch` so the...
Read more >
Chapter 6. Handling complex side effects - Redux in Action
Within the Redux architecture, only action creators and middleware should handle side effects. Conveniently, you already have experience using both action ...
Read more >
Middleware - Rodux Documentation
A middleware is a function that accepts the next dispatch function in the middleware chain, as well as the store the middleware is...
Read more >
How to Use Redux Middleware to Better Control Your Data ...
However, at times, we may need to run side effects without ... Reducer creates a new state based on data passed through the...
Read more >
Understanding Redux Through Implementation | by Ross
Without doing anything special, our dispatcher will now know to pass the action to the logger middleware, logging it's data before passing ......
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