UNPKG

rx-postmessenger

Version:

Minimal RxJS adapter for the window.postMessage API for request-response streams and notification streams across frame windows.

59 lines (58 loc) 2.16 kB
export interface IMessageIDGenerator { /** * Get a new unique ID value for message objects. */ generateID(): string; /** * Invalidates an ID value - marks it a used. This is a required * feature for syncing the used ID values */ invalidateID(id: string): void; } /** * The IMessageObject interface defines the format for data property values of MessageEvent objects. * This interface describes the actual format of data sent with postMessage that is required for * another RxPostmessenger on the other end of the line to be able to correctly interpret the * message. */ export interface IMessageObject<T = any> { readonly id: string; readonly type: MessageType; readonly channel: string; readonly payload: T; } export interface IRequestObject<T = any> extends IMessageObject<T> { readonly type: 'request'; } export interface IResponseObject<T = any> extends IMessageObject<T> { readonly requestId: string; readonly type: 'response'; } export interface INotificationObject<T = any> extends IMessageObject<T> { readonly type: 'notification'; } export declare type AnyMessage = IRequestObject | IResponseObject | INotificationObject; export interface IMessageTypeMap<T = any> { request: IRequestObject; response: IResponseObject; notification: INotificationObject; } export declare type MessageType = keyof IMessageTypeMap; export declare type MappedMessage<T extends MessageType> = IMessageTypeMap[T]; export interface IMessageFactory { invalidateID(id: string): void; makeRequest<T>(channel: string, payload: T): IRequestObject<T>; makeResponse<T>(requestId: string, channel: string, payload: T): IResponseObject<T>; makeNotification<T>(channel: string, payload: T): INotificationObject<T>; } export interface IOwnMessageEvent<T> extends MessageEvent { readonly data: T; } export interface IMessageValidator { validate(message: MessageEvent): message is IOwnMessageEvent<AnyMessage>; } export interface IPostmessageAdapter { readonly targetWindow: Window; readonly targetOrigin: string; postMessage<T extends AnyMessage>(data: T): void; }