Redux-Event-Emitter Middleware
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
;;;; // register an action creators to an eventconst ipc = reduxEventEmitter; // and/or if you want onceconst ipc2 = reduxEventEmitter; const store = ; // emit a message with arguments through the `emit` utility function// emit(channel, paramter)store; // disable itreduxEventEmitter;
Action
... { return type: 'IPC_PING' arg1 arg2 ;}...
Reducer
... { }...
Events
The key designates the events-emitter
channel; the value is a redux action
creator to be dispatched.
{ 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.
; store;
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; const store = ;
action function
...const receiveLocale = { }...
redux-thunk
?
What about 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;const store = ;
Questions
For any questions, please open an issue. Pull requests (with tests) are appreciated.