# redux-pure-subscribe

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

Latest version **0.1.4** (published 2017-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-pure-subscribe
pnpm add redux-pure-subscribe
yarn add redux-pure-subscribe
bun add redux-pure-subscribe
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2017-07-29 |
| First published | 2017-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Lucas Correia |
| Maintainers | tsirlucas |
| Keywords | redux, pure, subscribe, store, immutable |

## Links

- npm: https://www.npmjs.com/package/redux-pure-subscribe
- Repository: https://github.com/tsirlucas/redux-pure-subscribe
- Homepage: https://github.com/tsirlucas/redux-pure-subscribe#readme
- Issues: https://github.com/tsirlucas/redux-pure-subscribe/issues
- npm.io page: https://npm.io/package/redux-pure-subscribe

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.1.4 (latest) — 2017-07-29
- 0.1.3 — 2017-07-29
- 0.1.2 — 2017-07-29
- 0.1.1 — 2017-07-29
- 0.1.0 — 2017-07-27

## README

# Redux pure subscribe

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

[![Greenkeeper badge](https://badges.greenkeeper.io/tsirlucas/redux-pure-subscribe.svg)](https://greenkeeper.io/)
[![build status](https://img.shields.io/travis/tsirlucas/redux-pure-subscribe/master.svg)](https://travis-ci.org/tsirlucas/redux-pure-subscribe)
[![code climate](https://codeclimate.com/github/tsirlucas/redux-pure-subscribe/badges/gpa.svg)](https://codeclimate.com/github/tsirlucas/redux-pure-subscribe)
[![coveralls](https://img.shields.io/coveralls/tsirlucas/redux-pure-subscribe/master.svg)](https://coveralls.io/github/tsirlucas/redux-pure-subscribe)

Supports modern browsers and IE9+:

[![Browsers](https://saucelabs.com/browser-matrix/tsirlucas-rps.svg)](https://saucelabs.com/u/tsirlucas-rps)

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

#### Why?
As you can check [here](https://github.com/reactjs/redux/issues/303#issuecomment-125184409), 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](https://github.com/tsirlucas/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);

---
_Source: https://npm.io/package/redux-pure-subscribe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
