1 | var Echo = (function () {
|
2 | 'use strict';
|
3 |
|
4 | function _classCallCheck(instance, Constructor) {
|
5 | if (!(instance instanceof Constructor)) {
|
6 | throw new TypeError("Cannot call a class as a function");
|
7 | }
|
8 | }
|
9 |
|
10 | function _defineProperties(target, props) {
|
11 | for (var i = 0; i < props.length; i++) {
|
12 | var descriptor = props[i];
|
13 | descriptor.enumerable = descriptor.enumerable || false;
|
14 | descriptor.configurable = true;
|
15 | if ("value" in descriptor) descriptor.writable = true;
|
16 | Object.defineProperty(target, descriptor.key, descriptor);
|
17 | }
|
18 | }
|
19 |
|
20 | function _createClass(Constructor, protoProps, staticProps) {
|
21 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
22 | if (staticProps) _defineProperties(Constructor, staticProps);
|
23 | return Constructor;
|
24 | }
|
25 |
|
26 | function _extends() {
|
27 | _extends = Object.assign || function (target) {
|
28 | for (var i = 1; i < arguments.length; i++) {
|
29 | var source = arguments[i];
|
30 |
|
31 | for (var key in source) {
|
32 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
33 | target[key] = source[key];
|
34 | }
|
35 | }
|
36 | }
|
37 |
|
38 | return target;
|
39 | };
|
40 |
|
41 | return _extends.apply(this, arguments);
|
42 | }
|
43 |
|
44 | function _inherits(subClass, superClass) {
|
45 | if (typeof superClass !== "function" && superClass !== null) {
|
46 | throw new TypeError("Super expression must either be null or a function");
|
47 | }
|
48 |
|
49 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
50 | constructor: {
|
51 | value: subClass,
|
52 | writable: true,
|
53 | configurable: true
|
54 | }
|
55 | });
|
56 | if (superClass) _setPrototypeOf(subClass, superClass);
|
57 | }
|
58 |
|
59 | function _getPrototypeOf(o) {
|
60 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
61 | return o.__proto__ || Object.getPrototypeOf(o);
|
62 | };
|
63 | return _getPrototypeOf(o);
|
64 | }
|
65 |
|
66 | function _setPrototypeOf(o, p) {
|
67 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
68 | o.__proto__ = p;
|
69 | return o;
|
70 | };
|
71 |
|
72 | return _setPrototypeOf(o, p);
|
73 | }
|
74 |
|
75 | function _isNativeReflectConstruct() {
|
76 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
77 | if (Reflect.construct.sham) return false;
|
78 | if (typeof Proxy === "function") return true;
|
79 |
|
80 | try {
|
81 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
82 | return true;
|
83 | } catch (e) {
|
84 | return false;
|
85 | }
|
86 | }
|
87 |
|
88 | function _assertThisInitialized(self) {
|
89 | if (self === void 0) {
|
90 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
91 | }
|
92 |
|
93 | return self;
|
94 | }
|
95 |
|
96 | function _possibleConstructorReturn(self, call) {
|
97 | if (call && (typeof call === "object" || typeof call === "function")) {
|
98 | return call;
|
99 | }
|
100 |
|
101 | return _assertThisInitialized(self);
|
102 | }
|
103 |
|
104 | function _createSuper(Derived) {
|
105 | var hasNativeReflectConstruct = _isNativeReflectConstruct();
|
106 |
|
107 | return function () {
|
108 | var Super = _getPrototypeOf(Derived),
|
109 | result;
|
110 |
|
111 | if (hasNativeReflectConstruct) {
|
112 | var NewTarget = _getPrototypeOf(this).constructor;
|
113 |
|
114 | result = Reflect.construct(Super, arguments, NewTarget);
|
115 | } else {
|
116 | result = Super.apply(this, arguments);
|
117 | }
|
118 |
|
119 | return _possibleConstructorReturn(this, result);
|
120 | };
|
121 | }
|
122 |
|
123 | var Connector = function () {
|
124 | |
125 |
|
126 |
|
127 | function Connector(options) {
|
128 | _classCallCheck(this, Connector);
|
129 |
|
130 | |
131 |
|
132 |
|
133 | this._defaultOptions = {
|
134 | auth: {
|
135 | headers: {}
|
136 | },
|
137 | authEndpoint: '/broadcasting/auth',
|
138 | broadcaster: 'pusher',
|
139 | csrfToken: null,
|
140 | host: null,
|
141 | key: null,
|
142 | namespace: 'App.Events'
|
143 | };
|
144 | this.setOptions(options);
|
145 | this.connect();
|
146 | }
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 | _createClass(Connector, [{
|
153 | key: "setOptions",
|
154 | value: function setOptions(options) {
|
155 | this.options = _extends(this._defaultOptions, options);
|
156 |
|
157 | if (this.csrfToken()) {
|
158 | this.options.auth.headers['X-CSRF-TOKEN'] = this.csrfToken();
|
159 | }
|
160 |
|
161 | return options;
|
162 | }
|
163 | |
164 |
|
165 |
|
166 |
|
167 | }, {
|
168 | key: "csrfToken",
|
169 | value: function csrfToken() {
|
170 | var selector;
|
171 |
|
172 | if (typeof window !== 'undefined' && window['Laravel'] && window['Laravel'].csrfToken) {
|
173 | return window['Laravel'].csrfToken;
|
174 | } else if (this.options.csrfToken) {
|
175 | return this.options.csrfToken;
|
176 | } else if (typeof document !== 'undefined' && typeof document.querySelector === 'function' && (selector = document.querySelector('meta[name="csrf-token"]'))) {
|
177 | return selector.getAttribute('content');
|
178 | }
|
179 |
|
180 | return null;
|
181 | }
|
182 | }]);
|
183 |
|
184 | return Connector;
|
185 | }();
|
186 |
|
187 | |
188 |
|
189 |
|
190 | var Channel = function () {
|
191 | function Channel() {
|
192 | _classCallCheck(this, Channel);
|
193 | }
|
194 |
|
195 | _createClass(Channel, [{
|
196 | key: "listenForWhisper",
|
197 |
|
198 | |
199 |
|
200 |
|
201 | value: function listenForWhisper(event, callback) {
|
202 | return this.listen('.client-' + event, callback);
|
203 | }
|
204 | |
205 |
|
206 |
|
207 |
|
208 | }, {
|
209 | key: "notification",
|
210 | value: function notification(callback) {
|
211 | return this.listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', callback);
|
212 | }
|
213 | |
214 |
|
215 |
|
216 |
|
217 | }, {
|
218 | key: "stopListeningForWhisper",
|
219 | value: function stopListeningForWhisper(event, callback) {
|
220 | return this.stopListening('.client-' + event, callback);
|
221 | }
|
222 | }]);
|
223 |
|
224 | return Channel;
|
225 | }();
|
226 |
|
227 | |
228 |
|
229 |
|
230 | var EventFormatter = function () {
|
231 | |
232 |
|
233 |
|
234 | function EventFormatter(namespace) {
|
235 | _classCallCheck(this, EventFormatter);
|
236 |
|
237 | this.setNamespace(namespace);
|
238 | }
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 | _createClass(EventFormatter, [{
|
245 | key: "format",
|
246 | value: function format(event) {
|
247 | if (event.charAt(0) === '.' || event.charAt(0) === '\\') {
|
248 | return event.substr(1);
|
249 | } else if (this.namespace) {
|
250 | event = this.namespace + '.' + event;
|
251 | }
|
252 |
|
253 | return event.replace(/\./g, '\\');
|
254 | }
|
255 | |
256 |
|
257 |
|
258 |
|
259 | }, {
|
260 | key: "setNamespace",
|
261 | value: function setNamespace(value) {
|
262 | this.namespace = value;
|
263 | }
|
264 | }]);
|
265 |
|
266 | return EventFormatter;
|
267 | }();
|
268 |
|
269 | |
270 |
|
271 |
|
272 |
|
273 | var PusherChannel = function (_Channel) {
|
274 | _inherits(PusherChannel, _Channel);
|
275 |
|
276 | var _super = _createSuper(PusherChannel);
|
277 |
|
278 | |
279 |
|
280 |
|
281 | function PusherChannel(pusher, name, options) {
|
282 | var _this;
|
283 |
|
284 | _classCallCheck(this, PusherChannel);
|
285 |
|
286 | _this = _super.call(this);
|
287 | _this.name = name;
|
288 | _this.pusher = pusher;
|
289 | _this.options = options;
|
290 | _this.eventFormatter = new EventFormatter(_this.options.namespace);
|
291 |
|
292 | _this.subscribe();
|
293 |
|
294 | return _this;
|
295 | }
|
296 | |
297 |
|
298 |
|
299 |
|
300 |
|
301 | _createClass(PusherChannel, [{
|
302 | key: "subscribe",
|
303 | value: function subscribe() {
|
304 | this.subscription = this.pusher.subscribe(this.name);
|
305 | }
|
306 | |
307 |
|
308 |
|
309 |
|
310 | }, {
|
311 | key: "unsubscribe",
|
312 | value: function unsubscribe() {
|
313 | this.pusher.unsubscribe(this.name);
|
314 | }
|
315 | |
316 |
|
317 |
|
318 |
|
319 | }, {
|
320 | key: "listen",
|
321 | value: function listen(event, callback) {
|
322 | this.on(this.eventFormatter.format(event), callback);
|
323 | return this;
|
324 | }
|
325 | |
326 |
|
327 |
|
328 |
|
329 | }, {
|
330 | key: "listenToAll",
|
331 | value: function listenToAll(callback) {
|
332 | var _this2 = this;
|
333 |
|
334 | this.subscription.bind_global(function (event, data) {
|
335 | if (event.startsWith('pusher:')) {
|
336 | return;
|
337 | }
|
338 |
|
339 | var namespace = _this2.options.namespace.replace(/\./g, '\\');
|
340 |
|
341 | var formattedEvent = event.startsWith(namespace) ? event.substring(namespace.length + 1) : '.' + event;
|
342 | callback(formattedEvent, data);
|
343 | });
|
344 | return this;
|
345 | }
|
346 | |
347 |
|
348 |
|
349 |
|
350 | }, {
|
351 | key: "stopListening",
|
352 | value: function stopListening(event, callback) {
|
353 | if (callback) {
|
354 | this.subscription.unbind(this.eventFormatter.format(event), callback);
|
355 | } else {
|
356 | this.subscription.unbind(this.eventFormatter.format(event));
|
357 | }
|
358 |
|
359 | return this;
|
360 | }
|
361 | |
362 |
|
363 |
|
364 |
|
365 | }, {
|
366 | key: "stopListeningToAll",
|
367 | value: function stopListeningToAll(callback) {
|
368 | if (callback) {
|
369 | this.subscription.unbind_global(callback);
|
370 | } else {
|
371 | this.subscription.unbind_global();
|
372 | }
|
373 |
|
374 | return this;
|
375 | }
|
376 | |
377 |
|
378 |
|
379 |
|
380 | }, {
|
381 | key: "subscribed",
|
382 | value: function subscribed(callback) {
|
383 | this.on('pusher:subscription_succeeded', function () {
|
384 | callback();
|
385 | });
|
386 | return this;
|
387 | }
|
388 | |
389 |
|
390 |
|
391 |
|
392 | }, {
|
393 | key: "error",
|
394 | value: function error(callback) {
|
395 | this.on('pusher:subscription_error', function (status) {
|
396 | callback(status);
|
397 | });
|
398 | return this;
|
399 | }
|
400 | |
401 |
|
402 |
|
403 |
|
404 | }, {
|
405 | key: "on",
|
406 | value: function on(event, callback) {
|
407 | this.subscription.bind(event, callback);
|
408 | return this;
|
409 | }
|
410 | }]);
|
411 |
|
412 | return PusherChannel;
|
413 | }(Channel);
|
414 |
|
415 | |
416 |
|
417 |
|
418 |
|
419 | var PusherPrivateChannel = function (_PusherChannel) {
|
420 | _inherits(PusherPrivateChannel, _PusherChannel);
|
421 |
|
422 | var _super = _createSuper(PusherPrivateChannel);
|
423 |
|
424 | function PusherPrivateChannel() {
|
425 | _classCallCheck(this, PusherPrivateChannel);
|
426 |
|
427 | return _super.apply(this, arguments);
|
428 | }
|
429 |
|
430 | _createClass(PusherPrivateChannel, [{
|
431 | key: "whisper",
|
432 |
|
433 | |
434 |
|
435 |
|
436 | value: function whisper(eventName, data) {
|
437 | this.pusher.channels.channels[this.name].trigger("client-".concat(eventName), data);
|
438 | return this;
|
439 | }
|
440 | }]);
|
441 |
|
442 | return PusherPrivateChannel;
|
443 | }(PusherChannel);
|
444 |
|
445 | |
446 |
|
447 |
|
448 |
|
449 | var PusherEncryptedPrivateChannel = function (_PusherChannel) {
|
450 | _inherits(PusherEncryptedPrivateChannel, _PusherChannel);
|
451 |
|
452 | var _super = _createSuper(PusherEncryptedPrivateChannel);
|
453 |
|
454 | function PusherEncryptedPrivateChannel() {
|
455 | _classCallCheck(this, PusherEncryptedPrivateChannel);
|
456 |
|
457 | return _super.apply(this, arguments);
|
458 | }
|
459 |
|
460 | _createClass(PusherEncryptedPrivateChannel, [{
|
461 | key: "whisper",
|
462 |
|
463 | |
464 |
|
465 |
|
466 | value: function whisper(eventName, data) {
|
467 | this.pusher.channels.channels[this.name].trigger("client-".concat(eventName), data);
|
468 | return this;
|
469 | }
|
470 | }]);
|
471 |
|
472 | return PusherEncryptedPrivateChannel;
|
473 | }(PusherChannel);
|
474 |
|
475 | |
476 |
|
477 |
|
478 |
|
479 | var PusherPresenceChannel = function (_PusherChannel) {
|
480 | _inherits(PusherPresenceChannel, _PusherChannel);
|
481 |
|
482 | var _super = _createSuper(PusherPresenceChannel);
|
483 |
|
484 | function PusherPresenceChannel() {
|
485 | _classCallCheck(this, PusherPresenceChannel);
|
486 |
|
487 | return _super.apply(this, arguments);
|
488 | }
|
489 |
|
490 | _createClass(PusherPresenceChannel, [{
|
491 | key: "here",
|
492 |
|
493 | |
494 |
|
495 |
|
496 | value: function here(callback) {
|
497 | this.on('pusher:subscription_succeeded', function (data) {
|
498 | callback(Object.keys(data.members).map(function (k) {
|
499 | return data.members[k];
|
500 | }));
|
501 | });
|
502 | return this;
|
503 | }
|
504 | |
505 |
|
506 |
|
507 |
|
508 | }, {
|
509 | key: "joining",
|
510 | value: function joining(callback) {
|
511 | this.on('pusher:member_added', function (member) {
|
512 | callback(member.info);
|
513 | });
|
514 | return this;
|
515 | }
|
516 | |
517 |
|
518 |
|
519 |
|
520 | }, {
|
521 | key: "leaving",
|
522 | value: function leaving(callback) {
|
523 | this.on('pusher:member_removed', function (member) {
|
524 | callback(member.info);
|
525 | });
|
526 | return this;
|
527 | }
|
528 | |
529 |
|
530 |
|
531 |
|
532 | }, {
|
533 | key: "whisper",
|
534 | value: function whisper(eventName, data) {
|
535 | this.pusher.channels.channels[this.name].trigger("client-".concat(eventName), data);
|
536 | return this;
|
537 | }
|
538 | }]);
|
539 |
|
540 | return PusherPresenceChannel;
|
541 | }(PusherChannel);
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 | var SocketIoChannel = function (_Channel) {
|
548 | _inherits(SocketIoChannel, _Channel);
|
549 |
|
550 | var _super = _createSuper(SocketIoChannel);
|
551 |
|
552 | |
553 |
|
554 |
|
555 | function SocketIoChannel(socket, name, options) {
|
556 | var _this;
|
557 |
|
558 | _classCallCheck(this, SocketIoChannel);
|
559 |
|
560 | _this = _super.call(this);
|
561 | |
562 |
|
563 |
|
564 |
|
565 | _this.events = {};
|
566 | |
567 |
|
568 |
|
569 |
|
570 | _this.listeners = {};
|
571 | _this.name = name;
|
572 | _this.socket = socket;
|
573 | _this.options = options;
|
574 | _this.eventFormatter = new EventFormatter(_this.options.namespace);
|
575 |
|
576 | _this.subscribe();
|
577 |
|
578 | return _this;
|
579 | }
|
580 | |
581 |
|
582 |
|
583 |
|
584 |
|
585 | _createClass(SocketIoChannel, [{
|
586 | key: "subscribe",
|
587 | value: function subscribe() {
|
588 | this.socket.emit('subscribe', {
|
589 | channel: this.name,
|
590 | auth: this.options.auth || {}
|
591 | });
|
592 | }
|
593 | |
594 |
|
595 |
|
596 |
|
597 | }, {
|
598 | key: "unsubscribe",
|
599 | value: function unsubscribe() {
|
600 | this.unbind();
|
601 | this.socket.emit('unsubscribe', {
|
602 | channel: this.name,
|
603 | auth: this.options.auth || {}
|
604 | });
|
605 | }
|
606 | |
607 |
|
608 |
|
609 |
|
610 | }, {
|
611 | key: "listen",
|
612 | value: function listen(event, callback) {
|
613 | this.on(this.eventFormatter.format(event), callback);
|
614 | return this;
|
615 | }
|
616 | |
617 |
|
618 |
|
619 |
|
620 | }, {
|
621 | key: "stopListening",
|
622 | value: function stopListening(event, callback) {
|
623 | this.unbindEvent(this.eventFormatter.format(event), callback);
|
624 | return this;
|
625 | }
|
626 | |
627 |
|
628 |
|
629 |
|
630 | }, {
|
631 | key: "subscribed",
|
632 | value: function subscribed(callback) {
|
633 | this.on('connect', function (socket) {
|
634 | callback(socket);
|
635 | });
|
636 | return this;
|
637 | }
|
638 | |
639 |
|
640 |
|
641 |
|
642 | }, {
|
643 | key: "error",
|
644 | value: function error(callback) {
|
645 | return this;
|
646 | }
|
647 | |
648 |
|
649 |
|
650 |
|
651 | }, {
|
652 | key: "on",
|
653 | value: function on(event, callback) {
|
654 | var _this2 = this;
|
655 |
|
656 | this.listeners[event] = this.listeners[event] || [];
|
657 |
|
658 | if (!this.events[event]) {
|
659 | this.events[event] = function (channel, data) {
|
660 | if (_this2.name === channel && _this2.listeners[event]) {
|
661 | _this2.listeners[event].forEach(function (cb) {
|
662 | return cb(data);
|
663 | });
|
664 | }
|
665 | };
|
666 |
|
667 | this.socket.on(event, this.events[event]);
|
668 | }
|
669 |
|
670 | this.listeners[event].push(callback);
|
671 | return this;
|
672 | }
|
673 | |
674 |
|
675 |
|
676 |
|
677 | }, {
|
678 | key: "unbind",
|
679 | value: function unbind() {
|
680 | var _this3 = this;
|
681 |
|
682 | Object.keys(this.events).forEach(function (event) {
|
683 | _this3.unbindEvent(event);
|
684 | });
|
685 | }
|
686 | |
687 |
|
688 |
|
689 |
|
690 | }, {
|
691 | key: "unbindEvent",
|
692 | value: function unbindEvent(event, callback) {
|
693 | this.listeners[event] = this.listeners[event] || [];
|
694 |
|
695 | if (callback) {
|
696 | this.listeners[event] = this.listeners[event].filter(function (cb) {
|
697 | return cb !== callback;
|
698 | });
|
699 | }
|
700 |
|
701 | if (!callback || this.listeners[event].length === 0) {
|
702 | if (this.events[event]) {
|
703 | this.socket.removeListener(event, this.events[event]);
|
704 | delete this.events[event];
|
705 | }
|
706 |
|
707 | delete this.listeners[event];
|
708 | }
|
709 | }
|
710 | }]);
|
711 |
|
712 | return SocketIoChannel;
|
713 | }(Channel);
|
714 |
|
715 | |
716 |
|
717 |
|
718 |
|
719 | var SocketIoPrivateChannel = function (_SocketIoChannel) {
|
720 | _inherits(SocketIoPrivateChannel, _SocketIoChannel);
|
721 |
|
722 | var _super = _createSuper(SocketIoPrivateChannel);
|
723 |
|
724 | function SocketIoPrivateChannel() {
|
725 | _classCallCheck(this, SocketIoPrivateChannel);
|
726 |
|
727 | return _super.apply(this, arguments);
|
728 | }
|
729 |
|
730 | _createClass(SocketIoPrivateChannel, [{
|
731 | key: "whisper",
|
732 |
|
733 | |
734 |
|
735 |
|
736 | value: function whisper(eventName, data) {
|
737 | this.socket.emit('client event', {
|
738 | channel: this.name,
|
739 | event: "client-".concat(eventName),
|
740 | data: data
|
741 | });
|
742 | return this;
|
743 | }
|
744 | }]);
|
745 |
|
746 | return SocketIoPrivateChannel;
|
747 | }(SocketIoChannel);
|
748 |
|
749 | |
750 |
|
751 |
|
752 |
|
753 | var SocketIoPresenceChannel = function (_SocketIoPrivateChann) {
|
754 | _inherits(SocketIoPresenceChannel, _SocketIoPrivateChann);
|
755 |
|
756 | var _super = _createSuper(SocketIoPresenceChannel);
|
757 |
|
758 | function SocketIoPresenceChannel() {
|
759 | _classCallCheck(this, SocketIoPresenceChannel);
|
760 |
|
761 | return _super.apply(this, arguments);
|
762 | }
|
763 |
|
764 | _createClass(SocketIoPresenceChannel, [{
|
765 | key: "here",
|
766 |
|
767 | |
768 |
|
769 |
|
770 | value: function here(callback) {
|
771 | this.on('presence:subscribed', function (members) {
|
772 | callback(members.map(function (m) {
|
773 | return m.user_info;
|
774 | }));
|
775 | });
|
776 | return this;
|
777 | }
|
778 | |
779 |
|
780 |
|
781 |
|
782 | }, {
|
783 | key: "joining",
|
784 | value: function joining(callback) {
|
785 | this.on('presence:joining', function (member) {
|
786 | return callback(member.user_info);
|
787 | });
|
788 | return this;
|
789 | }
|
790 | |
791 |
|
792 |
|
793 |
|
794 | }, {
|
795 | key: "leaving",
|
796 | value: function leaving(callback) {
|
797 | this.on('presence:leaving', function (member) {
|
798 | return callback(member.user_info);
|
799 | });
|
800 | return this;
|
801 | }
|
802 | }]);
|
803 |
|
804 | return SocketIoPresenceChannel;
|
805 | }(SocketIoPrivateChannel);
|
806 |
|
807 | |
808 |
|
809 |
|
810 |
|
811 | var NullChannel = function (_Channel) {
|
812 | _inherits(NullChannel, _Channel);
|
813 |
|
814 | var _super = _createSuper(NullChannel);
|
815 |
|
816 | function NullChannel() {
|
817 | _classCallCheck(this, NullChannel);
|
818 |
|
819 | return _super.apply(this, arguments);
|
820 | }
|
821 |
|
822 | _createClass(NullChannel, [{
|
823 | key: "subscribe",
|
824 |
|
825 | |
826 |
|
827 |
|
828 | value: function subscribe() {}
|
829 |
|
830 | |
831 |
|
832 |
|
833 |
|
834 | }, {
|
835 | key: "unsubscribe",
|
836 | value: function unsubscribe() {}
|
837 |
|
838 | |
839 |
|
840 |
|
841 |
|
842 | }, {
|
843 | key: "listen",
|
844 | value: function listen(event, callback) {
|
845 | return this;
|
846 | }
|
847 | |
848 |
|
849 |
|
850 |
|
851 | }, {
|
852 | key: "stopListening",
|
853 | value: function stopListening(event, callback) {
|
854 | return this;
|
855 | }
|
856 | |
857 |
|
858 |
|
859 |
|
860 | }, {
|
861 | key: "subscribed",
|
862 | value: function subscribed(callback) {
|
863 | return this;
|
864 | }
|
865 | |
866 |
|
867 |
|
868 |
|
869 | }, {
|
870 | key: "error",
|
871 | value: function error(callback) {
|
872 | return this;
|
873 | }
|
874 | |
875 |
|
876 |
|
877 |
|
878 | }, {
|
879 | key: "on",
|
880 | value: function on(event, callback) {
|
881 | return this;
|
882 | }
|
883 | }]);
|
884 |
|
885 | return NullChannel;
|
886 | }(Channel);
|
887 |
|
888 | |
889 |
|
890 |
|
891 |
|
892 | var NullPrivateChannel = function (_NullChannel) {
|
893 | _inherits(NullPrivateChannel, _NullChannel);
|
894 |
|
895 | var _super = _createSuper(NullPrivateChannel);
|
896 |
|
897 | function NullPrivateChannel() {
|
898 | _classCallCheck(this, NullPrivateChannel);
|
899 |
|
900 | return _super.apply(this, arguments);
|
901 | }
|
902 |
|
903 | _createClass(NullPrivateChannel, [{
|
904 | key: "whisper",
|
905 |
|
906 | |
907 |
|
908 |
|
909 | value: function whisper(eventName, data) {
|
910 | return this;
|
911 | }
|
912 | }]);
|
913 |
|
914 | return NullPrivateChannel;
|
915 | }(NullChannel);
|
916 |
|
917 | |
918 |
|
919 |
|
920 |
|
921 | var NullPresenceChannel = function (_NullChannel) {
|
922 | _inherits(NullPresenceChannel, _NullChannel);
|
923 |
|
924 | var _super = _createSuper(NullPresenceChannel);
|
925 |
|
926 | function NullPresenceChannel() {
|
927 | _classCallCheck(this, NullPresenceChannel);
|
928 |
|
929 | return _super.apply(this, arguments);
|
930 | }
|
931 |
|
932 | _createClass(NullPresenceChannel, [{
|
933 | key: "here",
|
934 |
|
935 | |
936 |
|
937 |
|
938 | value: function here(callback) {
|
939 | return this;
|
940 | }
|
941 | |
942 |
|
943 |
|
944 |
|
945 | }, {
|
946 | key: "joining",
|
947 | value: function joining(callback) {
|
948 | return this;
|
949 | }
|
950 | |
951 |
|
952 |
|
953 |
|
954 | }, {
|
955 | key: "leaving",
|
956 | value: function leaving(callback) {
|
957 | return this;
|
958 | }
|
959 | |
960 |
|
961 |
|
962 |
|
963 | }, {
|
964 | key: "whisper",
|
965 | value: function whisper(eventName, data) {
|
966 | return this;
|
967 | }
|
968 | }]);
|
969 |
|
970 | return NullPresenceChannel;
|
971 | }(NullChannel);
|
972 |
|
973 | |
974 |
|
975 |
|
976 |
|
977 | var PusherConnector = function (_Connector) {
|
978 | _inherits(PusherConnector, _Connector);
|
979 |
|
980 | var _super = _createSuper(PusherConnector);
|
981 |
|
982 | function PusherConnector() {
|
983 | var _this;
|
984 |
|
985 | _classCallCheck(this, PusherConnector);
|
986 |
|
987 | _this = _super.apply(this, arguments);
|
988 | |
989 |
|
990 |
|
991 |
|
992 | _this.channels = {};
|
993 | return _this;
|
994 | }
|
995 | |
996 |
|
997 |
|
998 |
|
999 |
|
1000 | _createClass(PusherConnector, [{
|
1001 | key: "connect",
|
1002 | value: function connect() {
|
1003 | if (typeof this.options.client !== 'undefined') {
|
1004 | this.pusher = this.options.client;
|
1005 | } else {
|
1006 | this.pusher = new Pusher(this.options.key, this.options);
|
1007 | }
|
1008 | }
|
1009 | |
1010 |
|
1011 |
|
1012 |
|
1013 | }, {
|
1014 | key: "listen",
|
1015 | value: function listen(name, event, callback) {
|
1016 | return this.channel(name).listen(event, callback);
|
1017 | }
|
1018 | |
1019 |
|
1020 |
|
1021 |
|
1022 | }, {
|
1023 | key: "channel",
|
1024 | value: function channel(name) {
|
1025 | if (!this.channels[name]) {
|
1026 | this.channels[name] = new PusherChannel(this.pusher, name, this.options);
|
1027 | }
|
1028 |
|
1029 | return this.channels[name];
|
1030 | }
|
1031 | |
1032 |
|
1033 |
|
1034 |
|
1035 | }, {
|
1036 | key: "privateChannel",
|
1037 | value: function privateChannel(name) {
|
1038 | if (!this.channels['private-' + name]) {
|
1039 | this.channels['private-' + name] = new PusherPrivateChannel(this.pusher, 'private-' + name, this.options);
|
1040 | }
|
1041 |
|
1042 | return this.channels['private-' + name];
|
1043 | }
|
1044 | |
1045 |
|
1046 |
|
1047 |
|
1048 | }, {
|
1049 | key: "encryptedPrivateChannel",
|
1050 | value: function encryptedPrivateChannel(name) {
|
1051 | if (!this.channels['private-encrypted-' + name]) {
|
1052 | this.channels['private-encrypted-' + name] = new PusherEncryptedPrivateChannel(this.pusher, 'private-encrypted-' + name, this.options);
|
1053 | }
|
1054 |
|
1055 | return this.channels['private-encrypted-' + name];
|
1056 | }
|
1057 | |
1058 |
|
1059 |
|
1060 |
|
1061 | }, {
|
1062 | key: "presenceChannel",
|
1063 | value: function presenceChannel(name) {
|
1064 | if (!this.channels['presence-' + name]) {
|
1065 | this.channels['presence-' + name] = new PusherPresenceChannel(this.pusher, 'presence-' + name, this.options);
|
1066 | }
|
1067 |
|
1068 | return this.channels['presence-' + name];
|
1069 | }
|
1070 | |
1071 |
|
1072 |
|
1073 |
|
1074 | }, {
|
1075 | key: "leave",
|
1076 | value: function leave(name) {
|
1077 | var _this2 = this;
|
1078 |
|
1079 | var channels = [name, 'private-' + name, 'presence-' + name];
|
1080 | channels.forEach(function (name, index) {
|
1081 | _this2.leaveChannel(name);
|
1082 | });
|
1083 | }
|
1084 | |
1085 |
|
1086 |
|
1087 |
|
1088 | }, {
|
1089 | key: "leaveChannel",
|
1090 | value: function leaveChannel(name) {
|
1091 | if (this.channels[name]) {
|
1092 | this.channels[name].unsubscribe();
|
1093 | delete this.channels[name];
|
1094 | }
|
1095 | }
|
1096 | |
1097 |
|
1098 |
|
1099 |
|
1100 | }, {
|
1101 | key: "socketId",
|
1102 | value: function socketId() {
|
1103 | return this.pusher.connection.socket_id;
|
1104 | }
|
1105 | |
1106 |
|
1107 |
|
1108 |
|
1109 | }, {
|
1110 | key: "disconnect",
|
1111 | value: function disconnect() {
|
1112 | this.pusher.disconnect();
|
1113 | }
|
1114 | }]);
|
1115 |
|
1116 | return PusherConnector;
|
1117 | }(Connector);
|
1118 |
|
1119 | |
1120 |
|
1121 |
|
1122 |
|
1123 | var SocketIoConnector = function (_Connector) {
|
1124 | _inherits(SocketIoConnector, _Connector);
|
1125 |
|
1126 | var _super = _createSuper(SocketIoConnector);
|
1127 |
|
1128 | function SocketIoConnector() {
|
1129 | var _this;
|
1130 |
|
1131 | _classCallCheck(this, SocketIoConnector);
|
1132 |
|
1133 | _this = _super.apply(this, arguments);
|
1134 | |
1135 |
|
1136 |
|
1137 |
|
1138 | _this.channels = {};
|
1139 | return _this;
|
1140 | }
|
1141 | |
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 | _createClass(SocketIoConnector, [{
|
1147 | key: "connect",
|
1148 | value: function connect() {
|
1149 | var _this2 = this;
|
1150 |
|
1151 | var io = this.getSocketIO();
|
1152 | this.socket = io(this.options.host, this.options);
|
1153 | this.socket.on('reconnect', function () {
|
1154 | Object.values(_this2.channels).forEach(function (channel) {
|
1155 | channel.subscribe();
|
1156 | });
|
1157 | });
|
1158 | return this.socket;
|
1159 | }
|
1160 | |
1161 |
|
1162 |
|
1163 |
|
1164 | }, {
|
1165 | key: "getSocketIO",
|
1166 | value: function getSocketIO() {
|
1167 | if (typeof this.options.client !== 'undefined') {
|
1168 | return this.options.client;
|
1169 | }
|
1170 |
|
1171 | if (typeof io !== 'undefined') {
|
1172 | return io;
|
1173 | }
|
1174 |
|
1175 | throw new Error('Socket.io client not found. Should be globally available or passed via options.client');
|
1176 | }
|
1177 | |
1178 |
|
1179 |
|
1180 |
|
1181 | }, {
|
1182 | key: "listen",
|
1183 | value: function listen(name, event, callback) {
|
1184 | return this.channel(name).listen(event, callback);
|
1185 | }
|
1186 | |
1187 |
|
1188 |
|
1189 |
|
1190 | }, {
|
1191 | key: "channel",
|
1192 | value: function channel(name) {
|
1193 | if (!this.channels[name]) {
|
1194 | this.channels[name] = new SocketIoChannel(this.socket, name, this.options);
|
1195 | }
|
1196 |
|
1197 | return this.channels[name];
|
1198 | }
|
1199 | |
1200 |
|
1201 |
|
1202 |
|
1203 | }, {
|
1204 | key: "privateChannel",
|
1205 | value: function privateChannel(name) {
|
1206 | if (!this.channels['private-' + name]) {
|
1207 | this.channels['private-' + name] = new SocketIoPrivateChannel(this.socket, 'private-' + name, this.options);
|
1208 | }
|
1209 |
|
1210 | return this.channels['private-' + name];
|
1211 | }
|
1212 | |
1213 |
|
1214 |
|
1215 |
|
1216 | }, {
|
1217 | key: "presenceChannel",
|
1218 | value: function presenceChannel(name) {
|
1219 | if (!this.channels['presence-' + name]) {
|
1220 | this.channels['presence-' + name] = new SocketIoPresenceChannel(this.socket, 'presence-' + name, this.options);
|
1221 | }
|
1222 |
|
1223 | return this.channels['presence-' + name];
|
1224 | }
|
1225 | |
1226 |
|
1227 |
|
1228 |
|
1229 | }, {
|
1230 | key: "leave",
|
1231 | value: function leave(name) {
|
1232 | var _this3 = this;
|
1233 |
|
1234 | var channels = [name, 'private-' + name, 'presence-' + name];
|
1235 | channels.forEach(function (name) {
|
1236 | _this3.leaveChannel(name);
|
1237 | });
|
1238 | }
|
1239 | |
1240 |
|
1241 |
|
1242 |
|
1243 | }, {
|
1244 | key: "leaveChannel",
|
1245 | value: function leaveChannel(name) {
|
1246 | if (this.channels[name]) {
|
1247 | this.channels[name].unsubscribe();
|
1248 | delete this.channels[name];
|
1249 | }
|
1250 | }
|
1251 | |
1252 |
|
1253 |
|
1254 |
|
1255 | }, {
|
1256 | key: "socketId",
|
1257 | value: function socketId() {
|
1258 | return this.socket.id;
|
1259 | }
|
1260 | |
1261 |
|
1262 |
|
1263 |
|
1264 | }, {
|
1265 | key: "disconnect",
|
1266 | value: function disconnect() {
|
1267 | this.socket.disconnect();
|
1268 | }
|
1269 | }]);
|
1270 |
|
1271 | return SocketIoConnector;
|
1272 | }(Connector);
|
1273 |
|
1274 | |
1275 |
|
1276 |
|
1277 |
|
1278 | var NullConnector = function (_Connector) {
|
1279 | _inherits(NullConnector, _Connector);
|
1280 |
|
1281 | var _super = _createSuper(NullConnector);
|
1282 |
|
1283 | function NullConnector() {
|
1284 | var _this;
|
1285 |
|
1286 | _classCallCheck(this, NullConnector);
|
1287 |
|
1288 | _this = _super.apply(this, arguments);
|
1289 | |
1290 |
|
1291 |
|
1292 |
|
1293 | _this.channels = {};
|
1294 | return _this;
|
1295 | }
|
1296 | |
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 | _createClass(NullConnector, [{
|
1302 | key: "connect",
|
1303 | value: function connect() {}
|
1304 |
|
1305 | |
1306 |
|
1307 |
|
1308 |
|
1309 | }, {
|
1310 | key: "listen",
|
1311 | value: function listen(name, event, callback) {
|
1312 | return new NullChannel();
|
1313 | }
|
1314 | |
1315 |
|
1316 |
|
1317 |
|
1318 | }, {
|
1319 | key: "channel",
|
1320 | value: function channel(name) {
|
1321 | return new NullChannel();
|
1322 | }
|
1323 | |
1324 |
|
1325 |
|
1326 |
|
1327 | }, {
|
1328 | key: "privateChannel",
|
1329 | value: function privateChannel(name) {
|
1330 | return new NullPrivateChannel();
|
1331 | }
|
1332 | |
1333 |
|
1334 |
|
1335 |
|
1336 | }, {
|
1337 | key: "presenceChannel",
|
1338 | value: function presenceChannel(name) {
|
1339 | return new NullPresenceChannel();
|
1340 | }
|
1341 | |
1342 |
|
1343 |
|
1344 |
|
1345 | }, {
|
1346 | key: "leave",
|
1347 | value: function leave(name) {}
|
1348 |
|
1349 | |
1350 |
|
1351 |
|
1352 |
|
1353 | }, {
|
1354 | key: "leaveChannel",
|
1355 | value: function leaveChannel(name) {}
|
1356 |
|
1357 | |
1358 |
|
1359 |
|
1360 |
|
1361 | }, {
|
1362 | key: "socketId",
|
1363 | value: function socketId() {
|
1364 | return 'fake-socket-id';
|
1365 | }
|
1366 | |
1367 |
|
1368 |
|
1369 |
|
1370 | }, {
|
1371 | key: "disconnect",
|
1372 | value: function disconnect() {
|
1373 | }
|
1374 | }]);
|
1375 |
|
1376 | return NullConnector;
|
1377 | }(Connector);
|
1378 |
|
1379 | |
1380 |
|
1381 |
|
1382 |
|
1383 | var Echo = function () {
|
1384 | |
1385 |
|
1386 |
|
1387 | function Echo(options) {
|
1388 | _classCallCheck(this, Echo);
|
1389 |
|
1390 | this.options = options;
|
1391 | this.connect();
|
1392 |
|
1393 | if (!this.options.withoutInterceptors) {
|
1394 | this.registerInterceptors();
|
1395 | }
|
1396 | }
|
1397 | |
1398 |
|
1399 |
|
1400 |
|
1401 |
|
1402 | _createClass(Echo, [{
|
1403 | key: "channel",
|
1404 | value: function channel(_channel) {
|
1405 | return this.connector.channel(_channel);
|
1406 | }
|
1407 | |
1408 |
|
1409 |
|
1410 |
|
1411 | }, {
|
1412 | key: "connect",
|
1413 | value: function connect() {
|
1414 | if (this.options.broadcaster == 'pusher') {
|
1415 | this.connector = new PusherConnector(this.options);
|
1416 | } else if (this.options.broadcaster == 'socket.io') {
|
1417 | this.connector = new SocketIoConnector(this.options);
|
1418 | } else if (this.options.broadcaster == 'null') {
|
1419 | this.connector = new NullConnector(this.options);
|
1420 | } else if (typeof this.options.broadcaster == 'function') {
|
1421 | this.connector = new this.options.broadcaster(this.options);
|
1422 | }
|
1423 | }
|
1424 | |
1425 |
|
1426 |
|
1427 |
|
1428 | }, {
|
1429 | key: "disconnect",
|
1430 | value: function disconnect() {
|
1431 | this.connector.disconnect();
|
1432 | }
|
1433 | |
1434 |
|
1435 |
|
1436 |
|
1437 | }, {
|
1438 | key: "join",
|
1439 | value: function join(channel) {
|
1440 | return this.connector.presenceChannel(channel);
|
1441 | }
|
1442 | |
1443 |
|
1444 |
|
1445 |
|
1446 | }, {
|
1447 | key: "leave",
|
1448 | value: function leave(channel) {
|
1449 | this.connector.leave(channel);
|
1450 | }
|
1451 | |
1452 |
|
1453 |
|
1454 |
|
1455 | }, {
|
1456 | key: "leaveChannel",
|
1457 | value: function leaveChannel(channel) {
|
1458 | this.connector.leaveChannel(channel);
|
1459 | }
|
1460 | |
1461 |
|
1462 |
|
1463 |
|
1464 | }, {
|
1465 | key: "listen",
|
1466 | value: function listen(channel, event, callback) {
|
1467 | return this.connector.listen(channel, event, callback);
|
1468 | }
|
1469 | |
1470 |
|
1471 |
|
1472 |
|
1473 | }, {
|
1474 | key: "private",
|
1475 | value: function _private(channel) {
|
1476 | return this.connector.privateChannel(channel);
|
1477 | }
|
1478 | |
1479 |
|
1480 |
|
1481 |
|
1482 | }, {
|
1483 | key: "encryptedPrivate",
|
1484 | value: function encryptedPrivate(channel) {
|
1485 | return this.connector.encryptedPrivateChannel(channel);
|
1486 | }
|
1487 | |
1488 |
|
1489 |
|
1490 |
|
1491 | }, {
|
1492 | key: "socketId",
|
1493 | value: function socketId() {
|
1494 | return this.connector.socketId();
|
1495 | }
|
1496 | |
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 | }, {
|
1502 | key: "registerInterceptors",
|
1503 | value: function registerInterceptors() {
|
1504 | if (typeof Vue === 'function' && Vue.http) {
|
1505 | this.registerVueRequestInterceptor();
|
1506 | }
|
1507 |
|
1508 | if (typeof axios === 'function') {
|
1509 | this.registerAxiosRequestInterceptor();
|
1510 | }
|
1511 |
|
1512 | if (typeof jQuery === 'function') {
|
1513 | this.registerjQueryAjaxSetup();
|
1514 | }
|
1515 | }
|
1516 | |
1517 |
|
1518 |
|
1519 |
|
1520 | }, {
|
1521 | key: "registerVueRequestInterceptor",
|
1522 | value: function registerVueRequestInterceptor() {
|
1523 | var _this = this;
|
1524 |
|
1525 | Vue.http.interceptors.push(function (request, next) {
|
1526 | if (_this.socketId()) {
|
1527 | request.headers.set('X-Socket-ID', _this.socketId());
|
1528 | }
|
1529 |
|
1530 | next();
|
1531 | });
|
1532 | }
|
1533 | |
1534 |
|
1535 |
|
1536 |
|
1537 | }, {
|
1538 | key: "registerAxiosRequestInterceptor",
|
1539 | value: function registerAxiosRequestInterceptor() {
|
1540 | var _this2 = this;
|
1541 |
|
1542 | axios.interceptors.request.use(function (config) {
|
1543 | if (_this2.socketId()) {
|
1544 | config.headers['X-Socket-Id'] = _this2.socketId();
|
1545 | }
|
1546 |
|
1547 | return config;
|
1548 | });
|
1549 | }
|
1550 | |
1551 |
|
1552 |
|
1553 |
|
1554 | }, {
|
1555 | key: "registerjQueryAjaxSetup",
|
1556 | value: function registerjQueryAjaxSetup() {
|
1557 | var _this3 = this;
|
1558 |
|
1559 | if (typeof jQuery.ajax != 'undefined') {
|
1560 | jQuery.ajaxPrefilter(function (options, originalOptions, xhr) {
|
1561 | if (_this3.socketId()) {
|
1562 | xhr.setRequestHeader('X-Socket-Id', _this3.socketId());
|
1563 | }
|
1564 | });
|
1565 | }
|
1566 | }
|
1567 | }]);
|
1568 |
|
1569 | return Echo;
|
1570 | }();
|
1571 |
|
1572 | return Echo;
|
1573 |
|
1574 | }());
|