This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f810ce8
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["billogram/browser"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
*.log | ||
lib/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |