Commit c0ec257e authored by Robert Knight's avatar Robert Knight

Migrate port-finder.js to TypeScript

parent b069ee7f
import type { Destroyable } from '../../types/annotator';
import { ListenerCollection } from '../listener-collection'; import { ListenerCollection } from '../listener-collection';
import { generateHexString } from '../random'; import { generateHexString } from '../random';
import { isMessageEqual } from './port-util'; import { isMessageEqual } from './port-util';
import type { Frame } from './port-util';
/** Timeout for waiting for the host frame to respond to a port request. */ /** Timeout for waiting for the host frame to respond to a port request. */
export const MAX_WAIT_FOR_PORT = 1000 * 20; export const MAX_WAIT_FOR_PORT = 1000 * 20;
...@@ -8,28 +10,29 @@ export const MAX_WAIT_FOR_PORT = 1000 * 20; ...@@ -8,28 +10,29 @@ export const MAX_WAIT_FOR_PORT = 1000 * 20;
/** Polling interval for requests to the host frame for a port. */ /** Polling interval for requests to the host frame for a port. */
export const POLLING_INTERVAL_FOR_PORT = 250; export const POLLING_INTERVAL_FOR_PORT = 250;
/** export type Options = {
* @typedef {import('../../types/annotator').Destroyable} Destroyable /** The role of this frame. */
* @typedef {import('./port-util').Message} Message source: Exclude<Frame, 'host'>;
* @typedef {import('./port-util').Frame} Frame
*/ /** Identifier for this frame. */
sourceId?: string;
/** The frame where the `PortProvider` is listening for messages. */
hostFrame: Window;
};
/** /**
* PortFinder is used by non-host frames in the client to establish a * PortFinder is used by non-host frames in the client to establish a
* MessagePort-based connection to other frames. It is used together with * MessagePort-based connection to other frames. It is used together with
* PortProvider which runs in the host frame. See PortProvider for an overview. * PortProvider which runs in the host frame. See PortProvider for an overview.
*
* @implements {Destroyable}
*/ */
export class PortFinder { export class PortFinder implements Destroyable {
/** private _hostFrame: Window;
* @param {object} options private _listeners: ListenerCollection;
* @param {Exclude<Frame, 'host'>} options.source - the role of this frame private _source: Exclude<Frame, 'host'>;
* @param {string} [options.sourceId] - Identifier for this frame private _sourceId: string | undefined;
* @param {Window} options.hostFrame - the frame where the `PortProvider` is
* listening for messages. constructor({ hostFrame, source, sourceId }: Options) {
*/
constructor({ hostFrame, source, sourceId }) {
this._hostFrame = hostFrame; this._hostFrame = hostFrame;
this._source = source; this._source = source;
this._sourceId = sourceId; this._sourceId = sourceId;
...@@ -43,10 +46,9 @@ export class PortFinder { ...@@ -43,10 +46,9 @@ export class PortFinder {
/** /**
* Request a specific port from the host frame * Request a specific port from the host frame
* *
* @param {Frame} target - the frame aiming to be discovered * @param target - the frame aiming to be discovered
* @return {Promise<MessagePort>}
*/ */
async discover(target) { async discover(target: Frame): Promise<MessagePort> {
let isValidRequest = false; let isValidRequest = false;
if ( if (
(this._source === 'guest' && target === 'host') || (this._source === 'guest' && target === 'host') ||
......
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