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-store-emitter

v1.0.0

Published

Redux-store with ability to listen to an actions

Downloads

12

Readme

Redux-store-emitter

Decoration for redux, that gives him the ability to distribute events.

Install

npm i redux-store-emitter -S

Usage

It used just like a redux.

import { createStore } from 'redux-store-emitter';
import rootReducer from './reducer';
const store = createStore(rootReducer);

Usage with Jest

Best way to use it with Jest is mock redux, by creating redux.js in __mocks__ directory.

// __mocks__/redux.js
export * from 'redux-store-emitter';

Then you can mock redux with jest:

jest.mock('redux');
import { createStore } from 'redux';

API

Decorator adds own methods to the function dispatch:

store.dispatch.on(actionName, callback):fn

Add a listener to the action. The callback function will be executed each time the target action will affect to the state.

Arguments:

  • actionName {string} Action name
  • callback {function} Event handler

Returns: {function} Listener destoyer

store.dispatch.on(
  ADD_TODO,
  action => console.log(action.todo)
);
store.dispatch(todo('Just do')); // <--
store.dispatch(todo('Just do it again')); // <--
// Console: Just do
// Console: Just do it again

An important fact is that the function will be called only when the action will trigger a subscription.

store.dispatch.after(actionName):promise

The promise will be resolved immediately if the action has already occurred or it will be resolved at the next action dispatch.

Arguments:

  • actionName {string} Action name

Returns: {promise}

store.dispatch(todo('Just do')); // <--
store.dispatch.after(
  ADD_TODO
).then(action => console.log(action.todo));
// Console: Just do
store.dispatch(todo('Just do it again'));

Or:

store.dispatch.after(
  ADD_TODO
).then(action => console.log(action.todo));
store.dispatch(todo('Just do')); // <--
store.dispatch(todo('Just do it again'));
// Console: Just do

store.dispatch.record(actionName, callback):fn

The callback function will be called for each action that has already occurred and for each all next.

Arguments:

  • actionName {string} Action name
  • callback {function} Event handler

Returns: {function} Listener destoyer

store.dispatch(todo('Just do')); // <--
store.dispatch(todo('Just do it again')); // <--
store.dispatch(todo('Do it always')); // <--
store.dispatch.record(
  ADD_TODO,
  action => console.log(action.todo)
);
store.dispatch(todo('Forever')); // <--
// Console: Just do
// Console: Just do it again
// Console: Do it always
// Console: Forever

store.dispatch.before(actionName, callback):fn

The callback function will be called once before the action will be passed to the reducer. Thus, you get a state without the effect of the action.

Arguments:

  • actionName {string} Action name
  • callback {function} Event handler

Returns: {function} Listener destroyer

const INCREASE = 'INCREASE';
const INIT = { count: 1 };
function rootReducer(state = INIT, action) {
  switch (action.type) {
    case INCREASE:
      return {
        count: state.count + 1
      };
    default:
      return state;
  }
}

const store = createStore(rootReducer);
store.dispatch.before(
  INCREASE,
  () => console.log('before', store.getState().count)
);
store.dispatch.after(INCREASE)
.then(() => console.log('after', store.getState().count));
store.dispatch({ type: INCREASE }); // <~~
// Console: before 1
// Console: after 2

store.dispatch.beforeEach(actionName, callback):fn

The callback function will be called for each time, before the action will be passed to the reducer.

Arguments:

  • actionName {string} Action name
  • callback {function} Event handler

Returns: {function} Listener destroyer

Works same as method before, but countless times.

store.dispatch.waitFor(actionName):promise

The callback function will be executed once when the action will affect the state.

Arguments:

  • actionName {string} Action name

Returns: {promise}

store.dispatch(todo('Do it'));
store.dispatch.waitFor(ADD_TODO)
.then(action => console.log);
store.dispatch(todo('Do it again')); // <--
// Console: Do it again

store.dispatch.clear()

Erases all actions history.

Author

Vladimir Kalmykov [email protected]

License

MIT