"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.UnitySocket=void 0;var _socket=_interopRequireDefault(require("../../common/socket")),_websocketHeartbeatJs=_interopRequireDefault(require("websocket-heartbeat-js")),_modelApi=require("../model-api"),_enums=require("./enums"),_util=require("../../common/util");class UnitySocket extends _socket.default{constructor(a){super("unity-socket",a),this.promiseHandlers=new Map,this.notificationListeners=new Map,this.flushQueue=[]}dispatchNotification(a,b){a===_modelApi.NOTIFICATION_TYPE.LoadDataCompeted&&this.emit("loaded");const c=this.notificationListeners.get(a);null===c||void 0===c?void 0:c.forEach(a=>{a(b)})}dispatchResponse(a){const{Token:b,Status:c,ErrorMessage:d,Data:e}=a,f=this.promiseHandlers.get(b);if(f){switch(c){case _enums.ResponseStatus.success:f.resolve(e);break;case _enums.ResponseStatus.error:f.reject(new Error(d));break;default:f.reject(new Error("\u63A5\u53E3\u54CD\u5E94\u9519\u8BEF\uFF01"))}this.promiseHandlers.delete(b)}}dispatchMessage(a){let b;try{b="string"==typeof a?JSON.parse(a):a}catch(b){const c=new Error(`Unity 返回了非 JSON 数据：${a}`);return void this.emit("message_error",c,a)}switch(b.MessageType){case _enums.RequestMessageType.notification:this.dispatchNotification(b.NotificationType,b.Data);break;case _enums.RequestMessageType.response:this.dispatchResponse(b);break;default:this.emit("message",b)}}send(a){if(!this.ws)throw Error("ws is undefined.");"string"!=typeof a&&(a=JSON.stringify(a)),this.ws.send(a)}connect(){if(!this.ws){if(!this.config.url)throw new Error("\u672A\u6307\u5B9A Unity \u8FDE\u63A5\u5730\u5740\uFF01");this.emit("connecting"),this.ws=new _websocketHeartbeatJs.default({get pingMsg(){const a=(0,_util.createUUid)(),b=Date.now();return JSON.stringify({Token:a,MessageType:_enums.RequestMessageType.request,RequestType:"Heartbeat",Params:{token:a,msgTime:b}})},...this.config,url:this.config.url}),this.ws.onmessage=a=>{this.dispatchMessage(a.data)},this.ws.onopen=a=>{this.emit("connected",a),this.emit("open",a)},this.ws.onclose=a=>{this.emit("close",a),this.emit("disconnect",a)},this.ws.onerror=a=>{this.emit("error",a)}}}invoke(a,b){return new Promise((c,d)=>{const e=(0,_util.createUUid)(),f={MessageType:_enums.RequestMessageType.request,RequestType:a,Token:e,Params:b};this.promiseHandlers.set(e,{resolve:c,reject:d}),this.send(JSON.stringify(f))})}addNotificationListener(a,b){var c;const d=null!==(c=this.notificationListeners.get(a))&&void 0!==c?c:[];return 0===d.length&&this.invoke(_modelApi.REQUEST_TYPE.SubscribeNotification,{NotificationTypes:[a]}),d.includes(b)||(d.push(b),this.notificationListeners.set(a,d)),()=>{this.removeNotificationListener(a,b)}}removeNotificationListener(a,b){if(b){const c=this.notificationListeners.get(a);if(c){const d=c.filter(a=>a!==b);this.notificationListeners.set(a,d)}return!0}this.notificationListeners.delete(a);0===this.notificationListeners.size&&this.invoke(_modelApi.REQUEST_TYPE.UnsubscribeNotification,{NotificationTypes:[a]})}emit(a,...b){super.emit(a,...b)}addListener(a,b){return super.addListener(a,b)}removeListener(a,b){return super.removeListener(a,b)}removeAllListener(a){return super.removeAllListener(a)}once(a,b){return super.once(a,b)}disconnect(){super.close()}}exports.UnitySocket=UnitySocket;