throttle-action

v0.1.0
Create a throttled Redux action
throttle action redux thunk lodash

Throttle Action NPM version

Create throttled redux-thunk actions. Throttled actions can be dispatched at most once per every wait milliseconds.

Installation

npm i --save throttle-action

Usage

import throttleAction from 'throttle-action';

const INCREMENT_COUNTER = 'INCREMENT_COUNTER';

function increment() {
  return {
    type: INCREMENT_COUNTER
  };
}

function incrementThunk() {
  return dispatch => {
    dispatch(increment());
  };
}

// wrap normal actions with throttleAction() to create throttled actions
const incrementThrottled = throttleAction(increment, 1000);
const incrementThunkThrottled = throttleAction(incrementThunk, 5000, {trailing: false});

// call throttled actions like normal redux actions
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

store.dispatch(incrementThrottled());
// --> INCREMENT_COUNTER dispatched immediately
store.dispatch(incrementThrottled());
// --> INCREMENT_COUNTER dispatched again after one second

store.dispatch(incrementThunkThrottled());
// --> INCREMENT_COUNTER dispatched immediately
store.dispatch(incrementThunkThrottled());
store.dispatch(incrementThunkThrottled());
// --> nothing dispatched

Uses lodash's throttle.

License

MIT

npm i throttle-action

Metadata

  • MIT
  • Whatever
  • Jeff Hanson
  • released 2/22/2018

Downloads

Maintainers