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

v1.1.1

Published

Redux middleware to support mediator pattern

Downloads

28

Readme

redux-mediator

NPM version Build Status Coverage Status

Redux middleware to support mediator pattern. Helps you to reduce complexity of large redux applications by separating your codebase into independent modules which know nothing about each other. Communication between modules is encapsulated within a mediator middleware. Modules no longer use actions of each other directly, but instead communicate through the mediator. Mediator maps output actions of one module to input actions of another. This reduces the dependencies between communicating objects, thereby reducing coupling.

Install

npm install redux-mediator --save

Usage

The default export is a function - creator of middleware. It accepts one Object parameter - action mapper. Keys of this object is action types, values - descriptors of action transformation.

Transformation descriptor fields:

  • type [any] - type of mapped action (required*);
  • create [function] - action creator (required*);
  • predicate [function] - predicate to check if action should be mapped (optional).
  • suppress [boolean] - flag, which indicates if initial action should be suppressed (optional).

* user have to specify one of type or create. If create is specified, then type will be ignored.

Action creator:

create(action, state)
  • action - initial action;
  • state - current redux state (result of store.getState() call).

Predicate:

predicate(action, state)
  • action - initial action;
  • state - current redux state (result of store.getState() call).

Example:

import createMediator from 'redux-mediator';

const mediator = createMediator({
    /* For each action with type 'action1.in' */
    'action1.in': {
        /*
          create new action with type 'action1.out'
          with payload 'dst' populated from initial action field 'src'
        */
        create: (action, state) => ({
            type: 'action1.out',
            dst: action.src
        }),
        suppress: true // suppress initial action
        /*
          Initial action will be suppressed. Dispatching of action
          'action1.in' will actually dispatch action 'action1.out'
        */
    },
    
    /* For each action with type 'action2.in' */
    'action2.in': {
        /*
          create new action with type 'action2.out' and the same
          payload as in initial action
        */
        type: 'action2.out'
        /*
          Initial action won't be suppressed. Dispatching of action
          'action2.in' will actually dispatch two actions:
          'action2.in' and 'action2.out'
        */
    },
    
    /* For each action with type 'cond.in' which satisfies predicate */
    'cond.in': {
        /*
          create new action with type 'cond.out'
          with payload 'dst' populated from initial action field 'src'
        */
        create: (action, state) => ({
            type: 'cond.out',
            dst: action.src
        }),
        predicate: (action, state) => action.value > 0,
        suppress: true // suppress initial action
        /*
          If predicate returns true for initial action it will be suppressed
          and new action with type 'cond.out' will be dispatched.
          If predicate returns false for initial action it will be dispatched.
        */
    },
    
    /*
      Also you can map action to an array of descriptors.
    */
    'multi.in' : [
        { type: 'multi.out.1', suppress: true },
        { create: (action, state) => ({
          type: 'multi.out.2',
          dst: action.src + 'bar'
        }) },
        { create: (action, state) => ({
          type: 'multi.out.3',
          dst: action.src
        }) }
    ]
});

Changelog

version 1.1.1:

  • fixed the order of dispatching

version 1.1.0:

  • added support for multi descriptors

version 1.0.1:

  • first stable version

License

MIT