Skip to content

sgentle/redux-actionemitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Feb 8, 2016
33e0d20 · Feb 8, 2016

History

3 Commits
Feb 8, 2016
Feb 8, 2016
Feb 8, 2016
Feb 8, 2016
Feb 8, 2016
Feb 8, 2016
Feb 8, 2016

Repository files navigation

Redux Action Emitter

This is an experimental module for representing Redux actions as events. In this model, reducers become observers, switch statements become event listeners, and dispatch() emits events.

How to use it

npm install redux-actionemitter

Action Emitter has two parts: the eventReducer and the emitterMiddleware. You can use either in isolation, or both together.

emitterMiddleware

Attach the emitterMiddleware to your store like so:

import { createStore, applyMiddleware } from 'redux';
import { emitterMiddleware } from 'redux-actionemitter';

const applyStore = applyMiddleware(emitterMiddleware, logger)(createStore);

This lets you use an EventEmitter-compatible version of dispatch:

dispatch('EVENT_NAME', { data: 'goes here' });

Under the hood, it sets the type property of the object to the event name you provide. It's backwards-compatible, so you can still dispatch plain JS objects by providing only one argument.

eventReducer

Initialise your reducer with the initial state, then add event listeners for each action you want to listen to. Your event listener must return a value synchronously, just like a regular reducer.

import { eventReducer } from 'redux-actionemitter';

const reducer = eventReducer([]);

reducer.on('ADD_SNOWMAN', (state, action) => state.concat(['☃']));

export default reducer;

Much like emitterMiddleware, this looks at the type of the action object to trigger the event listeners.

About

Redux actions as event emitters

Resources

Stars

Watchers

Forks

Packages

No packages published