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

v1.1.6

Published

Redux Event Emitter Middleware

Downloads

25

Readme

Redux-Event-Emitter Middleware

GitHub license npm version

A Redux middleware to reduce only one line of code (you don't have to import specific action). Instead I change it to an emitter so that you can fire events with a simple api. Why so? I migrate some code from Electron to React Native so I want to keey the strusture of code unchanged. So this is a replacement (inspired) for Redux-Electron-IPC. With this libray, you may refactor your electron code and reat native code into a common shared code and refactor out Electron's IPC(possibly).

Install

npm

npm install --save redux-event-emitter

Working Demo Example

Check out the full demo application.

Usage

import { applyMiddleware, createStore } from 'redux';
import { reduxEventEmitter } from 'redux-event-emitter';
import { pingActionCreator } from './actions';
import { exampleReducer } from './reducer';

// register an action creators to an event
const ipc = reduxEventEmitter.createEvents({
  'ping': pingActionCreator, // receive a message
  ...
});

// and/or if you want once
const ipc2 = reduxEventEmitter.once({
  pongActionCreator, // another way of writing it if you don't want a different key
  ...
});

const store = createStore(exampleReducer, applyMiddleware(ipc, ipc2));

// emit a message with arguments through the `emit` utility function
// emit(channel, paramter)
store.dispatch(reduxEventEmitter.emit('ping', { a:1, b:3, c:5 }));

// disable it
reduxEventEmitter.off('ping');

Action

...
function pingActionCreator ( arg1, arg2 ) {

  return {
    type: 'IPC_PING',
    arg1,
    arg2
  };
}
...

Reducer

...
function exampleReducer (state = {}, action) {
  switch (action.type) {
    case 'IPC_PONG':
      console.log('Pong', action);
      return state;
    case 'IPC_PING':
      console.log('Ping', JSON.stringify(action)); 
      return state;
    default:
      return state;
  }
}
...

Events

The key designates the events-emitter channel; the value is a redux action creator to be dispatched.

{
  'channel name': (...args) => {
    return {
      type: 'YOUR_ACTION_TYPE',
      ... optional mapping of arguments ...
    }
  }
}

Examples

Sending an event

Use the utility function emit to issue an event-emitter message. The method signature is the same as ipcRenderer's send.

Behind the scenes, the middleware will trigger the tiny-emitter on the given channel with any number of arguments.

import { reduxEventEmitter } from 'redux-event-emitter';

store.dispatch(reduxEventEmitter.emit('event channel', ...args));

Receiving an reducers event

To receive events, register a channel response when configuring the middleware. e.g. include all your action functions into createEvents so that all can be called by using reduxEventEmitter.

example

const ipc = reduxEventEmitter.createEvents({
  receiveLocale, //<-- first action function
  lndSyncStatus, //<-- second action function
  ...
});

const store = createStore(exampleReducer, applyMiddleware(ipc));

action function

...
export const receiveLocale = (locale) => dispatch => {
  dispatch(setLocale(locale))
}
...

What about redux-thunk?

redux-event-emitter supports thunks out of the box as long as you install redux-thunk and apply the thunk middleware before the ipc middleware.

Example

const ipc = reduxEventEmitter.createEvents({
  'ipc channel name': () => dispatch =>
    dispatch({ type: 'DELAYED_ACTION_TYPE' })
});
const store = createStore(exampleReducer, applyMiddleware(thunk, ipc));

Questions

For any questions, please open an issue. Pull requests (with tests) are appreciated.