@jitsi/js-utils
Version:
Utilities for Jitsi JS projects
115 lines (114 loc) • 4.26 kB
TypeScript
import type { ITransportBackend, ITransportOptions, TransportListener } from './types';
/**
* Stores the current transport backend that has to be used. Also implements
* request/response mechanism.
*/
export default class Transport {
/**
* Maps an event name and listeners that have been added to the Transport instance.
*/
private _listeners;
/**
* The request ID counter used for the id property of the request. This
* property is used to match the responses with the request.
*/
private _requestID;
/**
* Maps IDs of the requests and handlers that will process the responses of those requests.
*/
private _responseHandlers;
/**
* A set with the events and requests that were received but not
* processed by any listener. They are later passed on every new
* listener until they are processed.
*/
private _unprocessedMessages;
/**
* The transport backend.
*/
private _backend?;
/**
* Alias for on method.
*/
addListener: typeof Transport.prototype.on;
/**
* Creates new instance.
*
* @param {ITransportOptions} options - Optional parameters for configuration of the transport backend.
*/
constructor({ backend }?: ITransportOptions);
/**
* Disposes the current transport backend.
*/
private _disposeBackend;
/**
* Handles incoming messages from the transport backend.
*
* @param {ITransportMessage} message - The message.
* @returns {void}
*/
private _onMessageReceived;
/**
* Disposes the allocated resources.
*
* @returns {void}
*/
dispose(): void;
/**
* Calls each of the listeners registered for the event named eventName, in
* the order they were registered, passing the supplied arguments to each.
*
* @param {string} eventName - The name of the event.
* @param {...any} args - Arguments to pass to the listeners.
* @returns {boolean} True if the event has been processed by any listener, false otherwise.
*/
emit(eventName: string, ...args: any[]): boolean;
/**
* Adds the listener function to the listeners collection for the event
* named eventName.
*
* @param {string} eventName - The name of the event.
* @param {TransportListener} listener - The listener that will be added.
* @returns {Transport} References to the instance of Transport class, so that calls can be chained.
*/
on(eventName: string, listener: TransportListener): this;
/**
* Removes all listeners, or those of the specified eventName.
*
* @param {string} [eventName] - The name of the event. If this parameter is not specified all listeners will be removed.
* @returns {Transport} References to the instance of Transport class, so that calls can be chained.
*/
removeAllListeners(eventName?: string): this;
/**
* Removes the listener function from the listeners collection for the event
* named eventName.
*
* @param {string} eventName - The name of the event.
* @param {TransportListener} listener - The listener that will be removed.
* @returns {Transport} References to the instance of Transport class, so that calls can be chained.
*/
removeListener(eventName: string, listener: TransportListener): this;
/**
* Sends the passed event.
*
* @param {Object} [event={}] - The event to be sent.
* @param {Array<any>} [transfer] - An optional array of transferable objects (e.g., ArrayBuffer, MessagePort)
* to transfer ownership of to the remote side, rather than cloning them.
* @returns {void}
*/
sendEvent(event?: any, transfer?: Transferable[]): void;
/**
* Sending request.
*
* @param {Object} request - The request to be sent.
* @returns {Promise<any>} A promise that resolves with the response result or rejects with an error.
*/
sendRequest(request: any): Promise<any>;
/**
* Changes the current backend transport.
*
* @param {ITransportBackend} backend - The new transport backend that will be used.
* @returns {void}
*/
setBackend(backend: ITransportBackend): void;
}