UNPKG

@bddh/starling-realtime-client

Version:

2 lines (1 loc) 6.13 kB
import{createClass as e,asyncToGenerator as t,regeneratorRuntime as n,typeof as r,classCallCheck as s,defineProperty as c,objectSpread2 as o}from"./_virtual/_rollupPluginBabelHelpers.js";import{v4 as a}from"uuid";import{createWebSocket as i}from"@bddh/starling-web-socket/es/create-web-socket";import u from"lodash/pick";import l from"lodash/omit";import h from"lodash/get";import{errorTypeEnum as d,CONNECT_MAIN_KEY as m,statusEnum as S,WS_STATUS_MAP as p}from"./interface.js";var f=3e4,k=function(f){function k(e,r){var f,v=this;s(this,k),c(this,"dhServerSocket",null),c(this,"preConnectSuccess",!1),c(this,"connectParams",null),c(this,"socketClosingPromise",null),c(this,"url",void 0),c(this,"firstConnect",!0),c(this,"connectingTimer",null),c(this,"connectingMs",3e4),c(this,"lastPreOpen",!1),c(this,"reconnectingTimeout",!1),c(this,"lastReportedReadyState",-1),c(this,"lastHbAckTs",0),c(this,"checkHeartbeatMismatch",void 0),c(this,"emitRawStatusMessage",void 0),c(this,"sendPreConnect",(function(e,t){var n,r=e.token,s=e.sessionId,c=e.onConnect;null===(n=v.dhServerSocket)||void 0===n||n.request({action:"RE_CONNECT",requestId:a(),body:JSON.stringify({token:r,sessionId:s})}).on((function(e,n){null==c||c(e),0!==e.code&&(n(),v.handleConnect()),null==t||t(v.dhServerSocket)})),v.firstConnect=!1})),c(this,"createSocket",(function(e,r){return(f=f||t(n().mark((function e(t,r){var s,c;return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(v.connectParams=t,v.lastPreOpen=!!r,s=t.sessionId,c=t.onDigitalHumanCallback,!v.socketClosingPromise){e.next=6;break}return e.next=6,v.socketClosingPromise;case 6:if(!v.dhServerSocket){e.next=9;break}return e.next=9,v.closeSocket();case 9:return e.abrupt("return",new Promise((function(e,n){var o=!1,a=function(t){o||(o=!0,e(t))},u={keepAlive:"heartbeat",checkHeartbeatMismatch:v.checkHeartbeatMismatch,reopen:h(v.connectParams,"reConnect",!0),getKeepAliveMessage:function(e){return{requestId:e,action:"HEART_BEAT",body:"".concat(v.lastHbAckTs),clientTs:Date.now()}},combine:function(e,t){return e&&t&&e.requestId===t.requestId},onOpen:function(){v.dhServerSocket=l,a(v.dhServerSocket),s&&v.firstConnect?v.sendPreConnect(t,a):r&&v.firstConnect||v.handleConnect()},onClose:function(){v.dhServerSocket=null,v.socketClosingPromise=null,a(null)},onError:function(e){n(e)}},l=i(v.url,u);l.onReadyStateChange((function(e){v.lastReportedReadyState=e,0===e?(v.startConnectingWatch(),c&&c({status:S.DH_LIB_INIT})):(v.clearConnectingWatch(),v.reconnectingTimeout=!1),1===e&&r&&(v.preConnectSuccess=!0),3===e&&(v.preConnectSuccess=!1),c&&c({status:S.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:e,body:p[e]}})})),l.onMessage((function(e){var t=e.action;"HEART_BEAT"===t&&(v.lastHbAckTs=Date.now()),[d.TIMEOUT_EXIT,d.DISCONNECT_ALERT].includes(t)&&(c&&c({status:S.DH_LIB_STATUS,content:{type:t}}),v.emitRawStatusMessage&&c&&c({status:S.DH_LIB_MESSAGE,content:e})),[d.TIMEOUT_EXIT,d.DISCONNECT_ALERT,"HEART_BEAT","CONNECT"].includes(t)||c&&c({status:S.DH_LIB_MESSAGE,content:e})})),v.dhServerSocket=l,l.open()})));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)})),c(this,"sendConnect",(function(e){v.connectParams=e,v.dhServerSocket&&v.handleConnect()})),c(this,"closeSocket",t(n().mark((function e(){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(v.resetState(),v.dhServerSocket){e.next=3;break}return e.abrupt("return");case 3:return v.socketClosingPromise=v.dhServerSocket.close(),e.next=6,v.socketClosingPromise;case 6:v.dhServerSocket=null;case 7:case"end":return e.stop()}}),e)})))),c(this,"handleConnect",t(n().mark((function e(){var t,r,s,c,i,h;return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(v.connectParams){e.next=2;break}return e.abrupt("return");case 2:if(t=v.connectParams,r=t.appId,s=t.appKey,c=t.token,i=t.parameters,h=t.onConnect,r||s||c){e.next=5;break}return e.abrupt("return");case 5:v.dhServerSocket&&v.dhServerSocket.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:a(),body:JSON.stringify(o(o({token:c,appId:r,appKey:s},u(v.connectParams,m)),{},{parameters:o(o({},l(i,["parameters"])),i&&i.parameters?i.parameters:{})}))}).on((function(e,t){null==h||h(e),0!==e.code&&t()}));case 6:case"end":return e.stop()}}),e)}))));var T=r||{},C=T.checkHeartbeatMismatch,g=T.emitRawStatusMessage,b=void 0!==g&&g;this.url=e,this.checkHeartbeatMismatch=null!=C&&C,this.emitRawStatusMessage=b}return e(k,[{key:"setEmitRawStatusMessage",value:function(e){this.emitRawStatusMessage=e}},{key:"sendMessage",value:function(e,s){return(f=f||t(n().mark((function e(t,s){var c;return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:try{this.dhServerSocket&&this.dhServerSocket&&this.dhServerSocket.request(t).on((function(e){s&&s(e)}))}catch(e){c="object"===r(t)&&"requestId"in t?t.requestId:a(),s&&s({code:-11,body:null,action:"RENDER_ERROR",requestId:c,message:JSON.stringify(e)})}case 1:case"end":return e.stop()}}),e,this)})))).apply(this,arguments)}},{key:"startConnectingWatch",value:function(){var e=this;this.connectingTimer||this.reconnectingTimeout||(this.connectingTimer=setTimeout(t(n().mark((function t(){return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.connectingTimer=null,e.dhServerSocket&&0===e.lastReportedReadyState){t.next=3;break}return t.abrupt("return");case 3:if(console.warn("reconnect from CONNECTING timeout"),e.reconnectingTimeout=!0,t.prev=5,!e.connectParams){t.next=9;break}return t.next=9,e.createSocket(e.connectParams,e.lastPreOpen);case 9:t.next=14;break;case 11:t.prev=11,t.t0=t.catch(5),console.error("reconnect from CONNECTING timeout failed",t.t0);case 14:return t.prev=14,e.reconnectingTimeout=!1,t.finish(14);case 17:case"end":return t.stop()}}),t,null,[[5,11,14,17]])}))),this.connectingMs))}},{key:"clearConnectingWatch",value:function(){this.connectingTimer&&(clearTimeout(this.connectingTimer),this.connectingTimer=null)}},{key:"resetState",value:function(){this.clearConnectingWatch(),this.reconnectingTimeout=!1,this.lastReportedReadyState=-1,this.lastHbAckTs=0}}]),k}();export{f as CONNECTING_STUCK_MS_DEFAULT,k as default};