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-batch-actions

v1.0.7

Published

Batch Redux Actions.

Downloads

29

Readme

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.