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

redux-generator

v1.2.1

Published

Redux middleware resolves action which is a generator function

Downloads

23

Readme

redux-generator

Coverage Status npm version Build Status

A middleware for redux, allows to dispatch an action which is a generator function.

Get Started

Installation

npm install redux-generator --save

Usage

First, apply middleware:

import {applyMiddleware, createStore} from 'redux';
// import redux-generator
import generator from 'redux-generator';
// reducers in your project.
import reducers from './path/to/your/reducers';
// apply redux-generator
const createStoreWithMiddleware = applyMiddleware(generator)(createStore);
const store = createStoreWithMiddleware(reducers);

You can write an action creator like this:

// in ./actions/userActionsCreator.js
export const doSaveUser = (user) => {
    return function *(dispatch, getState) {
        // async dispatch an action
        dispatch({type: 'LOADING', payload: 'Loading...'});

        // sync waterfall: a plain object yield
        let params = yield Object.assign({}, user, {lastModified: new Date().getTime()});

        // async dispatch an action
        dispatch({type: 'WILL_SAVE_USER', payload: params});

        // sync waterfall: a promise yield
        let payload = yield new Promise(resolve=> {
            process.nextTick(()=> {
                params.id = '1';
                resolve(params);
            });
        });

        // async dispatch an action
        dispatch({type: 'DID_SAVE_USER', payload});

        // sync waterfall: a thunk yield
        let redirect = yield ()=> {
            if (payload && payload.id) {
                return {type: 'ROUTING_POP', payload: `/user/${payload.id}`};
            }
        };

        // Only this ending action will be dispatched by generator middleware after all
        return redirect;
    };
} 

Dispatch in anywhere:

import {doSaveUser} from './actions/userActionsCreator';
store.dispatch(doSaveUser({name: 'xyx'}));

These series of actions as follow will be dispatched one by one:

dispatch action:      { type: 'LOADING', payload: 'Loading' }
dispatch action:      { type: 'WILL_SAVE_USER', payload: { lastModified: 1476005411707, name: 'xyx' } }
dispatch action:      { type: 'DID_SAVE_USER', payload: { lastModified: 1476005411707, name: 'xyx', id: '1' } }
dispatch action:      { type: 'ROUTING_POP', payload: '/user/1' }

Each action was returned by syntax followed yield will be executed step by step. If one of yield throws error, the others behind will be terminated.

Both errors, whether thunk yield threw or promise yield rejected will be translated to FSA standard error action and dispatched:

import {applyMiddleware, combineReducers, createStore} from 'redux';
import generator from 'redux-generator';

const user = (state = {}, action)=> {
    console.log('action>>>reducer:\t', action);
    return state;
}
const reducers = combineReducers({user});
const store = applyMiddleware(generator)(createStore)(reducers);

// action>>>reducer:    { type: 'ERROR', error: true, payload: [Error: foo] }
store.dispatch(function *action() {
    let payload = yield () => {
        throw new Error('foo');
    }

    // never be executed
    let redirect = yield {type: 'ROUTING_POP', payload};
    return redirect;
});

// action>>>reducer:    { type: 'ERROR', error: true, payload: [Error: bar] }
store.dispatch(function *action() {
    let payload = yield new Promise((resolve, reject)=> {
        process.nextTick(()=>reject(new Error('bar')));
    });

    // never be executed
    let redirect = yield {type: 'ROUTING_POP', payload};

    return redirect;
});

// action>>>reducer:    { type: 'ERROR', error: true, payload: [Error: Inner error] }
store.dispatch(function *action() {
    throw new Error('Inner error');

    // never be executed
    let params = yield ()=> {
        return {name: 'xyx'};
    }

    // never be executed
    let payload = yield new Promise((resolve)=> {
        process.nextTick(()=>resolve(Object.assign(params, {id: 1})));
    });
    return {type: "SAVE", payload};
});

Contribution

test

npm test

build

npm run release