Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Tacke committed May 31, 2016
1 parent 6cbf540 commit 65fb357
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
**/node_modules/**
38 changes: 38 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"es6": true
},
"extends": [
"eslint:recommended"
],
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
},
"rules": {
"object-curly-spacing": [2, "always"],
"object-shorthand": [2, "always"],
"one-var": [2, "never"],
"no-array-constructor": 2,
"no-console": 1,
"no-const-assign": 2,
"no-new-object": 2,
"no-unused-vars": 1,
"no-var": 2,
"prefer-arrow-callback": 2,
"prefer-const": 2,
"prefer-spread": 1,
"prefer-template": 2,
"quote-props": [2, "as-needed"],
"quotes": [2, "single"],
"require-yield": 1,
"semi": 2,
"keyword-spacing": 2,
"space-before-function-paren": 2
}
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
npm-debug.log
*.tgz

dist/
lib/
node_modules/
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.eslintrc
.eslintignore
.npmignore
webpack.config.js
*.tgz

!dist/

example/
src/
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Redux Electron IPC Middleware
A [Redux](https://github.com/reactjs/redux) middleware to reduce code around ipc
calls in an [Electron](http://electron.atom.io/) application. You can send and
receive
[IPC](https://github.com/electron/electron/blob/master/docs/api/ipc-main.md)
events with a simple api.

## Usage
Check out the full demo application.

### Window
```js
import { applyMiddleware, createStore } from 'redux';
import createIpc from 'redux-electron-ipc';
import { pingActionCreator, pongActionCreator } from './actions';
import { exampleReducer } from './reducer';

// register an action creator to an ipc channel (key/channel, value/action creator)
const ipc = createIpc({
'pong': pongActionCreator, // receive a message
...
});

const store = createStore(exampleReducer, applyMiddleware(ipc));

// send a message with arguments
store.dispatch(pingActionCreator('redux', 'electron', 'ipc'));
```

### Main
```js
const electron = require('electron');
const { ipcMain } = electron;

...

// pong event with arguments back to caller
ipcMain.on('ping', (event, ...args) => {
console.log('Ping', ...args);
event.sender.send('pong', ...args);
});
```
20 changes: 20 additions & 0 deletions example/app/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function pingActionCreator (arg1, arg2, arg3) {
return {
type: 'IPC_PING',
channel: 'ping',
args: [
arg1,
arg2,
arg3
]
};
}

export function pongActionCreator (event, arg1, arg2, arg3) {
return {
type: 'IPC_PONG',
arg1,
arg2,
arg3
};
}
11 changes: 11 additions & 0 deletions example/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { applyMiddleware, createStore } from 'redux';
import createIpc from '../../';
import { pingActionCreator, pongActionCreator } from './actions';
import { exampleReducer } from './reducer';

const ipc = createIpc({
'pong': pongActionCreator // eslint-disable-line quote-props
});
const store = createStore(exampleReducer, applyMiddleware(ipc));

store.dispatch(pingActionCreator('redux', 'electron', 'ipc'));
9 changes: 9 additions & 0 deletions example/app/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function exampleReducer (state = {}, action) {
switch (action.type) {
case 'IPC_PONG':
console.log('Pong', action); // eslint-disable-line no-console
return state;
default:
return state;
}
}
22 changes: 22 additions & 0 deletions example/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const electron = require('electron');
const { app, BrowserWindow, ipcMain } = electron;

let win;

app.on('ready', () => {
win = new BrowserWindow();

win.loadURL(`file://${__dirname}/dist/index.html`);

win.webContents.openDevTools();

win.on('closed', () => {
win = null;
});
});

// ping pong event with arguments back to caller
ipcMain.on('ping', (event, arg1, arg2, arg3) => {
console.log('Ping', arg1, arg2, arg3); // eslint-disable-line no-console
event.sender.send('pong', arg1, arg2, arg3);
});
16 changes: 16 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "redux-electron-ipc-middleware-example",
"main": "main.js",
"version": "1.0.0",
"scripts": {
"start": "./node_modules/.bin/webpack ./app/app.js && electron ."
},
"devDependencies": {
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"html-webpack-plugin": "^2.19.0",
"redux": "^3.5.2",
"webpack": "^1.13.1"
}
}
29 changes: 29 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: './app/app.js',
output: {
path: 'dist',
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: [
'es2015'
]
}
}
]
},
target: 'electron',
plugins: [
new HtmlWebpackPlugin({
title: 'Redux Electron IPC Middleware Example'
})
]
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/electron-redux-ipc.js');
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "redux-electron-ipc",
"version": "1.0.0",
"description": "Redux Electron IPC Middleware",
"main": "index.js",
"directories": {
"example": "example"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "./node_modules/.bin/eslint .",
"build": "WEBPACK_ENV=production ./node_modules/.bin/webpack",
"dev": "./node_modules/.bin/webpack --watch"
},
"keywords": [
"redux",
"electron",
"ipc",
"middleware"
],
"author": "Mario Tacke <[email protected]>",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.9.1",
"babel-eslint": "^6.0.4",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.9.0",
"eslint": "^2.11.1",
"webpack": "^1.13.1"
}
}
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ipcRenderer as ipc } from 'electron';

function createIpc (events = {}, prefix = 'IPC_') {
return ({ dispatch }) => {
Object.keys(events).forEach((key) => {
ipc.on(key, function () {
dispatch(events[key](...arguments));
});
});

return function (next) {
return function (action) {
if (action.channel && action.type.startsWith(prefix)) {
ipc.send(action.channel, ...(action.args || []));
}

return next(action);
};
};
};
}

export default createIpc;
48 changes: 48 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const webpack = require('webpack');
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
const env = process.env.WEBPACK_ENV;
const library = 'electron-redux-ipc';
const plugins = [];

let filename;

if (env === 'production') {
plugins.push(new UglifyJsPlugin({ minimize: true }));
filename = `${library}.min.js`;
} else {
filename = `${library}.js`;
}

const config = {
entry: `${__dirname}/src/index.js`,
output: {
path: `${__dirname}/dist`,
filename,
library,
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: [
'es2015'
],
plugins: [
'babel-plugin-add-module-exports'
]
}
}
]
},
target: 'electron',
plugins
};

module.exports = config;

0 comments on commit 65fb357

Please sign in to comment.