From 379143d3c94c2b9f04ce71e2e9cb0eef0234e419 Mon Sep 17 00:00:00 2001 From: Alexander Mikhailian Date: Tue, 11 Sep 2018 22:39:49 +0200 Subject: [PATCH] Check for Proxy before using it --- dist/build.js | 2 +- src/Main.js | 48 +++++++++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/dist/build.js b/dist/build.js index 598b4d3..43ee47c 100755 --- a/dist/build.js +++ b/dist/build.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.VueNativeSock=t():e.VueNativeSock=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,n){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n-1)&&(o.splice(r,1),this.listeners.set(e,o),!0)}},{key:"emit",value:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),o=1;o2&&void 0!==arguments[2]?arguments[2]:{};if(!t)throw new Error("[vue-native-socket] cannot locate connection");var o=null;n.$setInstance=function(t){e.prototype.$socket=t},n.connectManually?(e.prototype.$connect=function(){o=new i.default(t,n),e.prototype.$socket=o.WebSocket},e.prototype.$disconnect=function(){o&&o.reconnection&&(o.reconnection=!1),e.prototype.$socket&&(e.prototype.$socket.close(),delete e.prototype.$socket)}):(o=new i.default(t,n),e.prototype.$socket=o.WebSocket),e.mixin({created:function(){var e=this,t=this,n=this.$options.sockets;this.$options.sockets=new Proxy({},{set:function(e,n,o){return s.default.addListener(n,o,t),e[n]=o,!0},deleteProperty:function(e,n){return s.default.removeListener(n,t.$options.sockets[n],t),delete e.key,!0}}),n&&Object.keys(n).forEach(function(t){e.$options.sockets[t]=n[t]})},beforeDestroy:function(){var e=this,t=this.$options.sockets;t&&Object.keys(t).forEach(function(t){delete e.$options.sockets[t]})}})}}},function(e,t,n){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n1&&void 0!==arguments[1]?arguments[1]:{};o(this,e),this.format=n.format&&n.format.toLowerCase(),this.connectionUrl=t,this.opts=n,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(t,n),n.store&&(this.store=n.store),this.onEvent()}return r(e,[{key:"connect",value:function(e){var t=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.protocol||"";return this.WebSocket=n.WebSocket||(""===o?new WebSocket(e):new WebSocket(e,o)),"json"===this.format&&("sendObj"in this.WebSocket||(this.WebSocket.sendObj=function(e){return t.WebSocket.send(JSON.stringify(e))})),this.WebSocket}},{key:"reconnect",value:function(){var e=this;this.reconnectionCount<=this.reconnectionAttempts?(this.reconnectionCount++,clearTimeout(this.reconnectTimeoutId),this.reconnectTimeoutId=setTimeout(function(){e.store&&e.passToStore("SOCKET_RECONNECT",e.reconnectionCount),e.connect(e.connectionUrl,e.opts),e.onEvent()},this.reconnectionDelay)):this.store&&this.passToStore("SOCKET_RECONNECT_ERROR",!0)}},{key:"onEvent",value:function(){var e=this;["onmessage","onclose","onerror","onopen"].forEach(function(t){e.WebSocket[t]=function(n){c.default.emit(t,n),e.store&&e.passToStore("SOCKET_"+t,n),e.reconnection&&"onopen"===t&&(e.opts.$setInstance(n.currentTarget),e.reconnectionCount=0),e.reconnection&&"onclose"===t&&e.reconnect()}})}},{key:"passToStore",value:function(e,t){this.passToStoreHandler?this.passToStoreHandler(e,t,this.defaultPassToStore.bind(this)):this.defaultPassToStore(e,t)}},{key:"defaultPassToStore",value:function(e,t){if(e.startsWith("SOCKET_")){var n="commit",o=e.toUpperCase(),r=t;"json"===this.format&&t.data&&(r=JSON.parse(t.data),r.mutation?o=[r.namespace||"",r.mutation].filter(function(e){return!!e}).join("/"):r.action&&(n="dispatch",o=[r.namespace||"",r.action].filter(function(e){return!!e}).join("/"))),this.store[n](o,r)}}}]),e}();t.default=s}])}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.VueNativeSock=t():e.VueNativeSock=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,n){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n-1)&&(o.splice(r,1),this.listeners.set(e,o),!0)}},{key:"emit",value:function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),o=1;o2&&void 0!==arguments[2]?arguments[2]:{};if(!t)throw new Error("[vue-native-socket] cannot locate connection");var o=null;n.$setInstance=function(t){e.prototype.$socket=t},n.connectManually?(e.prototype.$connect=function(){o=new i.default(t,n),e.prototype.$socket=o.WebSocket},e.prototype.$disconnect=function(){o&&o.reconnection&&(o.reconnection=!1),e.prototype.$socket&&(e.prototype.$socket.close(),delete e.prototype.$socket)}):(o=new i.default(t,n),e.prototype.$socket=o.WebSocket);var r="undefined"!=typeof Proxy&&"function"==typeof Proxy&&/native code/.test(Proxy.toString());e.mixin({created:function(){var e=this,t=this,n=this.$options.sockets;r?(this.$options.sockets=new Proxy({},{set:function(e,n,o){return c.default.addListener(n,o,t),e[n]=o,!0},deleteProperty:function(e,n){return c.default.removeListener(n,t.$options.sockets[n],t),delete e.key,!0}}),n&&Object.keys(n).forEach(function(t){e.$options.sockets[t]=n[t]})):Object.seal(this.$options.sockets)},beforeDestroy:function(){var e=this;if(r){var t=this.$options.sockets;t&&Object.keys(t).forEach(function(t){delete e.$options.sockets[t]})}}})}}},function(e,t,n){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n1&&void 0!==arguments[1]?arguments[1]:{};o(this,e),this.format=n.format&&n.format.toLowerCase(),this.connectionUrl=t,this.opts=n,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(t,n),n.store&&(this.store=n.store),this.onEvent()}return r(e,[{key:"connect",value:function(e){var t=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=n.protocol||"";return this.WebSocket=n.WebSocket||(""===o?new WebSocket(e):new WebSocket(e,o)),"json"===this.format&&("sendObj"in this.WebSocket||(this.WebSocket.sendObj=function(e){return t.WebSocket.send(JSON.stringify(e))})),this.WebSocket}},{key:"reconnect",value:function(){var e=this;this.reconnectionCount<=this.reconnectionAttempts?(this.reconnectionCount++,clearTimeout(this.reconnectTimeoutId),this.reconnectTimeoutId=setTimeout(function(){e.store&&e.passToStore("SOCKET_RECONNECT",e.reconnectionCount),e.connect(e.connectionUrl,e.opts),e.onEvent()},this.reconnectionDelay)):this.store&&this.passToStore("SOCKET_RECONNECT_ERROR",!0)}},{key:"onEvent",value:function(){var e=this;["onmessage","onclose","onerror","onopen"].forEach(function(t){e.WebSocket[t]=function(n){s.default.emit(t,n),e.store&&e.passToStore("SOCKET_"+t,n),e.reconnection&&"onopen"===t&&(e.opts.$setInstance(n.currentTarget),e.reconnectionCount=0),e.reconnection&&"onclose"===t&&e.reconnect()}})}},{key:"passToStore",value:function(e,t){this.passToStoreHandler?this.passToStoreHandler(e,t,this.defaultPassToStore.bind(this)):this.defaultPassToStore(e,t)}},{key:"defaultPassToStore",value:function(e,t){if(e.startsWith("SOCKET_")){var n="commit",o=e.toUpperCase(),r=t;"json"===this.format&&t.data&&(r=JSON.parse(t.data),r.mutation?o=[r.namespace||"",r.mutation].filter(function(e){return!!e}).join("/"):r.action&&(n="dispatch",o=[r.namespace||"",r.action].filter(function(e){return!!e}).join("/"))),this.store[n](o,r)}}}]),e}();t.default=c}])}); \ No newline at end of file diff --git a/src/Main.js b/src/Main.js index b278dfc..53ab66d 100755 --- a/src/Main.js +++ b/src/Main.js @@ -29,38 +29,44 @@ export default { observer = new Observer(connection, opts) Vue.prototype.$socket = observer.WebSocket } + const hasProxy = typeof Proxy !== 'undefined' && typeof Proxy === 'function' && /native code/.test(Proxy.toString()) Vue.mixin({ created () { let vm = this let sockets = this.$options['sockets'] - this.$options.sockets = new Proxy({}, { - set (target, key, value) { - Emitter.addListener(key, value, vm) - target[key] = value - return true - }, - deleteProperty (target, key) { - Emitter.removeListener(key, vm.$options.sockets[key], vm) - delete target.key - return true - } - }) - - if (sockets) { - Object.keys(sockets).forEach((key) => { - this.$options.sockets[key] = sockets[key] + if (hasProxy) { + this.$options.sockets = new Proxy({}, { + set (target, key, value) { + Emitter.addListener(key, value, vm) + target[key] = value + return true + }, + deleteProperty (target, key) { + Emitter.removeListener(key, vm.$options.sockets[key], vm) + delete target.key + return true + } }) + if (sockets) { + Object.keys(sockets).forEach((key) => { + this.$options.sockets[key] = sockets[key] + }) + } + } else { + Object.seal(this.$options.sockets) } }, beforeDestroy () { - let sockets = this.$options['sockets'] + if (hasProxy) { + let sockets = this.$options['sockets'] - if (sockets) { - Object.keys(sockets).forEach((key) => { - delete this.$options.sockets[key] - }) + if (sockets) { + Object.keys(sockets).forEach((key) => { + delete this.$options.sockets[key] + }) + } } } })