Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: MessagePorts in the main process (#24323)
Co-authored-by: Milan Burda <miburda@microsoft.com>
- Loading branch information
Showing
34 changed files
with
1,319 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# MessageChannelMain | ||
|
||
`MessageChannelMain` is the main-process-side equivalent of the DOM | ||
[`MessageChannel`][] object. Its singular function is to create a pair of | ||
connected [`MessagePortMain`](message-port-main.md) objects. | ||
|
||
See the [Channel Messaging API][] documentation for more information on using | ||
channel messaging. | ||
|
||
## Class: MessageChannelMain | ||
|
||
Example: | ||
```js | ||
const { port1, port2 } = new MessageChannelMain() | ||
w.webContents.postMessage('port', null, [port2]) | ||
port1.postMessage({ some: 'message' }) | ||
``` | ||
|
||
### Instance Properties | ||
|
||
#### `channel.port1` | ||
|
||
A [`MessagePortMain`](message-port-main.md) property. | ||
|
||
#### `channel.port2` | ||
|
||
A [`MessagePortMain`](message-port-main.md) property. | ||
|
||
[`MessageChannel`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel | ||
[Channel Messaging API]: https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# MessagePortMain | ||
|
||
`MessagePortMain` is the main-process-side equivalent of the DOM | ||
[`MessagePort`][] object. It behaves similarly to the DOM version, with the | ||
exception that it uses the Node.js `EventEmitter` event system, instead of the | ||
DOM `EventTarget` system. This means you should use `port.on('message', ...)` | ||
to listen for events, instead of `port.onmessage = ...` or | ||
`port.addEventListener('message', ...)` | ||
|
||
See the [Channel Messaging API][] documentation for more information on using | ||
channel messaging. | ||
|
||
`MessagePortMain` is an [EventEmitter][event-emitter]. | ||
|
||
## Class: MessagePortMain | ||
|
||
### Instance Methods | ||
|
||
#### `port.postMessage(message, [transfer])` | ||
|
||
* `message` any | ||
* `transfer` MessagePortMain[] (optional) | ||
|
||
Sends a message from the port, and optionally, transfers ownership of objects | ||
to other browsing contexts. | ||
|
||
#### `port.start()` | ||
|
||
Starts the sending of messages queued on the port. Messages will be queued | ||
until this method is called. | ||
|
||
#### `port.close()` | ||
|
||
Disconnects the port, so it is no longer active. | ||
|
||
### Instance Events | ||
|
||
#### Event: 'message' | ||
|
||
Returns: | ||
|
||
* `messageEvent` Object | ||
* `data` any | ||
* `ports` MessagePortMain[] | ||
|
||
Emitted when a MessagePortMain object receives a message. | ||
|
||
[`MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort | ||
[Channel Messaging API]: https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { MessagePortMain } from '@electron/internal/browser/message-port-main'; | ||
const { createPair } = process.electronBinding('message_port'); | ||
|
||
export default class MessageChannelMain { | ||
port1: MessagePortMain; | ||
port2: MessagePortMain; | ||
constructor () { | ||
const { port1, port2 } = createPair(); | ||
this.port1 = new MessagePortMain(port1); | ||
this.port2 = new MessagePortMain(port2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.