electron/lib/utility/parent-port.ts
Milan Burda 71fb19ea14
chore: upgrade to eslint v8 (#38472)
chore: upgrade to eslint@8
2023-05-30 13:10:22 +02:00

30 lines
791 B
TypeScript

import { EventEmitter } from 'events';
import { MessagePortMain } from '@electron/internal/browser/message-port-main';
const { createParentPort } = process._linkedBinding('electron_utility_parent_port');
export class ParentPort extends EventEmitter {
#port: ParentPort;
constructor () {
super();
this.#port = createParentPort();
this.#port.emit = (channel: string | symbol, event: { ports: any[] }) => {
if (channel === 'message') {
event = { ...event, ports: event.ports.map(p => new MessagePortMain(p)) };
}
this.emit(channel, event);
return false;
};
}
start () : void {
this.#port.start();
}
pause () : void {
this.#port.pause();
}
postMessage (message: any) : void {
this.#port.postMessage(message);
}
}