hyper/app/rpc.ts

64 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import {EventEmitter} from 'events';
import {ipcMain, BrowserWindow} from 'electron';
import {v4 as uuidv4} from 'uuid';
2016-06-30 22:01:04 -08:00
export class Server extends EventEmitter {
destroyed = false;
win: BrowserWindow;
id!: string;
constructor(win: BrowserWindow) {
super();
2016-06-30 22:01:04 -08:00
this.win = win;
this.ipcListener = this.ipcListener.bind(this);
2016-07-25 10:01:01 -08:00
if (this.destroyed) {
return;
}
2016-07-25 10:01:01 -08:00
const uid = uuidv4();
2016-07-25 10:01:01 -08:00
this.id = uid;
// eslint-disable-next-line @typescript-eslint/unbound-method
2016-07-25 10:01:01 -08:00
ipcMain.on(uid, this.ipcListener);
// we intentionally subscribe to `on` instead of `once`
// to support reloading the window and re-initializing
// the channel
this.wc.on('did-finish-load', () => {
this.wc.send('init', uid);
2016-06-30 22:01:04 -08:00
});
}
get wc() {
2016-06-30 22:01:04 -08:00
return this.win.webContents;
}
ipcListener(event: any, {ev, data}: {ev: string; data: any}) {
super.emit(ev, data);
2016-06-30 22:01:04 -08:00
}
emit(ch: string, data: any = {}): any {
// This check is needed because data-batching can cause extra data to be
// emitted after the window has already closed
if (!this.win.isDestroyed()) {
this.wc.send(this.id, {ch, data});
}
2016-06-30 22:01:04 -08:00
}
destroy() {
2016-06-30 22:01:04 -08:00
this.removeAllListeners();
this.wc.removeAllListeners();
if (this.id) {
// eslint-disable-next-line @typescript-eslint/unbound-method
2016-06-30 22:01:04 -08:00
ipcMain.removeListener(this.id, this.ipcListener);
} else {
// mark for `genUid` in constructor
this.destroyed = true;
}
}
}
export default (win: BrowserWindow) => {
2016-06-30 22:01:04 -08:00
return new Server(win);
};