Giter VIP home page Giter VIP logo

redux-enqueue's Introduction

I'd strongly advise against using this. Redux is not good with handling asynchronous actions, and this will not change that. There's almost always better options.

redux-enqueue

Simple queue system for redux. Use with redux-thunk.

npm install --save redux-enqueue

Create Store

Create a redux store using the enqueue middleware.

// createStore.js
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import enqueueMiddleware from 'redux-enqueue';

const middlewares = applyMiddleware(
  thunkMiddleware,
  enqueueMiddleware
);

return createStore(reducers, initialState, middlewares);

Integrate into Actions

Call await dispatch(enqueue(queueId)), which will return a function that you must call when the action is finished. This will ensure that actions sharing a queueIds will not be run concurrently, but actions with other queueIds may be.

Note that we're using try/finally. This is recommended: the code in the finally block is guaranteed to be run.

// authentication.js
import { enqueue } from 'redux-enqueue';

export const login = (username, password) => async dispatch => {
  const completionHandler = await dispatch(enqueue('authentication:login')); // arbitrary id

  try {
    api.login(username, password);
  } finally {
    completionHandler();
  }
};

export const logout = () => async dispatch => {
  const completionHandler = await dispatch(enqueue('authentication:login')); // don't log out whilst logging in
  try {
    api.login();
  } finally {
    completionHandler();
  }
};

Cookbook

Fetching data

The below example will load messages for an id unless they've already been loaded. Fetching messages for different ids can happen concurrently. Fetching for the same id will not be able to happen in parallel, and no subsequent calls for this id will be executed after the first successful run.

// messages.js
import { enqueue } from 'redux-enqueue';

export const loadMessages = id => async (dispatch, getState) => {
  const completionHandler = await dispatch(enqueue(`messages:loadMessages:${id}`));

  try {
    const { currentMessages } = getState().messages;
    if (currentMessages[id]) return;
    const messages = await api.loadMessages(id);
    dispatch({ type: SET_MESSAGES, id, messages });
  } finally {
    completionHandler();
  }
};

redux-enqueue's People

Contributors

jacobp100 avatar

Stargazers

Eric Lei He avatar Vincent Pomageot avatar Franz Heinzmann avatar Andy Gower avatar Karl Alnebratt avatar Travis Cooper avatar Sebastian avatar David Salazar avatar Jordan Addison avatar Richard Hess avatar Petar Bojinov avatar Eric Clemmons avatar Matt Bailey avatar Drew Blaisdell avatar

Watchers

Richard Hess avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.