-
Notifications
You must be signed in to change notification settings - Fork 3
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
Mario Tacke
committed
May 31, 2016
1 parent
6cbf540
commit 65fb357
Showing
15 changed files
with
309 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,2 @@ | ||
dist/ | ||
**/node_modules/** |
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,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 | ||
} | ||
} |
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,6 @@ | ||
npm-debug.log | ||
*.tgz | ||
|
||
dist/ | ||
lib/ | ||
node_modules/ |
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,10 @@ | ||
.eslintrc | ||
.eslintignore | ||
.npmignore | ||
webpack.config.js | ||
*.tgz | ||
|
||
!dist/ | ||
|
||
example/ | ||
src/ |
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,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); | ||
}); | ||
``` |
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,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 | ||
}; | ||
} |
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,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')); |
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,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; | ||
} | ||
} |
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,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); | ||
}); |
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,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" | ||
} | ||
} |
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,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' | ||
}) | ||
] | ||
}; |
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 @@ | ||
module.exports = require('./dist/electron-redux-ipc.js'); |
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,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" | ||
} | ||
} |
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,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; |
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,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; |