diff --git a/src/parent/connectToChild.ts b/src/parent/connectToChild.ts index 737c613..858fc2c 100644 --- a/src/parent/connectToChild.ts +++ b/src/parent/connectToChild.ts @@ -1,13 +1,15 @@ +import { CallSender, PenpalError, AsyncMethodReturns, Connection, Methods } from '../types'; +import { ErrorCode, MessageType, NativeEventType } from '../enums'; + import createDestructor from '../createDestructor'; -import getOriginFromSrc from './getOriginFromSrc'; import createLogger from '../createLogger'; -import handleSynMessageFactory from './handleSynMessageFactory'; +import getOriginFromSrc from './getOriginFromSrc'; import handleAckMessageFactory from './handleAckMessageFactory'; -import { CallSender, Methods, PenpalError, AsyncMethodReturns } from '../types'; -import { ErrorCode, MessageType, NativeEventType } from '../enums'; -import validateIframeHasSrcOrSrcDoc from './validateIframeHasSrcOrSrcDoc'; +import handleSynMessageFactory from './handleSynMessageFactory'; import monitorIframeRemoval from './monitorIframeRemoval'; import startConnectionTimeout from '../startConnectionTimeout'; +import validateIframeHasSrcOrSrcDoc from './validateIframeHasSrcOrSrcDoc'; + type Options = { /** @@ -35,18 +37,6 @@ type Options = { debug?: boolean; }; -type Connection = { - /** - * A promise which will be resolved once a connection has been established. - */ - promise: Promise>; - /** - * A method that, when called, will disconnect any messaging channels. - * You may call this even before a connection has been established. - */ - destroy: Function; -}; - /** * Attempts to establish communication with an iframe. */ diff --git a/src/types.ts b/src/types.ts index 008ef22..7a56d39 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,32 +1,59 @@ import { ErrorCode, MessageType, Resolution } from './enums'; -export type WindowsInfo = { - /** - * A friendly name for the local window. - */ - localName: 'Parent' | 'Child'; - /** - * The local window. - */ - local: Window; +/** + * An ACK handshake message. + */ +export type AckMessage = { + penpal: MessageType.Ack; + methodNames: string[]; +}; - /** - * The remote window. - */ - remote: Window; +/** + * A mapped type to convert non async methods into async methods and exclude any non function properties. + */ +export type AsyncMethodReturns> = { + [KK in K]: T[KK] extends (...args: any[]) => PromiseLike + ? T[KK] + : T[KK] extends (...args: infer A) => infer R + ? (...args: A) => Promise + : T[KK] +}; +/** + * A method call message. + */ +export type CallMessage = { + penpal: MessageType.Call; + id: number; + methodName: string; + args: any[]; +}; + +/** + * Methods that may be called that will invoke methods on the remote window. + */ +export type CallSender = { + [index: string]: Function; +}; + +export type Connection = { /** - * Origin that should be used for sending messages to the remote window. + * A promise which will be resolved once a connection has been established. */ - originForSending: string; - + promise: Promise>; /** - * Origin that should be used for receiving messages from the remote window. + * A method that, when called, will disconnect any messaging channels. + * You may call this even before a connection has been established. */ - originForReceiving: string; + destroy: Function; }; +/** + * A mapped type to extract only object properties which are functions. + */ +export type FunctionPropertyNames = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]; + /** * Methods to expose to the remote window. */ @@ -35,14 +62,9 @@ export type Methods = { }; /** - * A method call message. + * A Penpal-specific error. */ -export type CallMessage = { - penpal: MessageType.Call; - id: number; - methodName: string; - args: any[]; -}; +export type PenpalError = Error & { code: ErrorCode }; /** * A method response message. @@ -55,13 +77,6 @@ export type ReplyMessage = { returnValueIsError?: boolean; }; -/** - * A SYN handshake message. - */ -export type SynMessage = { - penpal: MessageType.Syn; -}; - /** * A SYN-ACK handshake message. */ @@ -71,37 +86,35 @@ export type SynAckMessage = { }; /** - * An ACK handshake message. + * A SYN handshake message. */ -export type AckMessage = { - penpal: MessageType.Ack; - methodNames: string[]; +export type SynMessage = { + penpal: MessageType.Syn; }; -/** - * Methods that may be called that will invoke methods on the remote window. - */ -export type CallSender = { - [index: string]: Function; -}; +export type WindowsInfo = { + /** + * A friendly name for the local window. + */ + localName: 'Parent' | 'Child'; -/** - * A Penpal-specific error. - */ -export type PenpalError = Error & { code: ErrorCode }; + /** + * The local window. + */ + local: Window; -/** - * A mapped type to extract only object properties which are functions. - */ -export type FunctionPropertyNames = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]; + /** + * The remote window. + */ + remote: Window; -/** - * A mapped type to convert non async methods into async methods and exclude any non function properties. - */ -export type AsyncMethodReturns> = { - [KK in K]: T[KK] extends (...args: any[]) => PromiseLike - ? T[KK] - : T[KK] extends (...args: infer A) => infer R - ? (...args: A) => Promise - : T[KK] + /** + * Origin that should be used for sending messages to the remote window. + */ + originForSending: string; + + /** + * Origin that should be used for receiving messages from the remote window. + */ + originForReceiving: string; };