From f810ce89c5495a65f577ae9394177fcc6d13a3fc Mon Sep 17 00:00:00 2001 From: Mattias Rydengren Date: Sat, 21 Nov 2015 21:12:14 +0100 Subject: [PATCH] Initial commit. --- .eslintrc | 3 +++ .gitignore | 4 ++++ README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++ modules/index.ava.js | 50 +++++++++++++++++++++++++++++++++++++++++++ modules/index.js | 19 +++++++++++++++++ package.json | 35 ++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 modules/index.ava.js create mode 100644 modules/index.js create mode 100644 package.json diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..ded0e56 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": ["billogram/browser"] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..909dac8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +*.log +lib/ +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c86377 --- /dev/null +++ b/README.md @@ -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 diff --git a/modules/index.ava.js b/modules/index.ava.js new file mode 100644 index 0000000..0c617cd --- /dev/null +++ b/modules/index.ava.js @@ -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(); +}); diff --git a/modules/index.js b/modules/index.js new file mode 100644 index 0000000..3efef30 --- /dev/null +++ b/modules/index.js @@ -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); + }; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0d4ee36 --- /dev/null +++ b/package.json @@ -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" + } +}