Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
sgentle committed Feb 8, 2016
1 parent 5bbe968 commit 9077fa9
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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:

```javascript
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`:
```javascript
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.

```javascript
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.

0 comments on commit 9077fa9

Please sign in to comment.