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

v1.0.6

Published

Watch actions dispatched to redux.

Downloads

13,242

Readme

redux-action-watch

Build Status Coverage Status

redux-action-watch provides feature to listen action dispatched to redux.

  • You can watch for a redux action dispatch.
  • Provides some helpers to register you own function as watcher like, onAction, onActionOnce, subscribeActions.
  • It can act as IPC (inter-process communication) b/w components.

I don't think, we should only dispatch action to make changes in flux/redux state. Action can either change state or acknowledge that something happen.

Installation

You should install it as dependency:

$ npm install --save redux-action-watch

Setup with redux

app.js / index.js

import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk'; // optional
import reducers from './reducers';
import actionWatchMiddlewaregenerator from 'redux-action-watch/lib/middleware';
import actionWatchReducer from 'redux-action-watch/lib/reducer';
// import { middleware, reducer } from 'redux-action-watch';

const initialState = {};
// Important! name redux-action-watch reducer in your redux.
const actionWatcherStateName = 'watcher';
const reducersWithActionWatchReducer = Object.assign({}, reducers, {
    [actionWatcherStateName]: actionWatchReducer,
});

// generate middleware
const actionWatchMiddleware = actionWatchMiddlewaregenerator(actionWatcherStateName);

const store = createStore(
  combineReducers(reducersWithActionWatchReducer),
  initialState,
  compose(
    /** configure middlewares with redux */
    applyMiddleware(actionWatchMiddleware, thunk),
    /** optional, redux dev tool */
    window.devToolsExtension ? window.devToolsExtension() : f => f
  )
)

How to use

Container

import { connect } from 'react-redux';
import {
    onAction,   // takes a action and a listener.
    onActionOnce,   // takes a action and a listener, will called only once.
    subscribeActions,   // takes multiple actions and listners.
} from 'redux-action-watch/lib/actionCreators';

const mapPropsToDispatch = dispatch => ({
    // bind dispatch function
    subscribeActions: subscribeActions(dispatch),
    unsubscribeActions: unsubscribeActions(dispatch),
    onActionOnce: onActionOnce(dispatch),
});

export default connect(null, mapPropsToDispatch)(Component);

Component

import React from 'react';
import { LOGIN_SUCCEEDED, LOGIN_FAILED } from './../actions';
import Notifier from './notifier';

class LoginForm extends React.Component {

    componentDidMount() {
        this.unsubscribe = this.props.onAction(LOGIN_FAILED, (action) => Notifier.show(action.error));
        /*
        --------------------------or---------------------------------
        this.watchMeta = {
            ACTION_A: fn1,
            ACTION_B: [fn2, fn3];
        };
        this.unsubscribe = this.props.subscribeAction(this.watchMeta);
        --------------------------or---------------------------------
        // if you want to auto unsubscribe after once call.
        this.props.onActionOnce(ACTION_A, callMeOnceFunction);
        */
    }
    
    componentWillUnmount() {
        this.unsubscribe();
    }
    
    render() {
        // Your code ........
    }

}

Documentation

Note:- All functions should first call with dispatch

  • subscribeAction(dispatch)(listenersObj)
    It can register watcher for more than one actions.
    Argument
    listenersObj: It should be an object where keys will be action type and value will be listener or array of listeners. Example: { ACTION_A: func1, ACTION_B: [func2, func3] }.
    Returns
    unsubscribeFunc: It returns function. Which should invoke to unsubscribe those listeners.

  • unsubscribeAction(dispatch)(listenersObj)
    It can un-subscribe actions which subscribe by subscribeAction function. It takes same listenersObj used at time of subscription.
    Argument
    listenersObj: Same as above.

  • onAction(dispatch)(actionType, listener)
    It can register a watcher/listener for a action.
    Arguments
    actionType: Type/name of action. Example, const action = { type: ACTION_A }. Here ACTION_A is actionType.
    listener: Function which will be invoke on action dispatch.
    Returns
    unsubscribeFunc: It returns function. Which should invoke to unsubscribe that listener.

  • onActionOnce(dispatch)(actionType, listener)
    It can register a watcher/listener for a action. And it will automatically un-subscribe after once invoke.
    Arguments
    actionType: Type/name of action. Example, const action = { type: ACTION_A }. Here ACTION_A is actionType.
    listener: Function which will be invoke on action dispatch.
    Returns
    unsubscribeFunc: It returns function. You can unsubscribe without once invoke.

Development

Want to contribute? Great!

  • Clone this repo
  • Make changes
  • Run test case npm run test
  • Create pull request

Todos

  • Write example application

License

MIT