-
Notifications
You must be signed in to change notification settings - Fork 15
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
1 parent
c0232ac
commit 0c937c5
Showing
6 changed files
with
112 additions
and
11 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
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,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 |
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,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 |
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,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 } |
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
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 |
---|---|---|
@@ -1,8 +0,0 @@ | ||
module.exports = { | ||
ipc: { | ||
on: () => {}, | ||
once: () => {}, | ||
send: () => {}, | ||
removeAllListeners: () => {} | ||
} | ||
} | ||