GithubHelp home page GithubHelp logo

gaearon / redux-batched-subscribe Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tappleby/redux-batched-subscribe

3.0 2.0 1.0 12 KB

store enhancer for https://github.com/rackt/redux which allows batching subscribe notifications.

JavaScript 100.00%

redux-batched-subscribe's Introduction

redux-batched-subscribe

build status npm version

Store enhancer for redux which allows batching of subscribe notifications that occur as a result of dispatches.

npm install --save redux-batched-subscribe

Usage

The batchedSubscribe store enhancer accepts a function which is called after every dispatch with a notify callback as a single argument. Calling the notify callback will trigger all the subscription handlers, this gives you the ability to use various techniques to delay subscription notifications such as: debouncing, React batched updates or requestAnimationFrame.

Since batchedSubscribe overloads the dispatch and subscribe handlers on the original redux store it is important that it gets applied before any other store enhancers or middleware that depend on these functions; The compose utility in redux can be used to handle this:

import { createStore, applyMiddleware, compose } from 'redux';
import { batchedSubscribe } from 'redux-batched-subscribe';

const finalCreateStore = compose(
  applyMiddleware(...middleware),
  batchedSubscribe((notify) => {
    notify();
  })
);

Note: since compose applies functions from right to left, batchedSubscribe should appear at the end of the chain.

The store enhancer also exposes a subscribeImmediate method which allows for unbatched subscribe notifications.

Examples

Debounced subscribe handlers:

import { createStore } from 'redux';
import { batchedSubscribe } from 'redux-batched-subscribe';
import debounce from 'lodash.debounce';

const batchDebounce = debounce(notify => notify());
const finalCreateStore = batchedSubscribe(batchDebounce)(createStore);
const store = finalCreateStore(reducer, intialState);

React batched updates

import { createStore } from 'redux';
import { batchedSubscribe } from 'redux-batched-subscribe';

// React <= 0.13
import { addons } from 'react/addons';
const batchedUpdates = addons.batchedUpdates;

// React 0.14
import { unstable_batchedUpdates as batchedUpdates } from 'react-dom';

const finalCreateStore = batchedSubscribe(batchedUpdates)(createStore)
const store = finalCreateStore(reducer, intialState);

Thanks

Thanks to Andrew Clark for the clean library structure.

redux-batched-subscribe's People

Contributors

tappleby avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rubythonode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.