throttle-action

0.1.0 • Public • Published

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

Package Sidebar

Install

npm i throttle-action

Weekly Downloads

1,581

Version

0.1.0

License

MIT

Unpacked Size

5.97 kB

Total Files

7

Last publish

Collaborators

  • jshanson7