Commit 25d2b6a0 authored by Robert Knight's avatar Robert Knight

Migrate port-util.js to TypeScript

parent c0ec257e
export type Frame = 'guest' | 'host' | 'notebook' | 'profile' | 'sidebar';
/**
* Message sent by `PortProvider` and `PortFinder` to establish a
* MessageChannel-based connection between two frames.
*
* @typedef {'guest'|'host'|'notebook'|'profile'|'sidebar'} Frame
*
* @typedef Message
* @prop {Frame} frame1 - Role of the source frame
* @prop {Frame} frame2 - Role of the target frame
* @prop {'offer'|'request'} type - Message type. "request" messages are sent
* by the source frame to the host frame to request a connection. "offer"
* messages are sent from the host frame back to the source frame and also
* to the target frame, accompanied by a MessagePort.
* @prop {string} requestId - ID of the request. Used to associate "offer"
* messages with their corresponding "request" messages.
* @prop {string} [sourceId] - Identifier for the source frame. This is useful
* in cases where multiple source frames with a given role may connect to
* the same destination frame.
*/
export type Message = {
/** Role of the source frame. */
frame1: Frame;
/** Role of the target frame. */
frame2: Frame;
/**
* Message type. "request" messages are sent by the source frame to the host
* frame to request a connection. "offer" messages are sent from the host
* frame back to the source frame and also to the target frame, accompanied by
* a MessagePort.
*/
type: 'offer' | 'request';
/**
* ID of the request. Used to associate "offer" messages with their
* corresponding "request" messages.
*/
requestId: string;
/**
* Identifier for the source frame. This is useful in cases where multiple
* source frames with a given role may connect to the same destination frame.
*/
sourceId?: string;
};
/**
* Return true if an object, eg. from the data field of a `MessageEvent`, is a
* Return true if an object, eg. from the `data` field of a `MessageEvent`, is a
* valid `Message`.
*
* @param {any} data
* @return {data is Message}
*/
export function isMessage(data) {
export function isMessage(data: any): data is Message {
if (data === null || typeof data !== 'object') {
return false;
}
for (let property of ['frame1', 'frame2', 'type', 'requestId']) {
for (const property of ['frame1', 'frame2', 'type', 'requestId']) {
if (typeof data[property] !== 'string') {
return false;
}
......@@ -41,11 +52,8 @@ export function isMessage(data) {
/**
* Return true if the data payload from a MessageEvent matches `message`.
*
* @param {any} data
* @param {Partial<Message>} message
*/
export function isMessageEqual(data, message) {
export function isMessageEqual(data: any, message: Partial<Message>) {
if (!isMessage(data)) {
return false;
}
......@@ -60,11 +68,10 @@ export function isMessageEqual(data, message) {
/**
* Check that source is of type Window.
*
* @param {MessageEventSource|null} source
* @return {source is Window}
*/
export function isSourceWindow(source) {
export function isSourceWindow(
source: MessageEventSource | null
): source is Window {
if (
// `source` can be of type Window, MessagePort, ServiceWorker, or null.
// `source instanceof Window` doesn't work in Chrome if `source` is a
......
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