2020-03-20 20:28:31 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
export class MessagePortMain extends EventEmitter {
|
2023-05-30 11:10:22 +00:00
|
|
|
_internalPort: any;
|
2020-03-12 01:07:54 +00:00
|
|
|
constructor (internalPort: any) {
|
2020-03-20 20:28:31 +00:00
|
|
|
super();
|
|
|
|
this._internalPort = internalPort;
|
2020-03-12 01:07:54 +00:00
|
|
|
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);
|
|
|
|
};
|
2020-03-12 01:07:54 +00:00
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
start () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._internalPort.start();
|
2020-03-12 01:07:54 +00:00
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
close () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._internalPort.close();
|
2020-03-12 01:07:54 +00:00
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
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-12 01:07:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._internalPort.postMessage(...args);
|
2020-03-12 01:07:54 +00:00
|
|
|
}
|
|
|
|
}
|