UNPKG

vue-echo

Version:

Vue integration for the Laravel Echo library.

2 lines 14.1 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.VueEcho=t():e.VueEcho=t()}(this,function(){return function(e){function t(i){if(n[i])return n[i].exports;var o=n[i]={exports:{},id:i,loaded:!1};return e[i].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){e.exports=n(1)},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},r=n(2),s=i(r);t.default={install:function(e,t){if(!t)throw new Error("[Vue-Echo] cannot locate options");if("object"!==("undefined"==typeof t?"undefined":o(t)))throw new Error("[Vue-Echo] cannot initiate options");"function"==typeof t.socketId?e.prototype.$echo=t:e.prototype.$echo=new s.default(t),e.mixin({mounted:function(){var e=this.$options.channel;if(e){e.startsWith("private:")?this.channel=this.$echo.private(e.replace("private:","")):e.startsWith("presence:")?this.channel=this.$echo.join(e.replace("presence:","")):this.channel=this.$echo.channel(e);var t=this.$options.echo;t&&Object.keys(t).forEach(function(e){var n=this;this.channel.listen(e,function(i){t[e](i,n)})},this)}},beforeDestroy:function(){var e=this.$options.channel;e&&(e.startsWith("private:")?e=e.replace("private:",""):e.startsWith("presence:")&&(e=e.replace("presence:","")),this.$echo.leave(e))}})}}},function(e,t){var n=(function(){function e(e){this.value=e}function t(t){function n(e,t){return new Promise(function(n,o){var u={key:e,arg:t,resolve:n,reject:o,next:null};s?s=s.next=u:(r=s=u,i(e,t))})}function i(n,r){try{var s=t[n](r),u=s.value;u instanceof e?Promise.resolve(u.value).then(function(e){i("next",e)},function(e){i("throw",e)}):o(s.done?"return":"normal",s.value)}catch(e){o("throw",e)}}function o(e,t){switch(e){case"return":r.resolve({value:t,done:!0});break;case"throw":r.reject(t);break;default:r.resolve({value:t,done:!1})}r=r.next,r?i(r.key,r.arg):s=null}var r,s;this._invoke=n,"function"!=typeof t.return&&(this.return=void 0)}return"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)},{wrap:function(e){return function(){return new t(e.apply(this,arguments))}},await:function(t){return new e(t)}}}(),function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}),i=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},r=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},s=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},u=function(){function e(t){n(this,e),this._defaultOptions={auth:{headers:{}},authEndpoint:"/broadcasting/auth",broadcaster:"pusher",csrfToken:null,host:null,key:null,namespace:"App.Events"},this.setOptions(t),this.connect()}return i(e,[{key:"setOptions",value:function(e){return this.options=o(this._defaultOptions,e),this.csrfToken()&&(this.options.auth.headers["X-CSRF-TOKEN"]=this.csrfToken()),e}},{key:"csrfToken",value:function(){var e=void 0;return"undefined"!=typeof window&&window.Laravel&&window.Laravel.csrfToken?window.Laravel.csrfToken:this.options.csrfToken?this.options.csrfToken:"undefined"!=typeof document&&(e=document.querySelector('meta[name="csrf-token"]'))?e.getAttribute("content"):null}}]),e}(),c=function(){function e(){n(this,e)}return i(e,[{key:"notification",value:function(e){return this.listen(".Illuminate\\Notifications\\Events\\BroadcastNotificationCreated",e)}},{key:"listenForWhisper",value:function(e,t){return this.listen(".client-"+e,t)}}]),e}(),a=function(){function e(t){n(this,e),this.setNamespace(t)}return i(e,[{key:"format",value:function(e){return"."===e.charAt(0)||"\\"===e.charAt(0)?e.substr(1):(this.namespace&&(e=this.namespace+"."+e),e.replace(/\./g,"\\"))}},{key:"setNamespace",value:function(e){this.namespace=e}}]),e}(),h=function(e){function t(e,i,o){n(this,t);var r=s(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));return r.name=i,r.pusher=e,r.options=o,r.eventFormatter=new a(r.options.namespace),r.subscribe(),r}return r(t,e),i(t,[{key:"subscribe",value:function(){this.subscription=this.pusher.subscribe(this.name)}},{key:"unsubscribe",value:function(){this.pusher.unsubscribe(this.name)}},{key:"listen",value:function(e,t){return this.on(this.eventFormatter.format(e),t),this}},{key:"stopListening",value:function(e){return this.subscription.unbind(this.eventFormatter.format(e)),this}},{key:"on",value:function(e,t){return this.subscription.bind(e,t),this}}]),t}(c),l=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"whisper",value:function(e,t){return this.pusher.channels.channels[this.name].trigger("client-"+e,t),this}}]),t}(h),f=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"here",value:function(e){return this.on("pusher:subscription_succeeded",function(t){e(Object.keys(t.members).map(function(e){return t.members[e]}))}),this}},{key:"joining",value:function(e){return this.on("pusher:member_added",function(t){e(t.info)}),this}},{key:"leaving",value:function(e){return this.on("pusher:member_removed",function(t){e(t.info)}),this}},{key:"whisper",value:function(e,t){return this.pusher.channels.channels[this.name].trigger("client-"+e,t),this}}]),t}(h),p=function(e){function t(e,i,o){n(this,t);var r=s(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));return r.events={},r.name=i,r.socket=e,r.options=o,r.eventFormatter=new a(r.options.namespace),r.subscribe(),r.configureReconnector(),r}return r(t,e),i(t,[{key:"subscribe",value:function(){this.socket.emit("subscribe",{channel:this.name,auth:this.options.auth||{}})}},{key:"unsubscribe",value:function(){this.unbind(),this.socket.emit("unsubscribe",{channel:this.name,auth:this.options.auth||{}})}},{key:"listen",value:function(e,t){return this.on(this.eventFormatter.format(e),t),this}},{key:"on",value:function(e,t){var n=this,i=function(e,i){n.name==e&&t(i)};this.socket.on(e,i),this.bind(e,i)}},{key:"configureReconnector",value:function(){var e=this,t=function(){e.subscribe()};this.socket.on("reconnect",t),this.bind("reconnect",t)}},{key:"bind",value:function(e,t){this.events[e]=this.events[e]||[],this.events[e].push(t)}},{key:"unbind",value:function(){var e=this;Object.keys(this.events).forEach(function(t){e.events[t].forEach(function(n){e.socket.removeListener(t,n)}),delete e.events[t]})}}]),t}(c),v=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"whisper",value:function(e,t){return this.socket.emit("client event",{channel:this.name,event:"client-"+e,data:t}),this}}]),t}(p),y=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"here",value:function(e){return this.on("presence:subscribed",function(t){e(t.map(function(e){return e.user_info}))}),this}},{key:"joining",value:function(e){return this.on("presence:joining",function(t){return e(t.user_info)}),this}},{key:"leaving",value:function(e){return this.on("presence:leaving",function(t){return e(t.user_info)}),this}}]),t}(v),k=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"subscribe",value:function(){}},{key:"unsubscribe",value:function(){}},{key:"listen",value:function(e,t){return this}},{key:"stopListening",value:function(e){return this}},{key:"on",value:function(e,t){return this}}]),t}(c),b=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"whisper",value:function(e,t){return this}}]),t}(k),d=function(e){function t(){return n(this,t),s(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return r(t,e),i(t,[{key:"here",value:function(e){return this}},{key:"joining",value:function(e){return this}},{key:"leaving",value:function(e){return this}},{key:"whisper",value:function(e,t){return this}}]),t}(k),m=function(e){function t(){var e;n(this,t);for(var i=arguments.length,o=Array(i),r=0;r<i;r++)o[r]=arguments[r];var u=s(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(o)));return u.channels={},u}return r(t,e),i(t,[{key:"connect",value:function(){this.pusher=new Pusher(this.options.key,this.options)}},{key:"listen",value:function(e,t,n){return this.channel(e).listen(t,n)}},{key:"channel",value:function(e){return this.channels[e]||(this.channels[e]=new h(this.pusher,e,this.options)),this.channels[e]}},{key:"privateChannel",value:function(e){return this.channels["private-"+e]||(this.channels["private-"+e]=new l(this.pusher,"private-"+e,this.options)),this.channels["private-"+e]}},{key:"presenceChannel",value:function(e){return this.channels["presence-"+e]||(this.channels["presence-"+e]=new f(this.pusher,"presence-"+e,this.options)),this.channels["presence-"+e]}},{key:"leave",value:function(e){var t=this,n=[e,"private-"+e,"presence-"+e];n.forEach(function(e,n){t.channels[e]&&(t.channels[e].unsubscribe(),delete t.channels[e])})}},{key:"socketId",value:function(){return this.pusher.connection.socket_id}},{key:"disconnect",value:function(){this.pusher.disconnect()}}]),t}(u),_=function(e){function t(){var e;n(this,t);for(var i=arguments.length,o=Array(i),r=0;r<i;r++)o[r]=arguments[r];var u=s(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(o)));return u.channels={},u}return r(t,e),i(t,[{key:"connect",value:function(){var e=this.getSocketIO();return this.socket=e(this.options.host,this.options),this.socket}},{key:"getSocketIO",value:function(){if("undefined"!=typeof io)return io;if("undefined"!==this.options.client)return this.options.client;throw new Error("Socket.io client not found. Should be globally available or passed via options.client")}},{key:"listen",value:function(e,t,n){return this.channel(e).listen(t,n)}},{key:"channel",value:function(e){return this.channels[e]||(this.channels[e]=new p(this.socket,e,this.options)),this.channels[e]}},{key:"privateChannel",value:function(e){return this.channels["private-"+e]||(this.channels["private-"+e]=new v(this.socket,"private-"+e,this.options)),this.channels["private-"+e]}},{key:"presenceChannel",value:function(e){return this.channels["presence-"+e]||(this.channels["presence-"+e]=new y(this.socket,"presence-"+e,this.options)),this.channels["presence-"+e]}},{key:"leave",value:function(e){var t=this,n=[e,"private-"+e,"presence-"+e];n.forEach(function(e){t.channels[e]&&(t.channels[e].unsubscribe(),delete t.channels[e])})}},{key:"socketId",value:function(){return this.socket.id}},{key:"disconnect",value:function(){this.socket.disconnect()}}]),t}(u),w=function(e){function t(){var e;n(this,t);for(var i=arguments.length,o=Array(i),r=0;r<i;r++)o[r]=arguments[r];var u=s(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(o)));return u.channels={},u}return r(t,e),i(t,[{key:"connect",value:function(){}},{key:"listen",value:function(e,t,n){return new k}},{key:"channel",value:function(e){return new k}},{key:"privateChannel",value:function(e){return new b}},{key:"presenceChannel",value:function(e){return new d}},{key:"leave",value:function(e){}},{key:"socketId",value:function(){return"fake-socket-id"}},{key:"disconnect",value:function(){}}]),t}(u),g=function(){function e(t){n(this,e),this.options=t,"function"==typeof Vue&&Vue.http&&this.registerVueRequestInterceptor(),"function"==typeof axios&&this.registerAxiosRequestInterceptor(),"function"==typeof jQuery&&this.registerjQueryAjaxSetup(),"pusher"==this.options.broadcaster?this.connector=new m(this.options):"socket.io"==this.options.broadcaster?this.connector=new _(this.options):"null"==this.options.broadcaster&&(this.connector=new w(this.options))}return i(e,[{key:"registerVueRequestInterceptor",value:function(){var e=this;Vue.http.interceptors.push(function(t,n){e.socketId()&&t.headers.set("X-Socket-ID",e.socketId()),n()})}},{key:"registerAxiosRequestInterceptor",value:function(){var e=this;axios.interceptors.request.use(function(t){return e.socketId()&&(t.headers["X-Socket-Id"]=e.socketId()),t})}},{key:"registerjQueryAjaxSetup",value:function(){var e=this;"undefined"!=typeof jQuery.ajax&&jQuery.ajaxSetup({beforeSend:function(t){e.socketId()&&t.setRequestHeader("X-Socket-Id",e.socketId())}})}},{key:"listen",value:function(e,t,n){return this.connector.listen(e,t,n)}},{key:"channel",value:function(e){return this.connector.channel(e)}},{key:"private",value:function(e){return this.connector.privateChannel(e)}},{key:"join",value:function(e){return this.connector.presenceChannel(e)}},{key:"leave",value:function(e){this.connector.leave(e)}},{key:"socketId",value:function(){return this.connector.socketId()}},{key:"disconnect",value:function(){this.connector.disconnect()}}]),e}();e.exports=g}])}); //# sourceMappingURL=build.js.map