Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrydengren committed Nov 21, 2015
0 parents commit f810ce8
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["billogram/browser"]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
*.log
lib/
node_modules/
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# redux-batch-middleware

> Batch Redux actions
[![npm version](https://img.shields.io/npm/v/redux-batch-middleware.svg?style=flat-square)](https://www.npmjs.com/package/redux-batch-middleware)

Batch [middleware](http://rackt.github.io/redux/docs/advanced/Middleware.html) for Redux. Inspired by [redux-batched-actions](https://github.com/tshelburne/redux-batched-actions).

## Install

```
npm install --save redux-batch-middleware
```

## Usage

Add as middleware:

```js
import { applyMiddleware, createStore } from 'redux';
import { batch, batching } from 'redx-batch-actions';
import reducers from './reducers';

let middleware = [batch];

let store = applyMiddleware(...middleware)(createStore)(batching(reducers));
```

Dispatch multiple actions:

```js
store.dispatch([action1, action2]);
```

## API

### batch

Redux middleware which converts a dispatched array of actions to a batch action.

### batching(reducer)

#### reducer

Type: `function`

A reducer that should be able to handle batched actions, most likely the root reducer.

## License

MIT
50 changes: 50 additions & 0 deletions modules/index.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import test from 'ava';
import { applyMiddleware, combineReducers, createStore } from 'redux';

import { batch, batching } from '../lib/index';


function createBatchStore() {
let middleware = [
batch
];

let root = combineReducers({
actions: function(state = [], action = {}) {
if (action.type === '@@redux/INIT') {
return state;
}

return [...state, action];
}
});

return applyMiddleware(...middleware)(createStore)(batching(root));
}


test('can dispatch non-batch action', (t) => {
let type = 'action type';

let store = createBatchStore();
store.dispatch({ type });

let actions = store.getState().actions;
t.same(actions.map((action) => action.type), [type]);

t.end();
});


test('can dispatch batch action', (t) => {
let type1 = 'action type 1';
let type2 = 'action type 2';

let store = createBatchStore();
store.dispatch([{ type: type1 }, { type: type2 }]);

let actions = store.getState().actions;
t.same(actions.map((action) => action.type), [type1, type2]);

t.end();
});
19 changes: 19 additions & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const BatchActionType = '@@redux-batch-middleware/BATCH';


export function batch({ dispatch }) {
return (next) => (action) => {
Array.isArray(action)
? dispatch({ type: BatchActionType, payload: action })
: next(action);
};
}


export function batching(reducer) {
return function batcher(state, action) {
return action.type === BatchActionType
? action.payload.reduce(batcher, state)
: reducer(state, action);
};
}
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "redux-batch-middleware",
"version": "0.0.0",
"description": "Batch redux actions",
"license": "MIT",
"keywords": [
"Redux",
"Batch"
],
"repository": "mrydengren/redux-batch-middleware",
"main": "lib/index",
"jsnext:main": "modules/index",
"files": [
"lib",
"modules"
],
"scripts": {
"clean": "rimraf ./lib",
"build": "npm run clean && ./node_modules/.bin/babel ./modules -d lib --ignore *.ava.js",
"pretest": "./node_modules/.bin/eslint ./modules",
"test": "npm run build && ./node_modules/.bin/ava ./modules/**/*.ava.js",
"preversion": "npm test",
"prepublish": "npm run build"
},
"dependencies": {},
"devDependencies": {
"ava": "^0.5.0",
"babel": "^5.8.34",
"babel-eslint": "^4.1.5",
"eslint": "^1.10.1",
"eslint-config-billogram": "^0.1.0",
"redux": "^3.0.4",
"rimraf": "^2.4.4"
}
}

0 comments on commit f810ce8

Please sign in to comment.