From 78b8f85197bb2bad3a34d5b54276ac03b7c3d47a Mon Sep 17 00:00:00 2001 From: Laurent Pellegrino Date: Wed, 14 Oct 2020 18:08:39 +0200 Subject: [PATCH 1/2] Move Connection type to types.ts This patch fixes Aaronius/penpal#57. --- src/parent/connectToChild.ts | 24 +++++++----------------- src/types.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) 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..5de9edc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -105,3 +105,15 @@ export type AsyncMethodReturns> ? (...args: A) => Promise : T[KK] }; + +export 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; +}; From 41a107bc1f795dea5281bdd22d9b7ab561d28f2e Mon Sep 17 00:00:00 2001 From: Laurent Pellegrino Date: Wed, 14 Oct 2020 18:09:36 +0200 Subject: [PATCH 2/2] Sort type definitions by lexicographic order --- src/types.ts | 135 ++++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/src/types.ts b/src/types.ts index 5de9edc..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,49 +86,35 @@ export type SynAckMessage = { }; /** - * An ACK handshake message. - */ -export type AckMessage = { - penpal: MessageType.Ack; - methodNames: string[]; -}; - -/** - * Methods that may be called that will invoke methods on the remote window. + * A SYN handshake message. */ -export type CallSender = { - [index: string]: Function; +export type SynMessage = { + penpal: MessageType.Syn; }; -/** - * A Penpal-specific error. - */ -export type PenpalError = Error & { code: ErrorCode }; +export type WindowsInfo = { + /** + * A friendly name for the local window. + */ + localName: 'Parent' | 'Child'; -/** - * 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 local window. + */ + local: 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] -}; + /** + * The remote window. + */ + remote: Window; -export type Connection = { /** - * A promise which will be resolved once a connection has been established. + * Origin that should be used for sending messages to the remote window. */ - promise: Promise>; + originForSending: string; + /** - * A method that, when called, will disconnect any messaging channels. - * You may call this even before a connection has been established. + * Origin that should be used for receiving messages from the remote window. */ - destroy: Function; + originForReceiving: string; };