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-async-action

v0.0.6

Published

Redux middleware for dispatching FSA actions for asynchronous actions

Downloads

14

Readme

redux-async-action

Build Status npm version

Overview

Typically when you asynchronously dispatch actions in Flux or Redux, you'd want to know when the action creator kicked off the asynchronous job, when the job finished, the results of the job, and whether there was an error performing the job. Very commonly, you may see UIs or views that display different components based on the state of the job, e.g., a spinner animation is displayed after the job started and is waiting for the results, and when the results arrive, the spinner is removed and the results are displayed.

Certain events, or actions in this case, are needed to allow subscribers or listeners to know what to do based on the state of the app. As a result, this Redux middleware provides a way to dispatch all the necessary actions to update the state based on the progress of an asynchoronus job. The following FSA-compliant actions are dispatched:

Dispatched before the asynchronous job starts:

{
  type: 'ACTION_TYPE__START'
}

Dispatched when the job resolves with the results from the job:

{
  type: 'ACTION_TYPE',
  payload: results
}

Dispatched when there is an exception from the job (the job throws or is a promise that rejects):

{
  type: 'ACTION_TYPE__ERROR',
  payload: Error
  error: true
}

Installation

npm install --save redux-async-action

Then, use applyMiddleware():

import { createStore, applyMiddleware } from 'redux';
import { asyncActionMiddleware } from 'redux-async-action';
import rootReducer from './reducers';

const store = createStore(
  rootReducer,
  applyMiddleware(asyncActionMiddleware)
);

Usage

See below for examples:

// dispatch an action with a function payload
// the function payload can return the value or a promise that resolves the value
const fetchRandomUser = () => {
  // returns a random user
};

store.dispatch({
  type: 'FETCH_USER',
  payload: fetchRandomUser
});

// or

// dispatch an action with a value payload
store.dispatch({
  type: 'FETCH_USER',
  payload: user
});

// or

// dispatch an action creator (a function that returns an action)
store.dispatch(() => {
  // assume the user was previously retrieved in this function

  return {
    type: 'FETCH_USER',
    payload: user
  };
});

In all the above examples, a FETCH_USER__START action would first be dispatched, and then when the value is settled, a FETCH_USER action would be dispatched. In the case an exception is thrown, a FETCH_USER__ERROR action would be dispatched. There is no "SUCCESS" action. In this case, the FETCH_USER and FETCH_USER__ERROR actions serve as the success. Also, the original action dispatched to the middleware will not be passed down the remaining chain of middlewares, but the actions dispatched by the middleware would be sent through the entire chain of middlewares.

Any other action formats dispatched to the middleware will be ignored and passed to the next middleware in line, if any.

All actions dispatched to the middleware must be FSA-compliant. The actions dispatched from the middleware are also FSA-compliant.

Metadata

You can optionally attach metadata to the actions. The metadata will be passed onto the ___START and resolved actions through the meta property.

Example:

store.dispatch({
  type: 'FETCH_USER',
  payload: fetchRandomUser // fetchRandomUser is a function
  meta: {
    session: sessionData
  }
});

Notes

If you're also using redux-thunk, asyncActionMiddleware must be placed before redux-thunk in the middleware chain.