2018-04-25 15:00:58 +02:00
|
|
|
import { ipcRenderer } from 'electron';
|
|
|
|
|
|
|
|
import { MessagingService } from '../../abstractions/messaging.service';
|
|
|
|
|
|
|
|
import { BroadcasterService } from '../../angular/services/broadcaster.service';
|
|
|
|
|
|
|
|
export class ElectronRendererMessagingService implements MessagingService {
|
|
|
|
constructor(private broadcasterService: BroadcasterService) {
|
|
|
|
ipcRenderer.on('messagingService', async (event: any, message: any) => {
|
|
|
|
if (message.command) {
|
2018-05-08 16:29:19 +02:00
|
|
|
this.sendMessage(message.command, message, false);
|
2018-04-25 15:00:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
send(subscriber: string, arg: any = {}) {
|
2018-05-08 16:29:19 +02:00
|
|
|
this.sendMessage(subscriber, arg, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendMessage(subscriber: string, arg: any = {}, toMain: boolean) {
|
2018-04-25 15:00:58 +02:00
|
|
|
const message = Object.assign({}, { command: subscriber }, arg);
|
|
|
|
this.broadcasterService.send(message);
|
2018-05-08 16:29:19 +02:00
|
|
|
if (toMain) {
|
|
|
|
ipcRenderer.send('messagingService', message);
|
|
|
|
}
|
2018-04-25 15:00:58 +02:00
|
|
|
}
|
|
|
|
}
|