npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-batched-dispatch

v1.0.3

Published

redux store enhancer to allow batched dispatch

Downloads

29

Readme

redux-batched-dispatch

Redux store enhancer to allow batched dispatch.

npm Build Status min minzip

Table of Contents

Installation

yarn:

yarn add redux-batched-dispatch

npm:

npm install --save redux-batched-dispatch

Usage

Batch Action

import { createBatchEnhancer }  from 'redux-batched-dispatch';
import { createStore } from 'redux';

const store = createStore(reducer, createBatchEnhancer());

// Batched dispatch will notify to listeners only once after store updated
store.dispatch([
  { type: 'Hello' },
  { type: 'World' },
]);

Rate Limited Dispatch

import { createBatchEnhancer }  from 'redux-batched-dispatch';
import { createStore } from 'redux';
import throttle from 'lodash.throttle';
import debounce from 'lodash.debounce';

function reducer(state = [], action) {
  switch (action.type) {
    case 'PUSH':
      return [...state, action.value];
    default:
      return state;
  }
}

function push(value) {
  return {
    type: 'PUSH',
    value,
  };
}

const store = createStore(
  reducer,
  createBatchEnhancer({
    // Invoke `dispatch`, but not more than once every 100ms.
    'DISPATCH_THROTTLE': dispatch => throttle(dispatch, 100, { leading: false }),
    // Invoke `dispatch`, debouncing 100ms
    'DISPATCH_DEBOUNCE': dispatch => debounce(dispatch, 100),
  }),
);

store.subscribe(() => {
  console.log(store.getState());
});

setTimeout(() => { store.dispatch(push('T1'), 'DISPATCH_THROTTLE') }, 0);
setTimeout(() => { store.dispatch(push('T2'), 'DISPATCH_THROTTLE') }, 20);
setTimeout(() => { store.dispatch(push('T3'), 'DISPATCH_THROTTLE') }, 40);
setTimeout(() => { store.dispatch(push('T4'), 'DISPATCH_THROTTLE') }, 260);
setTimeout(() => { store.dispatch(push('D1'), 'DISPATCH_DEBOUNCE') }, 0);
setTimeout(() => { store.dispatch(push('D2'), 'DISPATCH_DEBOUNCE') }, 70);
setTimeout(() => { store.dispatch(push('D3'), 'DISPATCH_DEBOUNCE') }, 140);
setTimeout(() => { store.dispatch(push('D4'), 'DISPATCH_DEBOUNCE') }, 300);
  
// stdout1:100ms => ['T1', 'T2', 'T3']
// stdout2:240ms => ['T1', 'T2', 'T3', 'D1', 'D2', 'D3']
// stdout3:360ms => ['T1', 'T2', 'T3', 'D1', 'D2', 'D3', 'T4']
// stdout4:400ms => ['T1', 'T2', 'T3', 'D1', 'D2', 'D3', 'T4', 'D4']

With Middlewares

You can use extra middlewares like redux-thunk, redux-saga, redux-observable, etc..

import { createBatchEnhancer }  from 'redux-batched-dispatch';
import { createStore, applyMiddleware } from 'redux';

const store = createStore(
  reducer,
  createBatchEnhancer(
    applyMiddleware(exampleMiddleware),
    {
      'DISPATCH_DEBOUNCE': dispatch => debounce(dispatch, 100),
    },
  )
);

Module Usage

ES6 module

import { createBatchEnhancer } from 'redux-batched-dispatch';

CommonJS

const { createBatchEnhancer } = require('redux-batched-dispatch');

Browser

Add below <script> tag to the HTML page right before the closing </body> tag

<script src="https://unpkg.com/redux-batched-dispatch@1/dist/index.js" crossorigin></script>

And use global reduxBatchedDispatch variable

Extra API

batchAction

batchAction(action, dispatchType)

You can also use batched dispatch with batchAction

Arguments

action (Array | Object): The redux action

dispatchType (string): The type of dispatch defined when use createBatchEnhancer

Example

import { batchAction }  from 'redux-batched-dispatch';

dispatch(batchAction([
  { type: 'Hello' },
  { type: 'World' },
], 'DISPATCH_THROTTLE'));

It is useful on middleware(ex: redux-saga)

function* createTodo() {
  yield put(batchAction([
    { type: 'Hello' },
    { type: 'World' },
  ], 'DISPATCH_THROTTLE'));
}

store.getActionQueue

store.getActionQueue(dispatchType)

Get queue which contains actions from dispatch. This queue's reference will never change

Arguments

dispatchType (string): The type of dispatch defined when use createBatchEnhancer

Example

const store = createStore(
  reducer,
  createBatchEnhancer({
    'DISPATCH_DEBOUNCE': dispatch => debounce(dispatch, 100),
  })
);

const throttleQueue = store.getActionQueue('DISPATCH_DEBOUNCE');

Guide

Cancel actions in the queue

const throttleQueue = store.getActionQueue('DISPATCH_DEBOUNCE');

// Clear all actions
throttleQueue.length = 0;

// Remove last action
throttleQueue.pop();

// Remove second action
throttleQueue.splice(1, 1);

Cancel rate limited dispatch

import { createBatchEnhancer }  from 'redux-batched-dispatch';
import { createStore } from 'redux';
import throttle from 'lodash.throttle';

let throttledDispatch;

const store = createStore(
  reducer,
  createBatchEnhancer({
    'DISPATCH_THROTTLE': dispatch => {
      throttledDispatch = throttle(dispatch, 100);
      return throttledDispatch;
    }
  }),
);

$(window).on('popstate', throttledDispatch.cancel);

Even if cancel the throttledDispatch, there are still actions in queue If want to clear the queue, see the section Cancel actions in the queue