npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nfen/redux-saga-injector

v1.2.1

Published

Redux Saga utils for easier use of sagas on the server and dynamic components

Downloads

341

Readme

Travis codecov

Redux Saga Injector

Inject sagas using sagas, in a small 3KB package. Automatically queues up and cancels sagas on both the server-side and client-side.

This package is intended to solve issues where sagas need to be injected dynamically, and then cancelled when they're no longer needed i.e. when a user goes to another page in a SPA. It prevents side effects from occuring when sagas use similar actions or logic.

Good for composing components with their needed business logic, such as in the case of using react loadable or react universal component, or only running business logic on a single page.

Installation

npm install @nfen/redux-saga-injector
# or
yarn add @nfen/redux-saga-injector

Client Side

Components using the SagaInjector, will automatically run with this saga running on the client-side. Make sure to structure your code so that you're not running the sagaRunner on the server.

import { spawn } from 'redux-saga/effects';
import { sagaRunner } from '@nfen/redux-saga-injector';

export default function* rootSaga() {
    yield spawn( sagaRunner );
}

Manual Saga Injection

You can have more granular control over the injection and cancellation of sagas by dispatching actions.

The sagas to be injected need some sort of differentiator, so that they can be properly cancelled if needed.

import { runSagas, cancelSagas } from '@nfen/redux-saga-injector/actions';
import { genUID } from '@nfen/redux-saga-injector/utils';
import * as sagas from './sagas';
import store from './store';

const sagasToInject = [ sagas.one, sagas.two, sagas.three ];
const sagasId = genUID();

store.dispatch( runSagas(sagasToInject, sagasId) );

// Do some stuff...

store.dispatch( cancelSagas(sagasId) );

Server Side Rendering

This uses a queue in order to inject sagas for the server. Injected sagas are loaded in and then wait til they either finish, are cancelled, or timeout after 5 seconds.

Setting up

First, update the store with the augmentStore function like so. It adds a couple of methods on the store that's used by the queueing system, and for sagas to automatically cancel.

import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';
import { augmentStore } from '@nfen/redux-saga-injector';

const sagaMiddleware = createSagaMiddleware();
const store = createStore(/* redux store arguments */);
augmentStoreForSagas( sagaMiddleware, store );

React Router 4 Example

This example utilizes express.js and async/await syntax


const Application = ( {
    context,
    req,
    store,
} )=> (
    <Provider store={store}>
        <StaticRouter
            location={req.url}
            context={context}
        >
            {/* Your App here */}
            <App />
        </StaticRouter>
    </Provider>
);


async function render( req, res ){
    const store = createStore();
    const context = {};

    // Server saga listens for any injected sagas to finish
    const preload = store.runSaga( preloadQueue );

    // Start initial render to start sagas
    // This is a throwaway render
     ReactDOMServer.renderToString( <Application store={store} context={context} req={req} /> } );

    // Finish early if context was defined
    if( context.url ) {
        // End in progress sagas as we'll never finish render
        store.close();
        return res.redirect( 301, context.url );
    }

    // Proceed after preload saga finishes
    await preload.done;

    // Get markup with updated store
    const html = ReactDOMServer.renderToString( <Application store={store} context={context} req={req} /> } );

    // Send page to client
    res.send( html );
}

React HOC Component

A higher order component is availble and can be used like so. It has two benefits:

  • Keeps track of sagas it injects with a unique id, so many components can inject sagas on the same page (for SSR).
  • Cancels the running sagas automatically when the component unmounts
import { SagaInjector } from '@nfen/redux-saga-injector/components';
import MyComponent from './MyComponent';
import * as saga from './sagas';

const injector = SagaInjector({
    sagas: [
        saga.one,
        saga.two,
        saga.three
    ]
});

export default injector( MyComponent );

Use HOC with react-redux-injector

By utilizing our sister package, you can dynamically inject reducers and sagas on the fly.

import { compose } from 'redux';
import { SagaInjector } from '@nfen/redux-saga-injector/components';
import { ReducerInjector } from '@nfen/redux-reducer-injector/components';
import MyComponent from './MyComponent';
import * as saga from './sagas';
import * as reducers from './reducers';

const injector = (options) => compose(
    SagaInjector(options),
    ReducerInjector(options),
);

export default injector( MyComponent );