Skip to content

Commit

Permalink
Merge pull request #65 from lpellegr/fix/missing-types
Browse files Browse the repository at this point in the history
Export Connection type
  • Loading branch information
Aaronius committed Oct 16, 2020
2 parents b613a29 + 41a107b commit 627bea8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 76 deletions.
24 changes: 7 additions & 17 deletions src/parent/connectToChild.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand Down Expand Up @@ -35,18 +37,6 @@ type Options = {
debug?: boolean;
};

type Connection<TCallSender extends object = CallSender> = {
/**
* A promise which will be resolved once a connection has been established.
*/
promise: Promise<AsyncMethodReturns<TCallSender>>;
/**
* 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.
*/
Expand Down
131 changes: 72 additions & 59 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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<T, K extends keyof T = FunctionPropertyNames<T>> = {
[KK in K]: T[KK] extends (...args: any[]) => PromiseLike<any>
? T[KK]
: T[KK] extends (...args: infer A) => infer R
? (...args: A) => Promise<R>
: 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<TCallSender extends object = CallSender> = {
/**
* 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<AsyncMethodReturns<TCallSender>>;
/**
* 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<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T];

/**
* Methods to expose to the remote window.
*/
Expand All @@ -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.
Expand All @@ -55,13 +77,6 @@ export type ReplyMessage = {
returnValueIsError?: boolean;
};

/**
* A SYN handshake message.
*/
export type SynMessage = {
penpal: MessageType.Syn;
};

/**
* A SYN-ACK handshake message.
*/
Expand All @@ -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<T> = { [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<T, K extends keyof T = FunctionPropertyNames<T>> = {
[KK in K]: T[KK] extends (...args: any[]) => PromiseLike<any>
? T[KK]
: T[KK] extends (...args: infer A) => infer R
? (...args: A) => Promise<R>
: 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;
};

0 comments on commit 627bea8

Please sign in to comment.