1
+ /*
2
+ * This file is part of the Symfony package.
3
+ *
4
+ * (c) Fabien Potencier <[email protected] >
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ 'use strict' ;
10
+
11
+ function _typeof ( obj ) { "@babel/helpers - typeof" ; if ( typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ) { _typeof = function _typeof ( obj ) { return typeof obj ; } ; } else { _typeof = function _typeof ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ; } return _typeof ( obj ) ; }
12
+
13
+ Object . defineProperty ( exports , "__esModule" , {
14
+ value : true
15
+ } ) ;
16
+ exports [ "default" ] = void 0 ;
17
+
18
+ var _stimulus = require ( "stimulus" ) ;
19
+
20
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
21
+
22
+ function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
23
+
24
+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
25
+
26
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function" ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , writable : true , configurable : true } } ) ; if ( superClass ) _setPrototypeOf ( subClass , superClass ) ; }
27
+
28
+ function _setPrototypeOf ( o , p ) { _setPrototypeOf = Object . setPrototypeOf || function _setPrototypeOf ( o , p ) { o . __proto__ = p ; return o ; } ; return _setPrototypeOf ( o , p ) ; }
29
+
30
+ function _createSuper ( Derived ) { var hasNativeReflectConstruct = _isNativeReflectConstruct ( ) ; return function _createSuperInternal ( ) { var Super = _getPrototypeOf ( Derived ) , result ; if ( hasNativeReflectConstruct ) { var NewTarget = _getPrototypeOf ( this ) . constructor ; result = Reflect . construct ( Super , arguments , NewTarget ) ; } else { result = Super . apply ( this , arguments ) ; } return _possibleConstructorReturn ( this , result ) ; } ; }
31
+
32
+ function _possibleConstructorReturn ( self , call ) { if ( call && ( _typeof ( call ) === "object" || typeof call === "function" ) ) { return call ; } return _assertThisInitialized ( self ) ; }
33
+
34
+ function _assertThisInitialized ( self ) { if ( self === void 0 ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return self ; }
35
+
36
+ function _isNativeReflectConstruct ( ) { if ( typeof Reflect === "undefined" || ! Reflect . construct ) return false ; if ( Reflect . construct . sham ) return false ; if ( typeof Proxy === "function" ) return true ; try { Date . prototype . toString . call ( Reflect . construct ( Date , [ ] , function ( ) { } ) ) ; return true ; } catch ( e ) { return false ; } }
37
+
38
+ function _getPrototypeOf ( o ) { _getPrototypeOf = Object . setPrototypeOf ? Object . getPrototypeOf : function _getPrototypeOf ( o ) { return o . __proto__ || Object . getPrototypeOf ( o ) ; } ; return _getPrototypeOf ( o ) ; }
39
+
40
+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
41
+
42
+ /**
43
+ * @author Mathias Arlaud <[email protected] >
44
+ */
45
+ var _default = /*#__PURE__*/ function ( _Controller ) {
46
+ _inherits ( _default , _Controller ) ;
47
+
48
+ var _super = _createSuper ( _default ) ;
49
+
50
+ function _default ( ) {
51
+ var _this ;
52
+
53
+ _classCallCheck ( this , _default ) ;
54
+
55
+ for ( var _len = arguments . length , args = new Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
56
+ args [ _key ] = arguments [ _key ] ;
57
+ }
58
+
59
+ _this = _super . call . apply ( _super , [ this ] . concat ( args ) ) ;
60
+
61
+ _defineProperty ( _assertThisInitialized ( _this ) , "eventSources" , [ ] ) ;
62
+
63
+ return _this ;
64
+ }
65
+
66
+ _createClass ( _default , [ {
67
+ key : "connect" ,
68
+ value : function connect ( ) {
69
+ var _this2 = this ;
70
+
71
+ if ( ! ( 'Notification' in window ) || ! this . hubValue || ! this . topicsValue ) {
72
+ return ;
73
+ }
74
+
75
+ this . eventSources = this . topicsValue . map ( function ( topic ) {
76
+ return new EventSource ( "" . concat ( _this2 . hubValue , "?topic=" ) . concat ( encodeURIComponent ( topic ) ) ) ;
77
+ } ) ;
78
+ this . eventSources . forEach ( function ( eventSource ) {
79
+ return eventSource . addEventListener ( 'message' , function ( event ) {
80
+ return _this2 . _notify ( JSON . parse ( event . data ) . summary ) ;
81
+ } ) ;
82
+ } ) ;
83
+
84
+ this . _dispatchEvent ( 'notify:connect' , {
85
+ eventSources : this . eventSources
86
+ } ) ;
87
+ }
88
+ } , {
89
+ key : "disconnect" ,
90
+ value : function disconnect ( ) {
91
+ var _this3 = this ;
92
+
93
+ this . eventSources . forEach ( function ( eventSource ) {
94
+ eventSource . removeEventListener ( 'message' , _this3 . _notify ) ;
95
+ eventSource . close ( ) ;
96
+ } ) ;
97
+ this . eventSources = [ ] ;
98
+ }
99
+ } , {
100
+ key : "_notify" ,
101
+ value : function _notify ( content ) {
102
+ if ( ! content ) {
103
+ return ;
104
+ }
105
+
106
+ if ( 'granted' === Notification . permission ) {
107
+ new Notification ( content ) ;
108
+ return ;
109
+ }
110
+
111
+ if ( 'denied' !== Notification . permission ) {
112
+ Notification . requestPermission ( ) . then ( function ( permission ) {
113
+ if ( 'granted' === permission ) {
114
+ new Notification ( content ) ;
115
+ }
116
+ } ) ;
117
+ }
118
+ }
119
+ } , {
120
+ key : "_dispatchEvent" ,
121
+ value : function _dispatchEvent ( name ) {
122
+ var payload = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : null ;
123
+ var canBubble = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : false ;
124
+ var cancelable = arguments . length > 3 && arguments [ 3 ] !== undefined ? arguments [ 3 ] : false ;
125
+ var userEvent = document . createEvent ( 'CustomEvent' ) ;
126
+ userEvent . initCustomEvent ( name , canBubble , cancelable , payload ) ;
127
+ this . element . dispatchEvent ( userEvent ) ;
128
+ }
129
+ } ] ) ;
130
+
131
+ return _default ;
132
+ } ( _stimulus . Controller ) ;
133
+
134
+ exports [ "default" ] = _default ;
135
+
136
+ _defineProperty ( _default , "values" , {
137
+ hub : String ,
138
+ topics : Array
139
+ } ) ;
0 commit comments