feat: MessagePorts in the main process (#22404)
This commit is contained in:
parent
c4c0888972
commit
b4d07f76d3
34 changed files with 1316 additions and 113 deletions
25
lib/browser/message-port-main.ts
Normal file
25
lib/browser/message-port-main.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { EventEmitter } from 'events'
|
||||
|
||||
export class MessagePortMain extends EventEmitter {
|
||||
_internalPort: any
|
||||
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)
|
||||
}
|
||||
}
|
||||
start () {
|
||||
return this._internalPort.start()
|
||||
}
|
||||
close () {
|
||||
return this._internalPort.close()
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue