redux-pure-subscribe

0.1.4 • Public • Published

Redux pure subscribe

A store subscribe function that checks if state has changed before trigger.

Greenkeeper badge build status code climate coveralls

Supports modern browsers and IE9+:

Browsers

Installing

npm install redux-pure-subscribe --save
yarn add redux-pure-subscribe

Why?

As you can check here, in Redux, store's subscribe method should not be used directly because it's a low level API. The store API is meant to be extensible, so Dan did it simple as possible. The thing is that your subscribe method will trigger everytime the dispatch method is called, not everytime the store changes. redux-pure-subscribe is a post-execution pull that checks if some part of your state has changed before triggers and then calls the cb function. OBS: We use a shallow comparison algorithm with object/array reference equality check (its fast!), so you may need some immutable lib like immutable-merge-operators

How?

I'ts actually very simple, you just need to import and use passing the store as first argument and a cb function as second.

import pureSubscribe from 'redux-pure-subscribe';

pureSubscribe(store, callback);

Current state as callback argument

redux-pure-subscribe passes the store current state as an argument to the cb function, so its easier for you to use it.

const callback = function(state) {
  const cats = state.myCats;
};

pureSubscribe(store, callback);

You can also use object destruction and make it funnier

const callback = function({myCats, myDogs, myBirds}) {
      const pets = {myCats, myDogs, myBirds}
    };
    
    pureSubscribe(store, callback);

Specific observer as third parameter

You can also pass an array of trees you want to observe for changes, so if your component should only trigger when specifics parts of your state changes, you can pass it as a third parameter

 pureSubscribe(store, callback, 'myCats'); // We also accept a strig in case you just want to observe one path

Auto first trigger to get initial state

redux-pure-subscribe will trigger the cb function at least once to get the initial state from store, so you wont need to do this anymore:

callback();
pureSubscribe(store, callback);

Package Sidebar

Install

npm i redux-pure-subscribe

Weekly Downloads

4

Version

0.1.4

License

MIT

Last publish

Collaborators

  • tsirlucas