electron/lib/browser/message-port-main.ts

29 lines
773 B
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { EventEmitter } from 'events';
export class MessagePortMain extends EventEmitter {
_internalPort: any;
constructor (internalPort: any) {
2020-03-20 20:28:31 +00:00
super();
this._internalPort = internalPort;
this._internalPort.emit = (channel: string, event: {ports: any[]}) => {
2020-03-20 20:28:31 +00:00
if (channel === 'message') { event = { ...event, ports: event.ports.map(p => new MessagePortMain(p)) }; }
this.emit(channel, event);
};
}
start () {
2020-03-20 20:28:31 +00:00
return this._internalPort.start();
}
close () {
2020-03-20 20:28:31 +00:00
return this._internalPort.close();
}
postMessage (...args: any[]) {
if (Array.isArray(args[1])) {
2020-03-20 20:28:31 +00:00
args[1] = args[1].map((o: any) => o instanceof MessagePortMain ? o._internalPort : o);
}
2020-03-20 20:28:31 +00:00
return this._internalPort.postMessage(...args);
}
}