Skip to content

Commit

Permalink
feat(applyMiddleware): create optimizing applyMiddleware function
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Mar 15, 2017
1 parent 0447d0e commit 4ed74ed
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ Composes the given middlewares to be called one after another, just like Redux'
optimization: sequences of consecutive middleware that have `actionHandlers` will be recombined into a single middleware
that calls any `actionHandlers` for a given action directly.

## applyMiddleware(...middlewares: Middleware[]): createStore => createStore'
**Requires `redux` as an optional dependency.**
```js
import applyMiddleware from 'mindfront-redux-utils/lib/applyMiddleware';
```

Just like `applyMiddleware` from `redux`, but applies the same optimization as `composeMiddleware`: sequences of
consecutive middleware that have `actionHandlers` will be recombined into a single middleware that calls any
`actionHandlers` for a given action directly.

## combineMiddlewareWithActionHandlers(...middlewares: Middleware[]): Middleware[]
```js
import {combineMiddlewareWithActionHandlers} from 'mindfront-redux-utils';
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"mocha": "^3.2.0",
"nyc": "^10.0.0",
"pre-commit": "^1.1.3",
"redux": "^3.6.0",
"rimraf": "^2.6.1",
"semantic-release": "^6.3.2",
"sinon": "^1.17.6"
Expand All @@ -86,6 +87,9 @@
"lodash.reduce": "^4.6.0",
"lodash.size": "^4.2.0"
},
"optionalDependencies": {
"redux": "^3.6.0"
},
"pre-commit": [
"lint",
"flow"
Expand Down
7 changes: 7 additions & 0 deletions src/applyMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {combineMiddlewareWithActionHandlers} from './composeMiddleware'
import {applyMiddleware as naiveApplyMiddleware} from 'redux'

export default function applyMiddleware(...middlewares) {
return naiveApplyMiddleware(...combineMiddlewareWithActionHandlers(...middlewares))
}

91 changes: 91 additions & 0 deletions test/applyMiddlewareTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {expect} from 'chai'
import {createStore} from 'redux'
import createMiddleware from '../src/createMiddleware'
import applyMiddleware from '../src/applyMiddleware'

describe('applyMiddleware', () => {
it('composes a mix of middleware with and without actionHandlers properly', () => {
let index = 0
let callsA1 = []
let callsA2 = []
let callsA3 = []
let callsB1 = []
let callsB2 = []
let callsC1 = []
let calls3 = []
let middlewareA1 = store => next => action => callsA1.push({action, index: index++}) && next(action)
let middlewareA2 = store => next => action => callsA2.push({action, index: index++}) && next(action)
let middlewareA3 = store => next => action => callsA3.push({action, index: index++}) && next(action)
let middlewareB1 = store => next => action => callsB1.push({action, index: index++}) && next(action)
let middlewareB2 = store => next => action => callsB2.push({action, index: index++}) && next(action)
let middlewareC1 = store => next => action => callsC1.push({action, index: index++}) && next(action)

let middleware1 = createMiddleware({
a: middlewareA1,
})
let middleware2 = createMiddleware({
a: middlewareA3,
b: middlewareB1,
c: middlewareC1,
})
let middleware3 = store => next => action => calls3.push({action, index: index++}) && next(action)
let middleware4 = createMiddleware({
a: middlewareA2,
b: middlewareB2,
})
let callsNext = []

const store = createStore(
(state, action) => {
callsNext.push({action, index: index++})
return state
},
{},
applyMiddleware(middleware1, middleware2, middleware3, middleware4)
)
// reset index since createStore dispatched a dummy action
index = 0
callsNext = []

let action = {type: 'a'}
store.dispatch(action)
expect(callsA1).to.deep.equal([{action, index: 0}])
expect(callsA3).to.deep.equal([{action, index: 1}])
expect(calls3).to.deep.equal([{action, index: 2}])
expect(callsA2).to.deep.equal([{action, index: 3}])
expect(callsB1).to.deep.equal([])
expect(callsB2).to.deep.equal([])
expect(callsC1).to.deep.equal([])
expect(callsNext).to.deep.equal([{action, index: 4}])

index = 0
callsA1 = []
callsA2 = []
calls3 = []
callsNext = []
action = {type: 'b'}
store.dispatch(action)
expect(callsA1).to.deep.equal([])
expect(callsA2).to.deep.equal([])
expect(callsB1).to.deep.equal([{action, index: 0}])
expect(calls3).to.deep.equal([{action, index: 1}])
expect(callsB2).to.deep.equal([{action, index: 2}])
expect(callsC1).to.deep.equal([])
expect(callsNext).to.deep.equal([{action, index: 3}])

index = 0
callsB1 = []
callsB2 = []
calls3 = []
callsNext = []
action = {type: 'c'}
store.dispatch(action)
expect(callsA1).to.deep.equal([])
expect(callsA2).to.deep.equal([])
expect(callsB1).to.deep.equal([])
expect(callsB2).to.deep.equal([])
expect(callsC1).to.deep.equal([{action, index: 0}])
expect(calls3).to.deep.equal([{action, index: 1}])
expect(callsNext).to.deep.equal([{action, index: 2}])
})
})

0 comments on commit 4ed74ed

Please sign in to comment.