Commit 625bdb9d authored by Robert Knight's avatar Robert Knight

Streamline code for responding to port requests

Rework the code to make the code path for the sidebar-host channel more
similar to other channels. As a result the `_sendPorts` helper was only
called in one place and can be inlined where it is called to make the
overall logic easier to follow.
parent 94190ade
...@@ -137,37 +137,6 @@ export class PortProvider { ...@@ -137,37 +137,6 @@ export class PortProvider {
return isMessageEqual(data, allowedMessage); return isMessageEqual(data, allowedMessage);
} }
/**
* Send a message and a port to the corresponding destinations.
*
* @param {object} options
* @param {Channel} options.channel - communication channel enabled by this
* port.
* @param {Message} options.message - the message to be sent.
* @param {string} options.origin - the target origin to be used for sending
* the port.
* @param {Window} options.source - the frame to be used for sending the port.
* @param {MessagePort} options.port1 - the port to be sent.
* @param {MessagePort} [options.port2] - if a counterpart port is provided,
* send this port either, (1) to the `sidebar` frame using the `sidebar-host`
* channel or (2) through the `onHostPortRequest` event listener.
*/
_sendPorts({ channel, message, origin, source, port1, port2 }) {
source.postMessage(message, origin, [port1]);
if (!port2) {
return;
}
if (['notebook-sidebar', 'guest-sidebar'].includes(channel)) {
this._sidebarHostChannel.port2.postMessage(message, [port2]);
}
if (channel === 'guest-host' && message.frame1 === 'guest') {
this._emitter.emit('frameConnected', message.frame1, port2);
}
}
/** /**
* @param {'frameConnected'} eventName * @param {'frameConnected'} eventName
* @param {(source: 'guest', port: MessagePort) => void} handler - this handler * @param {(source: 'guest', port: MessagePort) => void} handler - this handler
...@@ -232,26 +201,25 @@ export class PortProvider { ...@@ -232,26 +201,25 @@ export class PortProvider {
return; return;
} }
/** @type {Message} */ // Create the channel for these two frames to communicate.
const message = { frame1, frame2, type: 'offer' }; if (frame1 === 'sidebar' && frame2 === 'host') {
const options = { channel, message, origin, source }; messageChannel = this._sidebarHostChannel;
} else {
// `sidebar-host` channel is an special case, because it is created in the messageChannel = new MessageChannel();
// constructor.
if (channel === 'sidebar-host') {
windowChannelMap.set(source, this._sidebarHostChannel);
this._sendPorts({
port1: this._sidebarHostChannel.port1,
...options,
});
return;
} }
messageChannel = new MessageChannel();
windowChannelMap.set(source, messageChannel); windowChannelMap.set(source, messageChannel);
const { port1, port2 } = messageChannel; // Send the ports to the frames at either end of the channel.
this._sendPorts({ port1, port2, ...options }); const message = { frame1, frame2, type: 'offer' };
source.postMessage(message, origin, [messageChannel.port1]);
if (frame2 === 'sidebar') {
this._sidebarHostChannel.port2.postMessage(message, [
messageChannel.port2,
]);
} else if (frame2 === 'host' && frame1 === 'guest') {
this._emitter.emit('frameConnected', frame1, messageChannel.port2);
}
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment