2020-03-12 01:07:54 +00:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
|
2023-10-25 18:02:15 +00:00
|
|
|
export class MessagePortMain extends EventEmitter implements Electron.MessagePortMain {
|
2023-05-30 11:10:22 +00:00
|
|
|
_internalPort: any;
|
2020-03-12 01:07:54 +00:00
|
|
|
constructor (internalPort: any) {
|
|
|
|
super();
|
|
|
|
this._internalPort = internalPort;
|
|
|
|
this._internalPort.emit = (channel: string, event: {ports: any[]}) => {
|
|
|
|
if (channel === 'message') { event = { ...event, ports: event.ports.map(p => new MessagePortMain(p)) }; }
|
|
|
|
this.emit(channel, event);
|
|
|
|
};
|
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
start () {
|
|
|
|
return this._internalPort.start();
|
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
close () {
|
|
|
|
return this._internalPort.close();
|
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
postMessage (...args: any[]) {
|
|
|
|
if (Array.isArray(args[1])) {
|
|
|
|
args[1] = args[1].map((o: any) => o instanceof MessagePortMain ? o._internalPort : o);
|
|
|
|
}
|
|
|
|
return this._internalPort.postMessage(...args);
|
|
|
|
}
|
|
|
|
}
|