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-action-state-listen

v1.0.0

Published

Provides subscription of listeners on (partial) state change and action dispatching

Downloads

4

Readme

ReduxListen

Let you subscribe some callbacks on specific change on the state, or on specific action dispatching

Specifying parts of a nested object

In order to target a specific part of a nested object (such as store's state), use a string whith each step's key joined with a dot Example:

const initialState = {
  auth: {
    login: "xxxx",
    token: "yyyyy",
    userData: {
      urlToAvatar: "https://blablabla.jpg",
      nbPoints: 300,
    }
  }
  newComment: {
    content: "",
    target: -1,
  }
}

to acces the urlToAvatar the path will be "auth.userData.urlToAvatar". If the path is invalid a string will be thrown with the largest subpath that was accessible Example: accessing "auth.userData.userBio" will throw "auth.userData". Specifying an empty string will return the whole object.

Listening on state change

 const initialState = {
  auth: {
    login: "xxxx",
    token: "yyyyy",
    userData: {
      urlToAvatar: "https://blablabla.jpg",
      nbPoints: 300,
    }
  }
  newComment: {
    content: "",
    target: -1,
  }
}
.
.
.
let store = createStore(myReducer);
let listener = (newStateSlice) => {
  console.log("nbPoints changed", newStateSlice);
}
listenOnStateChange(store, "auth.userData.nbPoints", listener);

listenOnStateChange has the following signature :

  listenOnStateChange(store, path, callback, comp=shallow)
  • store : your redux store (returned by createStore)
  • path : str path to the part of the state you want to listen on
  • callback : function (newStateSlice) : function called when the listened-to part changes
  • comp : function (a, b) : function used to check if the val actually changed, default to shallow equality

Listening on action dispatching

In order to listen on actions, you should apply the actionListener middleware on your store

import {createStore, applyMiddleware} from 'redux';
import {actionListener, listenOnAction} from 'redux-listen';
.
.
.
let store = createStore(myReducer, applyMiddleware(listenOnAction));
const loggedCallback = (action) => {
  console.log("Logged in!");
}
listenOnAction((action) => {
  return action.type === TRY_LOGIN && action.payload === "SUCCESS";
},  loggedCallback);

listenOnAction has the following signature

  listenOnAction(actionMatcher, listener)
  • actionMatcher : function (action) : this function is given every action that's dispatched, if the function returns true, the listener will be called
  • listener : function (action) : callback that is called when an action match the corresonding actionMatcher.

Removing listener's

Wether you have created an action listener or a state listener, both function return a callable, simply call it to remove your listener

Danger of calling store.dispatch in your listener

If you are dispatching an action in one of your listener watch out that that don't trigger again your listener. This would cause an infinite call cycle which will crash your app

Do not mutate state

As redux recommends it, don't mutate the state in your reducer's. This is especially true if you listen on some slice of the state, your listeners won't be called.