Skip to content

Commit

Permalink
feat: interop ipc (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
KeziahMoselle authored Jan 9, 2020
1 parent c0232ac commit 0c937c5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/app/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class App extends Component {
// Send `handshake` event to receive new value from the store
window.ipcRenderer.send('handshake')
// Receive new values from the store
window.ipcRenderer.once('handshake', (event, data) => {
window.ipcRenderer.once('handshake', data => {
this.setState({
total: data.work,
totalPause: data.pause,
Expand Down
31 changes: 31 additions & 0 deletions packages/app/src/backend/IpcMain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class IpcMain {
constructor() {
this.channels = {}
}

on(channel, listener) {
if (typeof this.channels[channel] !== 'object') {
this.channels[channel] = []
}
this.channels[channel].push(listener)
return () => this.removeListener(channel, listener)
}

/**
* Call functions from the IpcMain channels
*
* @param {*} channel
* @param {*} data
*/
emit(sender, channel, data) {
if (!this.channels[channel]) {
return console.warn(`IpcMain: No listener on '${channel}'`)
}

this.channels[channel].forEach(listener =>
listener.call(this, sender, data)
)
}
}

module.exports = IpcMain
60 changes: 60 additions & 0 deletions packages/app/src/backend/IpcRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class IpcRenderer {
constructor(IpcMain) {
this.channels = {}
this.IpcMain = IpcMain
}

on(channel, listener) {
if (typeof this.channels[channel] !== 'object') {
this.channels[channel] = []
}
this.channels[channel].push(listener)
return () => this.removeListener(channel, listener)
}

removeListener(channel, listener) {
if (typeof this.channels[channel] === 'object') {
const idx = this.channels[channel].indexOf(listener)
if (idx > -1) {
this.channels[channel].splice(idx, 1)
}
}
}

once(channel, listener) {
const remove = this.on(channel, (...args) => {
remove()
listener.apply(this, args)
})
}

send(channel, data) {
this.IpcMain.emit(this, channel, data)
}

/**
* Call functions from the IpcRenderer channels
*
* @param {*} channel
* @param {*} data
*/
async emit(channel, data) {
await this.waitEventLoop()

if (!this.channels[channel]) {
return console.warn(`IpcRenderer: No listener on '${channel}'`)
}

this.channels[channel].forEach(listener => listener.call(this, data))
}

removeAllListeners(channel) {
console.log('Remove all listeners ', channel)
}

waitEventLoop() {
return Promise.resolve()
}
}

module.exports = IpcRenderer
18 changes: 18 additions & 0 deletions packages/app/src/backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import IpcRenderer from './IpcRenderer'
import IpcMain from './IpcMain'

const ipcMain = new IpcMain()
const ipcRenderer = new IpcRenderer(ipcMain)

ipcMain.on('handshake', event => {
event.emit('handshake', {
work: 60 * 25,
pause: 60 * 5,
sessionStreak: 5,
numberOfCycle: 0,
isDraggable: false,
workTillDelayedMinutes: 0
})
})

export { ipcMain, ipcRenderer }
4 changes: 2 additions & 2 deletions packages/app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App.jsx'
import { ipc } from '@tempus/core'
import isElectron from './utils/isElectron'
import { ipcRenderer } from './backend'

if (!isElectron) {
window.ipcRenderer = ipc
window.ipcRenderer = ipcRenderer
}

ReactDOM.render(
Expand Down
8 changes: 0 additions & 8 deletions packages/core/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
module.exports = {
ipc: {
on: () => {},
once: () => {},
send: () => {},
removeAllListeners: () => {}
}
}

0 comments on commit 0c937c5

Please sign in to comment.