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

v6.0.4

Published

Use the listener pattern with Redux middleware.

Downloads

103

Readme

Redux Listen

Use the listener pattern with Redux middleware.

Middleware

To add the middleware to your store:

const createReduxListen = require('redux-listen')
const listenStore = createReduxListen()
const store = createStore(reducer, applyMiddleware(listenStore.middleware))

addListener

To add a listener:

listenStore.addListener(SET_VAR, ({ action, getState, dispatch }) => {
  // ...
})

Now, whenever the action with the type SET_VAR dispatches, the middleware will call the function.

listenStore.addListener(/^FAIL_.*$/, ({ action, getState, dispatch }) => {
  // ...
})

You can also listen for actions where the action type matches a RegExp.

listenStore.addListener('*', ({ action, getState, dispatch }) => {
  // ...
})

A * listener will trigger on every action.

You may set multiple listeners to the same action. We will check and call listeners in the order received.

Don't be afraid to call getState often, it's basically free.

addListener will return fn -- the second argument -- back, so you can export the returned value for unit testing.

addListeners

To add many listeners:

// Or add many listeners
listenStore.addListeners({
  [SET_VAR]({ action, getState, dispatch }) {
    // ...
  },
  [SET_OTHER_VAR]({ action, getState, dispatch }) {
    // ...
  },
})

The advantage of adding using the "many" syntax is you get named functions for free. Also in this case, whenever the action with the type SET_VAR dispatches, the middleware will call the function.

listenStore.addListeners({
  [FETCH_USERS]({ dispatch }, done) {
    fetchUser({ id: '1' }).then(() => {
      dispatch({ type: FETCH_USERS_SUCCESS })
      dispatch({ type: FETCH_NOTICES })
      done()
    })
  },

  [FETCH_NOTICES]({ getState, dispatch }, done) {
    fetchNotices({ userToken: getState().userToken }).then(() => {
      dispatch({ type: FETCH_NOTICES_SUCCESS })
      done()
    })
  },
})

To chain network requests: dispatch an action when the first call is done, then listen for what you've dispatched. You can also condition your chaining based on action or state properties.

addListeners will return what you give back, so you can export the returned value for unit testing.

removeListeners

There's four ways to use removeListeners.

listenStore.removeListeners()

With no arguments, the middleware removes all listeners.

listenStore.removeListeners({ type: 'SET_VAR' })

With type, the middleware removes all listeners with the matching type.

listenStore.removeListeners({ fn: listenerFn })

With fn, the middleware removes all listeners with the same callback function.

listenStore.removeListeners({ type: 'SET_VAR', fn: listenerFn })

You can also use both type and fn to remove listeners that match BOTH -- but not only type or only fn.

REDUX_LISTEN_RESOLVE

Got some async going on, and need to know when you're done "asyncing"?

listenStore.addListener('SET_VAR', ({ action, getState, dispatch }, done) => {
  myPromise.then(() => {
    done()
  })
})

listenStore.addListener('REDUX_LISTEN_RESOLVE', () => {
  alert('We are done asyncing! Page ready!')
})

dispatch({ type: 'SET_VAR' })

There's a second real argument to the callback of addListener: done. If you ask for done, that means you have something async going on in that listener. Call done when that callback is totally finished. After you've called every done, we dispatch { type: 'REDUX_LISTEN_RESOLVE' }.

Don't call for done on a listener to REDUX_LISTEN_RESOLVE. If you do, it will never trigger.

isPending

listenStore.isPending()

You'll get a true if you still have something asyncing, and false if the middleware isn't waiting on anything.

redux-listen
Copyright 2018 Kevin Heis and [contributors](https://github.com/heiskr/redux-listen/graphs/contributors)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.