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-fsa-thunk

v0.0.2

Published

FSA-compliant thunk middleware for Redux.

Downloads

307

Readme

redux-fsa-thunk

Build Status

FSA-compliant thunk middleware for Redux.

npm install redux-fsa-thunk

Middleware Usage

The middleware function is the default export and is used like any other middleware.

import { createStore, applyMiddleware } from 'redux';
import fsaThunkMiddleware from 'redux-fsa-thunk';
import rootReducer from './reducers';

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

Actions

Like redux-thunk, redux-fsa-thunk is used to allow action creators to return functions that dispatch other actions, possibly asynchronously or conditionally. The first difference is that redux-fsa-thunk middleware only handles FSA actions. The payload of the action must be a function. Another difference from redux-thunk is that in addition to any actions dispatched inside the function, the original action is also dispatched before executing the function, but with the function removed from the payload and replaced with null.

const action = {
  type: 'EXAMPLE_ACTION',
  payload: (dispatch, getState) => {
    // Dispatch other actions here.
  }
};

The thunk function will be executed synchronously and the dispatch and getState functions from the store are passed as arguments to the function.

const action = {
  type: 'FIRST_ACTION',
  payload: (dispatch, getState) => {
    dispatch({ type: 'SECOND_ACTION' });

    if (getState().foo) {
      dispatch({ type: 'THIRD_ACTION' });
    }

    fetch('/data')
      .then(response => response.json())
      .then(json => dispatch({ type: 'FOURTH_ACTION', payload: json });
  }
};

Usage with redux-actions

Since redux-fsa-thunk only handles FSA actions you can use redux-actions to create thunk actions.

import { createAction } from 'redux-actions';

const fetchUser = createAction('FETCH_USER', (id) => {
  return (dispatch, getState) => {
    fetch('/user/' + id).then(
      user => dispatch(fetchUserSuccess(user)),
      err => dispatch(fetchUserFailure(err))
    )
  };
});

const fetchUserSuccess = createAction('FETCH_USER_SUCCESS');
const fetchUserFailure = createAction('FETCH_USER_FAILURE');

dispatch(fetchUser(1));

Optimistic Updates

One common use case for thunks is performing optimistic updates before any asynchronous operations complete. In this example an update to a user in the store is dispatched and the store is updated synchronously, but the previous user state is stored so that it can be rolled back if the asynchronous update fails.

import { createAction, handleActions } from 'redux-actions';

const updateUser = createAction(
  'UPDATE_USER',
  (id, data) => {
    return (dispatch, getState) => {
      fetch('/user/' + id, {
        method: 'post',
        credentials: 'same-origin',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
      }).then(
        user => dispatch(updateUserSuccess({ id, user })),
        err => dispatch(updateUserFailure({ id, err }))
      )
    };
  },
  (id, data) => { id, data }
);

const updateUserSuccess = createAction('UPDATE_USER_SUCCESS');
const updateUserFailure = createAction('UPDATE_USER_FAILURE');

const initialState = {
  users: {
    'j.doe': {
      name: 'John Doe'
    }
  },
  rollbackData: {
  }
};

const reducer = handleActions({
  'UPDATE_USER': function (state, action) {
    const { id, data } = action.meta;

    return {
      users: {
        ...state.users,
        [id]: { ...state.users[id], ...data }
      },
      rollbackData: {
        ...state.rollbackData,
        [id]: state.users[id]
      }
    };
  },
  'UPDATE_USER_SUCCESS': function (state, action) {
    const { id } = action.payload;

    return {
      users: state.users,
      rollbackData: {
        ...state.rollbackData,
        [id]: {}
      }
    };
  },
  'UPDATE_USER_FAILURE': function (state, action) {
    const { id } = action.payload;

    return {
      users: {
        ...state.users,
        [id]: { ...state.users[id], ...state.rollbackData[id] }
      },
      rollbackData: {
        ...state.rollbackData,
        [id]: {}
      }
    };
  },
}, initialState);

dispatch(updateUser('j.doe', { name: 'Jack Doe' }));

See Also