mirror of
https://github.com/tenrok/vue-native-websocket.git
synced 2026-06-06 21:02:51 +03:00
2 lines
5.7 KiB
JavaScript
Executable File
2 lines
5.7 KiB
JavaScript
Executable File
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.VueNativeSock=e():t.VueNativeSock=e()}(self,(()=>(()=>{"use strict";var t={d:(e,o)=>{for(var n in o)t.o(o,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:o[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function o(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}t.r(e),t.d(e,{default:()=>c});const n=new(function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.listeners=new Map}var e,n,r;return e=t,n=[{key:"addListener",value:function(t,e,o){return"function"==typeof e&&(this.listeners.has(t)||this.listeners.set(t,[]),this.listeners.get(t).push({callback:e,vm:o}),!0)}},{key:"removeListener",value:function(t,e,o){var n,r=this.listeners.get(t);return!!(r&&r.length&&(n=r.reduce((function(t,n,r){return"function"==typeof n.callback&&n.callback===e&&n.vm===o&&(t=r),t}),-1))>-1)&&(r.splice(n,1),this.listeners.set(t,r),!0)}},{key:"emit",value:function(t){for(var e=arguments.length,o=new Array(e>1?e-1:0),n=1;n<e;n++)o[n-1]=arguments[n];var r=this.listeners.get(t);return!(!r||!r.length||(r.forEach((function(t){var e;(e=t.callback).call.apply(e,[t.vm].concat(o))})),0))}}],n&&o(e.prototype,n),r&&o(e,r),Object.defineProperty(e,"prototype",{writable:!1}),t}());function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var s=function(){function t(e){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(r(this,t),this.format=o.format&&o.format.toLowerCase(),e.startsWith("//")){var n="https:"===window.location.protocol?"wss":"ws";e="".concat(n,":").concat(e)}this.connectionUrl=e,this.opts=o,this.reconnection=this.opts.reconnection||!1,this.reconnectionAttempts=this.opts.reconnectionAttempts||1/0,this.reconnectionDelay=this.opts.reconnectionDelay||1e3,this.reconnectTimeoutId=0,this.reconnectionCount=0,this.passToStoreHandler=this.opts.passToStoreHandler||!1,this.connect(e,o),o.store&&(this.store=o.store),o.mutations&&(this.mutations=o.mutations),this.onEvent()}var e,o,s;return e=t,o=[{key:"connect",value:function(t){var e=this,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=o.protocol||"";return this.WebSocket=o.WebSocket||(""===n?new WebSocket(t):new WebSocket(t,n)),"json"===this.format&&("sendObj"in this.WebSocket||(this.WebSocket.sendObj=function(t){return e.WebSocket.send(JSON.stringify(t))})),this.WebSocket}},{key:"reconnect",value:function(){var t=this;this.reconnectionCount<=this.reconnectionAttempts?(this.reconnectionCount++,clearTimeout(this.reconnectTimeoutId),this.reconnectTimeoutId=setTimeout((function(){t.store&&t.passToStore("SOCKET_RECONNECT",t.reconnectionCount),t.connect(t.connectionUrl,t.opts),t.onEvent()}),this.reconnectionDelay)):this.store&&this.passToStore("SOCKET_RECONNECT_ERROR",!0)}},{key:"onEvent",value:function(){var t=this;["onmessage","onclose","onerror","onopen"].forEach((function(e){t.WebSocket[e]=function(o){n.emit(e,o),t.store&&t.passToStore("SOCKET_"+e,o),t.reconnection&&"onopen"===e&&(t.opts.$setInstance(o.currentTarget),t.reconnectionCount=0),t.reconnection&&"onclose"===e&&t.reconnect()}}))}},{key:"passToStore",value:function(t,e){this.passToStoreHandler?this.passToStoreHandler(t,e,this.defaultPassToStore.bind(this)):this.defaultPassToStore(t,e)}},{key:"defaultPassToStore",value:function(t,e){if(t.startsWith("SOCKET_")){var o="commit",n=t.toUpperCase(),r=e;"json"===this.format&&e.data&&((r=JSON.parse(e.data)).mutation?n=[r.namespace||"",r.mutation].filter((function(t){return!!t})).join("/"):r.action&&(o="dispatch",n=[r.namespace||"",r.action].filter((function(t){return!!t})).join("/"))),this.mutations&&(n=this.mutations[n]||n),this.store[o](n,r)}}}],o&&i(e.prototype,o),s&&i(e,s),Object.defineProperty(e,"prototype",{writable:!1}),t}();const c={install:function(t,e){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!e&&!o.connectManually)throw new Error("[vue-native-socket] cannot locate connection");var r=null;o.$setInstance=function(e){t.prototype.$socket=e},o.connectManually?(t.prototype.$connect=function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:e,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o;i.$setInstance=o.$setInstance,r=new s(n,i),t.prototype.$socket=r.WebSocket},t.prototype.$disconnect=function(){r&&r.reconnection&&(r.reconnection=!1),t.prototype.$socket&&(t.prototype.$socket.close(),delete t.prototype.$socket)}):(r=new s(e,o),t.prototype.$socket=r.WebSocket);var i="undefined"!=typeof Proxy&&"function"==typeof Proxy&&/native code/.test(Proxy.toString());t.mixin({created:function(){var t=this,e=this,o=this.$options.sockets;i?(this.$options.sockets=new Proxy({},{set:function(t,o,r){return n.addListener(o,r,e),t[o]=r,!0},deleteProperty:function(t,o){return n.removeListener(o,e.$options.sockets[o],e),delete t.key,!0}}),o&&Object.keys(o).forEach((function(e){t.$options.sockets[e]=o[e]}))):(Object.seal(this.$options.sockets),o&&Object.keys(o).forEach((function(t){n.addListener(t,o[t],e)})))},beforeDestroy:function(){var t=this;if(i){var e=this.$options.sockets;e&&Object.keys(e).forEach((function(e){delete t.$options.sockets[e]}))}}})}};return e})()));
|
|
//# sourceMappingURL=build.js.map
|