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