UNPKG

@bddh/starling-realtime-client

Version:

2 lines (1 loc) 3.44 kB
import{createClass as e,asyncToGenerator as t,classCallCheck as r,defineProperty as n,regeneratorRuntime as o,objectSpread2 as s}from"./_virtual/_rollupPluginBabelHelpers.js";import{v4 as a}from"uuid";import{createWebSocket as c}from"@bddh/starling-web-socket/es/create-web-socket";import i from"lodash/pick";import u from"lodash/omit";import{errorTypeEnum as l,CONNECT_MAIN_KEY as p,statusEnum as d,WS_STATUS_MAP as f}from"./interface.js";var h=function(h){function k(e){var h,m=this;r(this,k),n(this,"dhServerSocket",null),n(this,"socketClosingPromise",null),n(this,"url",void 0),n(this,"firstConnect",!0),n(this,"connectParams",null),n(this,"createSocket",(function(e){return(h=h||t(o().mark((function e(t){var r,n,s,i;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(m.connectParams=t,r=t.token,n=t.sessionId,s=t.onConnect,i=t.onDigitalHumanCallback,!m.socketClosingPromise){e.next=5;break}return e.next=5,m.socketClosingPromise;case 5:if(!m.dhServerSocket){e.next=8;break}return e.next=8,m.closeSocket();case 8:return e.abrupt("return",new Promise((function(e,t){var o=c(m.url,{reopen:!0,keepAlive:"heartbeat",combine:function(e,t){return e&&t&&e.requestId===t.requestId},onOpen:function(){if(m.dhServerSocket=o,n&&m.firstConnect)return o.request({action:"RE_CONNECT",requestId:a(),body:JSON.stringify({token:r,sessionId:n})}).on((function(t,r){s(t),0!==t.code&&(r(),m.handleConnect()),e(o)})),void(m.firstConnect=!1);m.handleConnect()},onClose:function(){m.dhServerSocket=null,m.socketClosingPromise=null},onError:function(e){t(e)}});o.onReadyStateChange((function(e){0===e&&i&&i({status:d.DH_LIB_INIT}),i&&i({status:d.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:e,body:f[e]}})})),o.onMessage((function(e){var t=e.action;[l.TIMEOUT_EXIT,l.DISCONNECT_ALERT].includes(t)&&i&&i({status:d.DH_LIB_STATUS,content:{type:t}})})),o.open()})));case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)})),n(this,"closeSocket",t(o().mark((function e(){return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(m.dhServerSocket){e.next=2;break}return e.abrupt("return");case 2:return m.socketClosingPromise=m.dhServerSocket.close(),e.next=5,m.socketClosingPromise;case 5:m.dhServerSocket=null;case 6:case"end":return e.stop()}}),e)})))),n(this,"handleConnect",t(o().mark((function e(){var t,r,n,c,l,d;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(m.connectParams){e.next=2;break}return e.abrupt("return");case 2:if(t=m.connectParams,r=t.appId,n=t.appKey,c=t.token,l=t.parameters,d=t.onConnect,r||n||c){e.next=5;break}return e.abrupt("return");case 5:m.dhServerSocket&&m.dhServerSocket.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:a(),body:JSON.stringify(s(s({token:c,appId:r,appKey:n},i(m.connectParams,p)),{},{parameters:s(s({},u(l,["parameters"])),l&&l.parameters?l.parameters:{})}))}).on((function(e,t){d(e),0!==e.code&&t()}));case 6:case"end":return e.stop()}}),e)})))),this.url=e}return e(k,[{key:"sendMessage",value:function(e,r){return(h=h||t(o().mark((function e(t,r){return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:try{this.dhServerSocket&&this.dhServerSocket&&this.dhServerSocket.request(t).on((function(e){r&&r(e)}))}catch(e){r&&r({code:-11,body:null,action:"RENDER_ERROR",requestId:t.requestId,message:JSON.stringify(e)})}case 1:case"end":return e.stop()}}),e,this)})))).apply(this,arguments)}}]),k}();export{h as default};