Socket
Socket
Sign inDemoInstall

redux-batch-actions

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redux-batch-actions

Batch Redux Actions.


Version published
Weekly downloads
9
increased by200%
Maintainers
1
Install size
1.38 MB
Created
Weekly downloads
 

Readme

Source

Redux Batch Middleware Build Status

Batch Redux actions.

NPM

Installation

% npm install redux-batch-actions

Usage

import { createStore } from 'redux';
import reducify from 'reducify';
import batchMiddleware from 'redux-batch-actions';


const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware({
    "GRAPH": true
  }))
);

store.dispatch({
   type: 'GRAPH',
   data: {foo: 'bar'}
});

store.dispatch({
   type: 'GRAPH',
   data: {fuz: 'bus'}
});

// on next tick:
// State is: {foo: 'bar', fuz: 'bus'}

Merge

By default, batch-actions merges your data objects into a single action.

If you want to customize this, include it as a configuration with your middleware.

import { createStore } from 'redux';
import reducify from 'reducify';
import batchMiddleware from 'redux-batch-actions';

const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware({
    "GRAPH": {
        merge: (state, part) => ({...state, ...part, myProp: {...state.myProp, ...part.myProp}})
    }
  }))
);

store.dispatch({
   type: 'GRAPH',
   myProp: {foo: 'bar'}
});

store.dispatch({
   type: 'GRAPH',
   myProp: {fuz: 'bus'}
});

// on next tick:
// Action is: {type: 'GRAPH', myProp: {foo: 'bar', fuz: 'bus'}}

If you pass in a function as your type config, we'll assume that it's the merge function

import { createStore } from 'redux';
import reducify from 'reducify';


const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware({
    "GRAPH": (state, part) => ({...state, ...part, myProp: {...state.myProp, ...part.myProp}})
  }))
);

// same as above

Batch

If you don't include the action type when you configure your middleware, just include batch as an argument with your action type

import { createStore } from 'redux';
import reducify from 'reducify';
import batchMiddleware from 'redux-batch-actions';

const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware())
);

store.dispatch({
   type: 'GRAPH',
   data: {foo: 'bar'},
   batch: true
});

store.dispatch({
   type: 'GRAPH',
   data: {fuz: 'bus'},
   batch: true
});

// on next tick:
// State is: {foo: 'bar', fuz: 'bus'}

You can also pass in batch config this way.

import { createStore } from 'redux';
import reducify from 'reducify';


const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware())
);

store.dispatch({
   type: 'GRAPH',
   myProp: {foo: 'bar'},
   batch: (state, part) => ({...state, ...part, myProp: {...state.myProp, ...part.myProp}})
});

store.dispatch({
   type: 'GRAPH',
   myProp: {fuz: 'bus'},
   batch: (state, part) => ({...state, ...part, myProp: {...state.myProp, ...part.myProp}})
});

// on next tick:
// Action is: {type: 'GRAPH', myProp: {foo: 'bar', fuz: 'bus'}}

Batch Complete

Want to ensure the batch fires synchronously? You'll need to dispatch the batched action with one additional property: batchComplete.

import { createStore } from 'redux';
import reducify from 'reducify';
import batchMiddleware from 'redux-batch-actions';

const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware({
    "GRAPH": true
  }))
);

store.dispatch({
   type: 'GRAPH',
   data: {foo: 'bar'}   
});

store.dispatch({
   type: 'GRAPH',
   data: {fuz: 'bus'}
});

store.dispatch({
   type: 'GRAPH',
   batchComplete: true
});

// Action is: {type: 'GRAPH', data: {foo: 'bar', fuz: 'bus'}}

Batch Purge

Want to stop a batch from going out? Add batchPurge to an action and we'll clear it.

import { createStore } from 'redux';
import reducify from 'reducify';
import batchMiddleware from 'redux-batch-actions';

const store = createStore(
  reducify({
    "GRAPH": (state = 0, action) => action.data
  }),
  {},
  applyMiddleware(batchMiddleware({
    "GRAPH": true
  }))
);

store.dispatch({
   type: 'GRAPH',
   data: {foo: 'bar'}   
});

store.dispatch({
   type: 'GRAPH',
   data: {fuz: 'bus'}
});

store.dispatch({
   type: 'GRAPH',
   batchPurge: true
});

// No Action fired on current tick or next

Configuration

Tick

This batching relies on a tick function, that is - any function that we know will fire next time there is a javascript event loop.

By default, we use setTimeout([fn], 1) on the browser and process.nextTick([fn]) on the server. You can change this, however, by passing it in as an option when you declare your middleware.

const store = createStore(
  reducify({}),
  {},
  applyMiddleware(batchMiddleware({tick: requestAnimationFrame}))
);
Finalize

Once a batch is going to be committed, we'll run it through a finalize function. This defaults to _.identity.

const store = createStore(
  reducify({}),
  {},
  applyMiddleware(batchMiddleware({finalize: action => {...action, hi: 'mom'}}))
);

store.dispatch({
   type: 'GRAPH',
   batch: true,
   data: {fuz: 'bus'}
});


// on next tick:
// Action is: {type: 'GRAPH', data: {fuz: 'bus'}, hi: 'mom'}

If you pass a function to batchComplete, it will be used to finalize.

const store = createStore(
  reducify({}),
  {},
  applyMiddleware(batchMiddleware())
);

store.dispatch({
   type: 'GRAPH',
   batch: true,
   data: {fuz: 'bus'}
});


store.dispatch({
   type: 'GRAPH',
   batchComplete: action => {...action, hi: 'mom'}
});


// Action is: {type: 'GRAPH', data: {fuz: 'bus'}, hi: 'mom'}

Credits

Redux Batch Middleware is free software under the MIT license. It was created in sunny Santa Monica by Matthew Drake.

Keywords

FAQs

Last updated on 20 Aug 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc