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