import { Backoff } from "./backoff/backoff" ; import { Buffer } from "./buffer/buffer" ; import { RetryEventDetails , Websocket , WebsocketEvents } from "./websocket" ; /**
 * Used to build Websocket-instances.
 */ export class WebsocketBuilder { private readonly url : string ; private ws : Websocket | null = null ; private protocols ? : string | string [ ] ; private backoff ? : Backoff ; private buffer ? : Buffer < string | ArrayBufferLike | Blob | ArrayBufferView > ; private onOpenListeners : ( { listener : ( instance : Websocket , ev : Event ) => any ; options ? : boolean | EventListenerOptions ; } ) [ ] = [ ] ; private onCloseListeners : ( { listener : ( instance : Websocket , ev : CloseEvent ) => any ; options ? : boolean | EventListenerOptions ; } ) [ ] = [ ] ; private onErrorListeners : ( { listener : ( instance : Websocket , ev : Event ) => any ; options ? : boolean | EventListenerOptions ; } ) [ ] = [ ] ; private onMessageListeners : ( { listener : ( instance : Websocket , ev : MessageEvent ) => any ; options ? : boolean | EventListenerOptions ; } ) [ ] = [ ] ; private onRetryListeners : ( { listener : ( instance : Websocket , ev : CustomEvent < RetryEventDetails > ) => any ; options ? : boolean | EventListenerOptions ; } ) [ ] = [ ] ; constructor ( url ) { this . url = url ; } public withProtocols ( p ) { this . protocols = p ; return this ; } public withBackoff ( backoff ) { this . backoff = backoff ; return this ; } public withBuffer ( buffer ) { this . buffer = buffer ; return this ; } public onOpen ( listener , options ? ) { this . onOpenListeners . push ( { listener , options } ) ; return this ; } public onClose ( listener , options ? ) { this . onCloseListeners . push ( { listener , options } ) ; return this ; } public onError ( listener , options ? ) { this . onErrorListeners . push ( { listener , options } ) ; return this ; } public onMessage ( listener , options ? ) { this . onMessageListeners . push ( { listener , options } ) ; return this ; } public onRetry ( listener , options ? ) { this . onRetryListeners . push ( { listener , options } ) ; return this ; } /**
     * Multiple calls to build() will always return the same websocket-instance.
     */ public build ( ) { if ( this . ws !== null ) return this . ws ; this . ws = new Websocket ( this . url , this . protocols , this . buffer , this . backoff ) ; this . onOpenListeners . forEach ( h => this . ws ?. addEventListener ( WebsocketEvents . open , h . listener , h . options ) ) ; this . onCloseListeners . forEach ( h => this . ws ?. addEventListener ( WebsocketEvents . close , h . listener , h . options ) ) ; this . onErrorListeners . forEach ( h => this . ws ?. addEventListener ( WebsocketEvents . error , h . listener , h . options ) ) ; this . onMessageListeners . forEach ( h => this . ws ?. addEventListener ( WebsocketEvents . message , h . listener , h . options ) ) ; this . onRetryListeners . forEach ( h => this . ws ?. addEventListener ( WebsocketEvents . retry , h . listener , h . options ) ) ; return this . ws ; } }