0.0.7 • Published 6 years ago
redux-count-dispatch-middleware v0.0.7
redux-count-dispatch-middleware
A Redux middleware to count dispatch.
install
$ npm i redux-count-dispatch-middleware -S
# or
$ yarn add redux-count-dispatch-middleware
usage
import { createStore, applyMiddleware, combineReducers } from 'redux'
import {
createCountDispatchMiddleware,
countDispatchReducer,
} from 'redux-count-dispatch-middleware'
/**
* matcher
* @param {string} type type
* @param {object} action action
* @return {string|false} counter key or false for no count
*/
const filter = (type, action) =>
type === 'noop' ? false : 'dispatched ' + type
const initialState = {}
const middlewares = [createCountDispatchMiddleware({ filter })]
const store = createStore(
combineReducers({
dispatchCounter: countDispatchReducer,
}),
initialState,
applyMiddleware(...middlewares)
)
store.dispatch({ type: 'hello' })
store.dispatch({ type: 'hello' })
store.dispatch({ type: 'world' })
store.dispatch({ type: 'noop' })
store.getState().dispatchCounter // { 'dispatched hello': 2, 'dispatched world': 1 }
development
$ git clone https://github.com/kamataryo/redux-count-dispatch-middleware.git
$ cd redux-count-dispatch-middleware
$ npm test