-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |