Skip to content

Commit

Permalink
electron-ipc-stream: started conversion to ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
jprichardson committed Mar 19, 2016
1 parent 29a582d commit 1100957
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
20 changes: 8 additions & 12 deletions src/main.js
@@ -1,7 +1,7 @@
var bufferJson = require('buffer-json')
var Duplex = require('stream').Duplex
var ipcMain = require('ipc-main')
var util = require('util')
const bufferJson = require('buffer-json')
const Duplex = require('stream').Duplex
const ipcMain = require('electron').ipcMain
const util = require('util')

function MainIPCStream (channel, browserWindow, streamOpts) {
if (!(this instanceof MainIPCStream)) {
Expand All @@ -13,23 +13,19 @@ function MainIPCStream (channel, browserWindow, streamOpts) {
this.browserWindow = browserWindow
this.channel = channel

var self = this
function ipcCallback (event, data) {
const ipcCallback = (event, data) => {
if (typeof data === 'string') {
data = JSON.parse(data, bufferJson.reviver)
}
self.push(data)
this.push(data)
}
ipcMain.on(this.channel, ipcCallback)

this.on('finish', function () {
this.on('finish', () => {
if (this.browserWindow) this.browserWindow.webContents.send(this.channel + '-finish')
ipcMain.removeListener(this.channel, ipcCallback)
})

ipcMain.once(this.channel + '-finish', function () {
self.push(null)
})
ipcMain.once(this.channel + '-finish', () => this.push(null))

Duplex.call(this, streamOpts)
}
Expand Down
19 changes: 7 additions & 12 deletions src/rend.js
@@ -1,7 +1,7 @@
var bufferJson = require('buffer-json')
var Duplex = require('stream').Duplex
var ipcRenderer = require('ipc-renderer')
var util = require('util')
const bufferJson = require('buffer-json')
const Duplex = require('stream').Duplex
const ipcRenderer = require('electron').ipcRenderer
const util = require('util')

function RendIPCStream (channel, streamOpts) {
if (!(this instanceof RendIPCStream)) {
Expand All @@ -12,24 +12,19 @@ function RendIPCStream (channel, streamOpts) {

this.channel = channel


var self = this
function ipcCallback (event, data) {
const ipcCallback = (event, data) => {
if (typeof data === 'string') {
data = JSON.parse(data, bufferJson.reviver)
}
self.push(data)
this.push(data)
}
ipcRenderer.on(this.channel, ipcCallback)

this.on('finish', function () {
ipcRenderer.send(this.channel + '-finish')
ipcRenderer.removeListener(this.channel, ipcCallback)
})

ipcRenderer.once(this.channel + '-finish', function () {
self.push(null)
})
ipcRenderer.once(this.channel + '-finish', () => this.push(null))

Duplex.call(this, streamOpts)
}
Expand Down

0 comments on commit 1100957

Please sign in to comment.