Skip to content

Commit

Permalink
Sort type definitions by lexicographic order
Browse files Browse the repository at this point in the history
  • Loading branch information
lpellegr committed Oct 14, 2020
1 parent 78b8f85 commit 41a107b
Showing 1 changed file with 68 additions and 67 deletions.
135 changes: 68 additions & 67 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,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<T> = { [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<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]
};
/**
* The remote window.
*/
remote: Window;

export type Connection<TCallSender extends object = CallSender> = {
/**
* 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<AsyncMethodReturns<TCallSender>>;
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;
};

0 comments on commit 41a107b

Please sign in to comment.