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-action-side-effects

v0.10.0

Published

Add side effects to your actions

Downloads

963

Readme

redux-action-side-effects

Trigger side effects after actions have been reduced.

npm install --save redux-action-side-effects

What's a side effect?

A side effect is a function which is invoked or an another action which is dispatched as a result of an original action being reduced.

import { withSideEffects } from 'redux-action-side-effects';

// Create an action as per usual.
const action = someAction();

// Optionally, create an action which would be dispatched after an original action being reduced.
const actionHasBeenReducedAction = someAnotherAction();

// Tag it with a side-effect. Note that a new action is returned; the original
// action is not mutated.
const actionWithSideEffect = withSideEffects(
  action,
  () => console.log('action reduced!'),
  actionHasBeenReducedAction
);

Motivation

Sometimes you need to invoke logic or dispatch another action after the store's state has been modified (typically in stateful middleware). As middleware can be asynchronous, it may cause issues to depend on the following pattern as there is no guarantee that the next piece of middleware will have not have deferred, or detained the action.

function customMiddleware() {
  return next => action => {
    if (action.type === 'SOME_ACTION') {
      next(action);

      // Assume that the action has been reduced and the store updated.
      triggerSideEffect();
    }
  }

Installation

npm install --save redux-action-side-effects

Then, to enable redux-action-side-effects, add it to your store's composition chain:

import { createStore, applyMiddleware, compose } from 'redux';
import { processSideEffects } from 'redux-action-side-effects';

const store = compose(
  applyMiddleware( /* your middleware */ ),
  processSideEffects
)(createStore)(reducer);

processSideEffects should be the last store enhancer in your chain (or at least come after any store processors which delay, or detain actions) to ensure side-effects are processed after an action has been reduced.

Implementation

Side effects are store in an action's meta.sideEffects property as an Array of functions. Each side effect is invoked in order with the store's dispatch function which can be used to dispatch other actions.

Example Usage

The following example shows how we can handle a common use-case example; showing a spinner whilst an API call is made and dismissing it afterwards.

import { CALL_API } from 'redux-api-middleware';
import { withSideEffect } from 'redux-action-side-effects';

function fetchUser(userId) {
  return {
    [CALL_API]: {
      endpoint: `http://example.org/users/${userId}`,
      method: 'GET',
      types: [
        { type: 'SHOW_SPINNER' },
        withSideEffect(
          { type: 'FETCH_USER_SUCCESS', payload: response },
          (dispatch) => dispatch({ type: 'HIDE_SPINNER' })
        ),
        { type: 'FETCH_USER_ERROR' },
      ]
    }
  };
}

Same example using HIDE_SPINNER action as a plain javascript object.

import { CALL_API } from 'redux-api-middleware';
import { withSideEffect } from 'redux-action-side-effects';

function fetchUser(userId) {
  return {
    [CALL_API]: {
      endpoint: `http://example.org/users/${userId}`,
      method: 'GET',
      types: [
        { type: 'SHOW_SPINNER' },
        withSideEffect(
          { type: 'FETCH_USER_SUCCESS', payload: response },
          { type: 'HIDE_SPINNER' }
        ),
        { type: 'FETCH_USER_ERROR' },
      ]
    }
  };
}