reconnecting-websocket-iife.min.js 4.9 KB

1
  1. var ReconnectingWebSocket=function(e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;class t{constructor(e,t){this.target=t,this.type=e}}class s extends t{constructor(e,t){super("error",t),this.message=e.message,this.error=e}}class i extends t{constructor(e=1e3,t="",s){super("close",s),this.wasClean=!0,this.code=e,this.reason=t}}const n=e?(t,s)=>e.setTimeout(t,s):setTimeout,o=e?t=>e.clearTimeout(t):clearTimeout,r=()=>{if("undefined"!=typeof WebSocket)return WebSocket},h=e=>"function"==typeof e&&2===e.CLOSING,c={maxReconnectionDelay:1e4,minReconnectionDelay:1e3+4e3*Math.random(),minUptime:5e3,reconnectionDelayGrowFactor:1.3,connectionTimeout:4e3,maxRetries:1/0,debug:!1};class _{constructor(e,t,s={}){this._listeners={error:[],message:[],open:[],close:[]},this._retryCount=-1,this._shouldReconnect=!0,this._connectLock=!1,this._binaryType="blob",this.eventToHandler=new Map([["open",this._handleOpen.bind(this)],["close",this._handleClose.bind(this)],["error",this._handleError.bind(this)],["message",this._handleMessage.bind(this)]]),this.onclose=void 0,this.onerror=void 0,this.onmessage=void 0,this.onopen=void 0,this._url=e,this._protocols=t,this._options=s,this._connect()}static get CONNECTING(){return 0}static get OPEN(){return 1}static get CLOSING(){return 2}static get CLOSED(){return 3}get CONNECTING(){return _.CONNECTING}get OPEN(){return _.OPEN}get CLOSING(){return _.CLOSING}get CLOSED(){return _.CLOSED}get binaryType(){return this._ws?this._ws.binaryType:this._binaryType}set binaryType(e){this._binaryType=e,this._ws&&(this._ws.binaryType=e)}get retryCount(){return Math.max(this._retryCount,0)}get bufferedAmount(){return this._ws?this._ws.bufferedAmount:0}get extensions(){return this._ws?this._ws.extensions:""}get protocol(){return this._ws?this._ws.protocol:""}get readyState(){return this._ws?this._ws.readyState:_.CONNECTING}get url(){return this._ws?this._ws.url:""}close(e,t){this._shouldReconnect=!1,this._ws&&this._ws.readyState!==this.CLOSED&&this._ws.close(e,t)}reconnect(e,t){this._shouldReconnect=!0,this._retryCount=-1,this._ws&&this._ws.readyState!==this.CLOSED||this._connect(),this._disconnect(e,t),this._connect()}send(e){this._ws&&this._ws.send(e)}addEventListener(e,t){this._listeners[e]&&this._listeners[e].push(t)}removeEventListener(e,t){this._listeners[e]&&(this._listeners[e]=this._listeners[e].filter(e=>e!==t))}_debug(...e){this._options.debug&&console.log("RWS>",...e)}_getNextDelay(){let e=0;if(this._retryCount>0){const{reconnectionDelayGrowFactor:t=c.reconnectionDelayGrowFactor,minReconnectionDelay:s=c.minReconnectionDelay,maxReconnectionDelay:i=c.maxReconnectionDelay}=this._options;(e=s+Math.pow(this._retryCount-1,t))>i&&(e=i)}return this._debug("next delay",e),e}_wait(){return new Promise(e=>{n(e,this._getNextDelay())})}_getNextUrl(e){if("string"==typeof e)return Promise.resolve(e);if("function"==typeof e){const t=e();if("string"==typeof t)return Promise.resolve(t);if(t.then)return t}throw Error("Invalid URL")}_connect(){if(this._connectLock)return;this._connectLock=!0;const{maxRetries:e=c.maxRetries,connectionTimeout:t=c.connectionTimeout,WebSocket:s=r()}=this._options;if(this._retryCount>=e)this._debug("max retries reached",this._retryCount,">=",e);else{if(this._retryCount++,this._debug("connect",this._retryCount),this._removeListeners(),!h(s))throw Error("No valid WebSocket class provided");this._wait().then(()=>this._getNextUrl(this._url)).then(e=>{this._debug("connect",{url:e,protocols:this._protocols}),this._ws=new s(e,this._protocols),this._ws.binaryType=this._binaryType,this._connectLock=!1,this._addListeners(),this._connectTimeout=n(()=>this._handleTimeout(),t)})}}_handleTimeout(){this._debug("timeout event"),this._handleError(new s(Error("TIMEOUT"),this))}_disconnect(e,t){if(o(this._connectTimeout),this._ws){this._removeListeners();try{this._ws.close(e,t),this._handleClose(new i(e,t,this))}catch(e){}}}_acceptOpen(){this._retryCount=0}_handleOpen(e){this._debug("open event");const{minUptime:t=c.minUptime}=this._options;o(this._connectTimeout),this._uptimeTimeout=n(()=>this._acceptOpen(),t),this._debug("assign binary type"),this._ws.binaryType=this._binaryType,this.onopen&&this.onopen(e),this._listeners.open.forEach(t=>t(e))}_handleMessage(e){this._debug("message event"),this.onmessage&&this.onmessage(e),this._listeners.message.forEach(t=>t(e))}_handleError(e){this._debug("error event",e.message),this._disconnect(void 0,"TIMEOUT"===e.message?"timeout":void 0),this.onerror&&this.onerror(e),this._debug("exec error listeners"),this._listeners.error.forEach(t=>t(e)),this._connect()}_handleClose(e){this._debug("close event"),this.onclose&&this.onclose(e),this._listeners.close.forEach(t=>t(e))}_removeListeners(){if(this._ws){this._debug("removeListeners");for(const[e,t]of this.eventToHandler)this._ws.removeEventListener(e,t)}}_addListeners(){this._debug("addListeners");for(const[e,t]of this.eventToHandler)this._ws.addEventListener(e,t)}}return _}(RNTimer);