import Emittery from 'emittery'; import { BufferLike, Nullable } from '@mlytics/core-ts/core/type/basic'; import { Channel, ChannelHub, Condition } from '@mlytics/core-ts/core/util/future'; import { RTCPeerConnection } from '@mlytics/core-ts/native/webrtc'; import { AbstractBrokerInfo, SpecBroker } from '../../base/broker/broker'; interface SpecPeerBroker extends SpecBroker, SpecConstraintPeerBroker { info: PeerBrokerInfo; createOffer(): Promise; createAnswer(): Promise; commitOffer(offer: RTCSessionDescriptionInit): Promise; commitAnswer(answer: RTCSessionDescriptionInit): Promise; addIceCandidate(ice: RTCIceCandidateInit): Promise; } interface SpecConstraintPeerBroker { info: PeerBrokerInfo; isAvailable: boolean; } declare class PeerBroker implements SpecPeerBroker { protected _info: PeerBrokerInfo; protected _client: PeerClient; protected _condition: Condition; protected _eventHub: ChannelHub; protected _messageHub: ChannelHub; protected _deliverChannel: Channel; protected _receiveChannel: Channel; constructor(); get info(): PeerBrokerInfo; get condition(): Condition; get eventHub(): ChannelHub; get messageHub(): ChannelHub; get deliverChannel(): Channel; get receiveChannel(): Channel; get isActivated(): boolean; get isConnected(): boolean; get isSendable(): boolean; get isAvailable(): boolean; activate(): Promise; deactivate(): Promise; sendMessage(data: BufferLike): Promise; commitMessage(data: BufferLike): Promise; createOffer(): Promise; createAnswer(): Promise; commitOffer(offer: RTCSessionDescriptionInit): Promise; commitAnswer(answer: RTCSessionDescriptionInit): Promise; addIceCandidate(ice: RTCIceCandidateInit): Promise; protected _bindEvents(): Promise; protected _openEventHub(): Promise; } declare class PeerBrokerInfo extends AbstractBrokerInfo { protected _swarmID?: Nullable; protected _resourceStats: Map; get id(): Nullable; get swarmID(): Nullable; set swarmID(value: Nullable); get resourceStats(): Map; set resourceStats(values: Map); } declare class PeerBrokerID { static make(peerID: string, swarmID?: Nullable): string; } declare class PeerClient extends Emittery { protected readonly _options?: RTCConfiguration | undefined; static readonly DEFAULT_DATACHANNEL_NAME = "datachannel"; protected _state: PeerState; protected _connection: RTCPeerConnection; protected _dataChannel?: RTCDataChannel; constructor(_options?: RTCConfiguration | undefined); get state(): SpecPeerState; get bufferedAmount(): number; send(data: BufferLike): Promise; close(): Promise; createOffer(): Promise; createAnswer(): Promise; commitOffer(offer: RTCSessionDescriptionInit): Promise; commitAnswer(answer: RTCSessionDescriptionInit): Promise; addIceCandidate(ice: RTCIceCandidateInit): Promise; protected _bindConnection(): void; protected _bindDataChannel(): void; protected _buildDataChannel(): Promise; protected _onNegotiationNeeded(): Promise; protected _onIceCandidate(event: RTCPeerConnectionIceEvent): Promise; protected _onSignalingStateChange(): Promise; protected _onIceGatheringStateChange(): Promise; protected _onIceConnectionStateChange(): Promise; protected _onDataChannel(event: RTCDataChannelEvent): Promise; protected _onDataChannelOpen(): Promise; protected _onDataChannelData(event: MessageEvent): Promise; protected _onDataChannelError(): Promise; protected _onDataChannelClose(): Promise; } declare class WebRTCEvent { static readonly ICE_NEGOTIATION = "ice_negotiation"; static readonly ICE_CANDIDATE = "ice_candidate"; static readonly ICE_SIGNALING = "ice_signaling"; static readonly ICE_GATHERING = "ice_gathering"; static readonly ICE_CONNECTION = "ice_connection"; static readonly DATACHANNEL_OPEN = "datachannel_open"; static readonly DATACHANNEL_DATA = "datachannel_data"; static readonly DATACHANNEL_ERROR = "datachannel_error"; static readonly DATACHANNEL_CLOSE = "datachannel_close"; } declare class PeerEvent extends WebRTCEvent { static readonly CONNECTION_CLOSED = "connection_closed"; } interface SpecPeerState { isInitial: boolean; isConnected: boolean; isDestroyed: boolean; } declare class PeerState implements SpecPeerState { protected _name: PeerStateName; constructor(); get isInitial(): boolean; get isConnected(): boolean; get isDestroyed(): boolean; toInitial(): void; toConnected(): void; toDestroyed(): void; } declare class PeerStateName { static readonly INITIAL = "initial"; static readonly CONNECTED = "connected"; static readonly DESTROYED = "destroyed"; } export { SpecPeerBroker, SpecConstraintPeerBroker, PeerBroker, PeerBrokerInfo, PeerBrokerID, PeerEvent };