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-watcher

v1.0.1

Published

State watcher for Redux.

Downloads

250

Readme

redux-watcher

NPM version travis

Watch Redux state changes.

Example

import store from './store'
import ReduxWatcher from 'redux-watcher'

const watcher = new ReduxWatcher(store)
watcher.watch('user', logChange) // watch `store.getState().user`
watcher.watch(['user', 'name'], logChange) // watch `store.getState().user.name`

watcher.setCompareFunction('user', (a, b) => a === b) // use custom compare function

setTimeout(() => {
	watcher.off('user', logChange)
	watcher.off(['user', 'name'], logChange)
}, 5000)

function logChange({ store, selector, prevState, currentState, prevValue, currentValue }) {
	console.log(`${selector.join('.')} changed from ${prevValue} to ${currentValue}.`)
}

store.dispatch({
  type: 'SET_USER_NAME',
  name: 'Jack'
})

Installation

npm i --save redux-watcher

You can include the script directly. (not recommended)

<script src="node_modules/redux-watcher/dist/redux-watcher.browser.min.js"></script>

Or import as a module:

import ReduxWatcher from 'redux-watcher'
const ReduxWatcher = require('redux-watcher')

Usage

ReduxWatcher(store)

Create a new watcher.

ReduxWatcher.prototype.watch(selector, listener)

Watch the value of matching state.

  • selector: An Array or String of the path to target state. e.g. user or ['user'] for store.getState().user; ['user', 'name'] for store.getState().user.name
  • listener: A Function called when target state changes. An Object contains following properties will be passed as the parameter:
    • store: Redux Store which the watcher bound to.
    • selector: Same as the selector above.
    • prevState: Previous state before store updating.
    • currentState: Current state of store.
    • prevValue: Previous value of the target state before store updating.
    • currentValue: Current value of the target state.

Note: Value of state will be compared deeply.

ReduxWatcher.prototype.off(selector, listener)

Stop watching the matching state.

  • selector: The selector you watched before.
  • listener: Listener for the selector.

ReduxWatcher.select(state, selector) -> value

Return value of the state according to the selector.

  • state: A state object.
  • selector: Selector for the target.
  • value: Value of the state.

ReduxWatcher.prototype.setCompareFunction(selector, isEqual)

Use custom compare function.

  • selector: The selector to use custom compare function.
  • isEqual: A Function to determine if previous value equals to current value. Previous value and current value will be passed as the parameter.

ReduxWatcher.prototype.clearCompareFunction(selector)

Remove custom compare function and use the default one(deep equal).

  • selector: The selector to use default compare function.

License

MIT