rx-postmessenger
Version:
Minimal RxJS adapter for the window.postMessage API for request-response streams and notification streams across frame windows.
53 lines (52 loc) • 2.79 kB
TypeScript
import type { Messenger as IMessenger, Request } from '../rx-postmessenger';
import type { AnyMessage, INotificationObject, IRequestObject, IResponseObject, MappedMessage, MessageType, IMessageFactory, IMessageValidator, IPostmessageAdapter } from './types';
import { Observable } from 'rxjs';
export declare class Messenger implements IMessenger {
protected readonly messageFactory: IMessageFactory;
protected readonly messageValidator: IMessageValidator;
protected readonly adapter: IPostmessageAdapter;
/**
* Observable stream of all incoming messages that originate from remoteWindow with a
* remoteOrigin url matching this.remoteOrigin.
*/
readonly inboundMessages$: Observable<AnyMessage>;
/**
* Filtered subset of inboundMessages$, emitting all messages of type 'notification'
*/
readonly notifications$: Observable<INotificationObject>;
/**
* Filtered subset of inboundMessages$, emitting all messages of type 'request'
*/
readonly requests$: Observable<IRequestObject>;
/**
* Filtered subset of inboundMessages$, emitting all messages of type 'response'
*/
readonly responses$: Observable<IResponseObject>;
/**
* @param {IMessageFactory} messageFactory - Factory instance for scalar message objects.
* @param {IMessageValidator} messageValidator - Validator instance for incoming messages.
* @param {IPostmessageAdapter} adapter - Adapter for interacting with the postMessage API
*/
constructor(messageFactory: IMessageFactory, messageValidator: IMessageValidator, adapter: IPostmessageAdapter);
request<T = any, U = any>(channel: string, payload?: T): Observable<U>;
notify<T>(channel: string, payload?: T): void;
requests<T = any, U = any>(channel: string): Observable<Request<T, U>>;
notifications<T = any>(channel: string): Observable<T>;
/**
* Sends a response through given channel to the remote window, carrying given payload.
*/
protected respond<T>(requestId: string, channel: string, payload: T): this;
/**
* Creates an Observable that completes after a single emission of the MessageEvent
* response for request of given requestId. If no matching response is ever received,
* the Observable never completes.
*/
protected createResponseObservable<T>(requestId: string): Observable<T>;
/**
* Returns an Observable emitting all inbound messages` of given type.
* Returns an Observable emitting a subset of MessageEvent objects emitted
* by this.inboundMessages$, passing through all MessageEvent objects whose
* data.type property matches given type.
*/
protected messagesOfType<T extends MessageType>(type: T): Observable<MappedMessage<T>>;
}