All Projects → JBlaak → Redux Queue

JBlaak / Redux Queue

Higher order reducer to handle execution order of actions

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Redux Queue

Train
Train, a fast (FIFO) queue with rollback mechanism.
Stars: ✭ 5 (-87.18%)
Mutual labels:  queue
Quedis
Quedis - redis queue for bosses
Stars: ✭ 31 (-20.51%)
Mutual labels:  queue
Sidekiq Job Php
Push and schedule jobs to Sidekiq from PHP
Stars: ✭ 34 (-12.82%)
Mutual labels:  queue
Inc.runtime.queue
An runtime queue use Asynchronous program
Stars: ✭ 19 (-51.28%)
Mutual labels:  queue
Appserver
A multithreaded application server for PHP, written in PHP.
Stars: ✭ 930 (+2284.62%)
Mutual labels:  queue
Storage Based Queue
Javascript queue library with persistent storage based queue mechanism for the browsers environments. Specially designed for offline.
Stars: ✭ 33 (-15.38%)
Mutual labels:  queue
Arq
Fast job queuing and RPC in python with asyncio and redis.
Stars: ✭ 695 (+1682.05%)
Mutual labels:  queue
Laravel Queue Database Ph4
Laravel Database Queue with Optimistic locking
Stars: ✭ 37 (-5.13%)
Mutual labels:  queue
Go Queue
Multi backend queues for Golang
Stars: ✭ 15 (-61.54%)
Mutual labels:  queue
Azure.data.wrappers
Azure Storage Simplified
Stars: ✭ 34 (-12.82%)
Mutual labels:  queue
Liblist
Generic Linked list Management Library in C
Stars: ✭ 22 (-43.59%)
Mutual labels:  queue
Activejob Scheduler
A background job scheduler for any queue backend
Stars: ✭ 24 (-38.46%)
Mutual labels:  queue
Cockburst
一个高性能,可靠,异步的本地持久化队列实现;重启JVM、重启服务器、或者强制KILL进程时,队列里的数据不丢失;
Stars: ✭ 33 (-15.38%)
Mutual labels:  queue
Bigq
Messaging platform in C# for TCP and Websockets, with or without SSL
Stars: ✭ 18 (-53.85%)
Mutual labels:  queue
Yii2 Queue
Yii2 Queue Extension. Supports DB, Redis, RabbitMQ, Beanstalk and Gearman
Stars: ✭ 977 (+2405.13%)
Mutual labels:  queue
30 Seconds Of Cpp
30 Seconds of C++ (STL in C++). Read More about 30C++ here 👉
Stars: ✭ 815 (+1989.74%)
Mutual labels:  queue
Laravel Couchbase
Couchbase providers for Laravel
Stars: ✭ 31 (-20.51%)
Mutual labels:  queue
Yii Queue
Queue extension for Yii 3.0
Stars: ✭ 38 (-2.56%)
Mutual labels:  queue
Cracking The Coding Interview
Solutions for Cracking the Coding Interview - 6th Edition
Stars: ✭ 35 (-10.26%)
Mutual labels:  queue
Core
Package core is a service container that elegantly bootstrap and coordinate twelve-factor apps in Go.
Stars: ✭ 34 (-12.82%)
Mutual labels:  queue

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].