Redux Debounced Middleware

0.5.0 · maintenance · verified Wed Apr 22

Redux-debounced is a middleware for Redux designed to manage the dispatching of fast-paced actions. It enables developers to debounce actions by adding specific metadata to the action object, ensuring that the Redux state is updated only after a defined period of inactivity following the last dispatch of a particular action. This functionality is crucial for optimizing performance in scenarios like search input fields, where continuous user input could otherwise trigger an excessive number of API requests or state changes. The package is currently at version 0.5.0, with its last publish date in April 2018, which suggests it is in a maintenance state with no active development or regular release cadence. Its primary distinction lies in its direct integration within the Redux middleware chain, utilizing Flux Standard Actions (FSA) for configuration, offering an alternative to debouncing at the component level or within action creators.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `redux-debounced` middleware with `redux-thunk` and dispatch debounced actions, including how to configure a unique key for thunks to enable proper debouncing and cancellation.

import { createStore, applyMiddleware, combineReducers } from 'redux';
import type { Action, Middleware } from 'redux';
import createDebounce from 'redux-debounced';
import thunkMiddleware from 'redux-thunk';

// Minimal reducer for example
interface AppState { searchKey: string; };
const initialState: AppState = { searchKey: '' };
const rootReducer = (state: AppState = initialState, action: Action): AppState => {
  switch (action.type) {
    case 'TRACK_CUSTOMER_SEARCH':
      console.log('Reducer received TRACK_CUSTOMER_SEARCH with key:', (action as any).key);
      return { ...state, searchKey: (action as any).key };
    default:
      return state;
  }
};

// Action creator for a debounced thunk
interface DebouncedThunkAction extends Action {
  meta?: { debounce?: { time: number; key: string; } };
  (dispatch: Function, getState: Function): void;
}

export function trackCustomerSearch(key: string): DebouncedThunkAction {
  const thunk = ((dispatch: Function) => {
    console.log(`Simulating API call for key: ${key}`);
    dispatch({ type: 'TRACK_CUSTOMER_SEARCH', key });
  }) as DebouncedThunkAction;

  thunk.meta = {
    debounce: {
      time: 2500, // Debounce for 2.5 seconds
      key: 'TRACK_CUSTOMER_SEARCH' // Must specify a key for thunks
    }
  };
  return thunk;
}

// Create the Redux store with middleware
const store = createStore(
  rootReducer,
  applyMiddleware(createDebounce() as Middleware, thunkMiddleware)
);

// Dispatch debounced actions
console.log('Dispatching search 1 (will be cancelled)');
store.dispatch(trackCustomerSearch('apple'));

setTimeout(() => {
  console.log('Dispatching search 2 (will trigger after 2.5s from now)');
  store.dispatch(trackCustomerSearch('apricot'));
}, 500);

setTimeout(() => {
  console.log('Dispatching search 3 (will trigger after 2.5s from now, cancelling previous)');
  store.dispatch(trackCustomerSearch('orange'));
}, 1000);

view raw JSON →