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-queue

v0.2.0

Published

Library to handle queued requests with ease in your Redux application

Downloads

14

Readme

Redux-Queue

Build Status

Higher order reducer so you don't have to worry about order of arrival of the results of your actions.

Installation

Using Yarn:

yarn add redux-queue

In your reducers.js (or where you combine your reducers), wrap it:

import Queue from 'redux-queue';
import MyReducer from './my_reducer';

MyReducer = Queue(MyReducer);

//...pass to Redux

tl;dr

Make sure the async result of your action is always executed directly after the enthusiastic one. Add queued: true to your actions and add parent: action to the async result to have them execute after the first one:

function doSomething() {
    return dispatch => {
        const action = {
            type: MY_ACTION,
            queued: true
        };
        dispatch(action);
        
        setTimeout(() => {
            dispatch({
                type: MY_ACTION_SUCCESS,
                queued: true,
                parent: action
            });
        }, 1000);
    };
}

Example

Managing state while responses will arrive asynchronously back to you is hard. Imagine the following scenario:

Dispatch action 1, user continues and action 2 is dispatched, meanwhile you're syncing state with the server so you get the response from action 1 back, lets call this a. So now the order of dispatched actions on your reducer is as follows:

1 -> 2 -> a

Ok, so if I apply my server state a on top of 2 this might result in weird unexpected glitches in the resulting state, god forbid the initial request fails and we're left with an out-of-sync state. What we want is:

1 -> a -> 2

But without having to block user interaction while we're syncing state with, for example, a server. This is where the Redux-Queue comes into play.

It will save all applied actions and prior states so it will always be able to "inject" the server response in between two other actions as well as reverting local changes when a prior request fails.

Usage

Set-up by wrapping your regular reducers with the Queue:

import {combineReducers} from 'redux'
import Queue from 'redux-queue';

let reducers = combineReducers({
    entries: Queue(entries)
});

Dispatch actions which announce they are queued (including Redux Thunk):

function doSomething() {
    return dispatch => {
        const action = {
            type: MY_ACTION,
            queued: true
        };
        dispatch(action);
        
        setTimeout(() => {
            dispatch({
                type: MY_ACTION_SUCCESS,
                queued: true,
                parent: action
            });
        }, 1000);
    };
}

No matter what happens in between MY_ACTION and MY_ACTION_SUCCESS these will always be executed in order on the entries reducer.

So what to do on failure? Here you go:

function doSomething() {
    return dispatch => {
        const action = {
            type: MY_ACTION,
            queued: true
        };
        dispatch(action);
        
        setTimeout(() => {
            dispatch({
                type: MY_ACTION_FAILED,
                queued: true,
                failed: true,
                parent: action
            });
        }, 1000);
    };
}

Now the state of entries will revert back to the state just before MY_ACTION