项目作者: mariotacke

项目描述 :
Redux Electron IPC Middleware
高级语言: JavaScript
项目地址: git://github.com/mariotacke/redux-electron-ipc.git
创建时间: 2016-05-31T23:27:38Z
项目社区:https://github.com/mariotacke/redux-electron-ipc

开源协议:MIT License

下载


Redux Electron IPC Middleware

Build Status npm version Coverage Status GitHub license

A Redux middleware to reduce code around ipc
calls in an Electron application. You can send and
receive IPC
events with a simple api.

Install

npm

  1. npm install --save redux-electron-ipc

Usage

Check out the full demo
application.

Window

  1. import { applyMiddleware, createStore } from 'redux';
  2. import createIpc, { send } from 'redux-electron-ipc';
  3. import { pongActionCreator } from './actions';
  4. import { exampleReducer } from './reducer';
  5. // register an action creator to an ipc channel (key/channel, value/action creator)
  6. const ipc = createIpc({
  7. 'pong': pongActionCreator, // receive a message
  8. ...
  9. });
  10. const store = createStore(exampleReducer, applyMiddleware(ipc));
  11. // send a message with arguments through the `send` utility function
  12. store.dispatch(send('ping', 'redux', 'electron', 'ipc'));

Main

  1. // your regular ipc setup
  2. const electron = require('electron');
  3. const { ipcMain } = electron;
  4. ...
  5. // pong event with arguments back to caller
  6. ipcMain.on('ping', (event, ...args) => {
  7. console.log('Ping', ...args);
  8. event.sender.send('pong', ...args);
  9. });

API

redux-electron-ipc has a default constructor function for creating ipc
middleware, and a named send utility function.

  1. createIpc(events?: Object) => IpcMiddleware
  2. send(channel: string, ...arg1?: Object, arg2?: Object, ..., argN?:Object) => Action

Events

Each key on the events object (default: {}) registers a single ipc channel
response. The key designates the ipc channel; the value is a redux action
creator to be dispatched.

  1. {
  2. 'ipc channel name': (event, ...args) => {
  3. return {
  4. type: 'YOUR_ACTION_TYPE',
  5. ... optional mapping of arguments ...
  6. }
  7. }
  8. }

Examples

Sending an IPC event

Use the utility function send to issue an ipc message to the main thread. The
method signature is the same as ipcRenderer’s send.

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

  1. import { send } from 'redux-electron-ipc';
  2. store.dispatch(send('ipc event channel', ...args));

Receiving an IPC event

To receive events, register a channel response when configuring the middleware.

  1. const ipc = createIpc({
  2. 'channel to listen to': () => {
  3. return {
  4. type: 'IPC_RESPONSE_ACTION',
  5. ... optional mapping of arguments ...
  6. }
  7. }
  8. ...
  9. });
  10. const store = createStore(exampleReducer, applyMiddleware(ipc));

What about redux-thunk?

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

Example

  1. const ipc = createIpc({
  2. 'ipc channel name': () => dispatch =>
  3. dispatch({ type: 'DELAYED_ACTION_TYPE' })
  4. });
  5. const store = createStore(exampleReducer, applyMiddleware(thunk, ipc));

Questions

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