Skip to content

PenguinOfWar/reducks-redux-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

52 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Reducks (Redux Queue)

A redux-observable driven FIFO (first in - first out) queuing system for redux and redux-thunk. Fully compatible with react-redux.

This project's github pages and local demo were bootstrapped with Create React App.

Logo credit: Damian Ryan

npm npm GitHub Pages Node.js Package CodeQL

Demos & Examples

The interactive demos for Reducks leverage react, react-redux and the wonderful libraries rsup-progress and react-toastr to demonstrate the FIFO queuing system in action.

If you are integrating Reducks into a react-redux application I recommend browsing the source code of the demo pages.

Click here for demos and examples

Installation

The installation instructions assume you are already set up and working with redux. If you are not, I recommend you start with the basics.

Step 1

Install Reducks and required peer dependencies.

npm install reducks-redux-queue redux-observable redux-thunk redux rxjs --save

Configure your root reducer and root epics. The reducer name for Reducks must be reducks.

import { combineReducers } from 'redux';
import { reducks, reducksEpic } from 'reducks-redux-queue';

export const rootEpic = combineEpics(reducksEpic);

export default combineReducers({
  ..., // your other reducers go here
  reducks
});

Step 2

Include both in your store. Please see the demo source code for an example integration that includes react-router and redux-dev-tools.

After your store is configured, the epic middleware must be run with custom arguments to pass the store through to Reducks.

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { createEpicMiddleware } from 'redux-observable';

import rootReducer, { rootEpic } from './rootReducer';

const epicMiddleware = createEpicMiddleware();

export default function configureStore({ initialState = {} }) {
  const store = createStore(
    rootReducer,
    initialState,
    compose(applyMiddleware(thunk, epicMiddleware))
  );

  epicMiddleware.run((action$, state$) => {
    return rootEpic(action$, state$, {
      store
    });
  });

  return store;
}

When configuring the root epic, you can also pass custom methods that will be fired when the queue starts and when it finishes. See the demo page for an example of this that implements a progress bar.

epicMiddleware.run((action$, state$) => {
  return rootEpic(action$, state$, {
    store,
    config: {
      onStart: () => {
        // code that will fire when the queue starts
      },
      onFinish: () => {
        // code that will fire when the queue finshes
      }
    }
  });
});

Step 3

With our epic connected up our changes can be observed and the queue can progress. The final step is adding some stuff to our queue.

Reducks exposes an add action for queuing jobs. Connect this into your dispatch.

import { add as addToQueue } from 'reducks-redux-queue';

const dispatched = dispatch => {
  return {
    addToQueue: (name, job) => dispatch(addToQueue(name, job))
  };
};

The add action accepts two properties:

  • name - A friendly name that is logged to the console along with any errors if your job fails.
  • job - Your async/await or Promise job method to be called. Receives the job object as a property. If you do not use async/await or Promise then Reducks will not be able to guarantee call order.

Note that if a job fails the result of the call will be logged to the console and the job will be removed from the queue in order to process the next one.

You can then call the add action to push a job into the queue.

add('addToQueue', async ({ job }) => {
  const { id, name } = job;

  console.log('About to wait');
  console.log(id, name);

  await new Promise(resolve =>
    setTimeout(() => {
      console.log('Waited 5000ms');
      resolve();
    }, 5000)
  );

})};

You can also wrap add with dispatch manually, for example within other actions.

dispatch(
  addToQueue('deleteItem', async () => {
    /* your async code here */
  })
);

Done. You can now enqueue jobs and Reducks will do the rest.

API

reducksEpic() / rootEpic()

Property Type Description Required
action$ stream Observable Stream from redux-observable Yes
state$ stream Observable Stream from redux-observable Yes
props object Props object to pass store and config to Reducks Yes
props.store redux store Redux store to be observed for queued jobs Yes
props.config object Optional object to pass additional configuration options to Reducks No
props.config.onStart func Function called when a queue starts processing jobs No
props.config.onFinish func Function called when a queue finishes processing jobs No
epicMiddleware.run((action$, state$) => {
  return rootEpic(action$, state$, {
    store,
    config: {
      onStart: () => {
        // code that will fire when the queue starts
      },
      onFinish: () => {
        // code that will fire when the queue finshes
      }
    }
  });
});

add()

Property Type Description Required
name string Friendly name for the queued job. Useful primarily for debugging Yes
job func Async function to be called as a queued job. Receives the job object with a unique ID and friendly name. Yes
dispatch(
  addToQueue('myJob', async ({ job = {} }) => {
    const { id, name } = job;
    
    console.log(id, name);
    
    /* your async code here */
    
    await myAsyncCall();
  })
);

Support

Please create an issue in the issue tracker if you have a problem or need support. Please select the correct label when creating your issue (e.g. help wanted or bug).

Contributing

Contributions are welcome. Note that code style is enforced with prettier. Kindly adhere to this while making contributions.

Step 1: Fork this repo

Step 2: Start hacking

Step 3: Open a PR

Step 4: Profit πŸ’°πŸ’°πŸ’°

Available Scripts

In the project directory, you can run:

npm start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

npm test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

License

MIT License