import t from"@babel/runtime/helpers/classCallCheck";import e from"@babel/runtime/helpers/createClass";import n from"@babel/runtime/helpers/defineProperty";import{getHrefDebugField as r}from"./constants.js";function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function c(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?o(Object(r),!0).forEach((function(e){n(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):o(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var i=function(){function o(e){var r=this;t(this,o),n(this,"_socket",void 0),n(this,"onConnectionChange",(function(t){var e=function(){return t({status:"connected"})},n=function(e){return t({status:"disconnected",reason:e})},o=function(){return t({status:"reconnecting"})},c=function(e){return t({status:"error",reason:(null==e?void 0:e.message)||String(e)})};return r._socket.on("connect",e),r._socket.on("disconnect",n),r._socket.on("reconnect",o),r._socket.on("connect_error",c),function(){r._socket.off("connect",e),r._socket.off("disconnect",n),r._socket.off("reconnect",o),r._socket.off("connect_error",c)}})),this._socket=e}return e(o,[{key:"subscribe",value:function(t,e,n){var r=this;this._validateArgs(t,e,n),this._socket.on("data#".concat(t),e),this._emit("subscribe",t,n);var o=function(){return r.unsubscribe(t,e,n)};return Object.defineProperty(o,"name",{value:"unsubscribe_".concat(t.replace(/[^\w]/g,"_")),writable:!1}),o}},{key:"unsubscribe",value:function(t,e,n){this._validateArgs(t,e,n),this._emit("unsubscribe",t,n),this._socket.off("data#".concat(t),e)}},{key:"disconnect",value:function(){return this._socket.disconnect(),this._socket.removeAllListeners(),this}},{key:"_validateArgs",value:function(t,e,n){if("string"!=typeof t||!t)throw new Error("Channel must be a non-empty string");if("function"!=typeof e)throw new Error("onData handler must be a function");if(void 0!==n&&"function"!=typeof n)throw new Error("ack must be a function")}},{key:"_shouldEmitToServer",value:function(t){return 1===this._socket.listeners("data#".concat(t)).length}},{key:"_emit",value:function(t,e,n){if(!this._shouldEmitToServer(e))return null==n?void 0:n();n?this._socket.emitWithAck(t,c({channel:e},r()),(function(r){return r.ok?n():n(new Error("Failed to ".concat(t," to channel ").concat(e,": ").concat(r.error)))})):this._socket.emit(t,c({channel:e},r()))}}]),o}();export{i as SubscriptionsClientV2};
//# sourceMappingURL=subscriptions-client.js.map
