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