66
77var reTrim = / ^ ( \s | \u00A0 ) + | ( \s | \u00A0 ) + $ / g;
88
9- var EventSource = function ( url , options ) {
9+ var EventSource = function ( url , options ) {
1010 var eventsource = this ,
11- interval = 500 , // polling interval
12- lastEventId = null ,
13- cache = '' ,
14- eventType ;
11+ interval = 500 , // polling interval
12+ lastEventId = null ,
13+ cache = '' ,
14+ eventType ;
1515
1616 if ( ! url || typeof url != 'string' ) {
1717 throw new SyntaxError ( 'Not enough arguments' ) ;
@@ -24,13 +24,14 @@ var EventSource = function (url, options) {
2424 this . _xhr = null ;
2525
2626 function pollAgain ( interval ) {
27- eventsource . _pollTimer = setTimeout ( function ( ) {
27+ eventsource . _pollTimer = setTimeout ( function ( ) {
2828 poll . call ( eventsource ) ;
2929 } , interval ) ;
3030 }
3131
3232 function poll ( ) {
33- try { // force hiding of the error message... insane?
33+ try {
34+ // force hiding of the error message... insane?
3435 if ( eventsource . readyState == eventsource . CLOSED ) return ;
3536
3637 // NOTE: IE7 and upwards support
@@ -47,15 +48,20 @@ var EventSource = function (url, options) {
4748 // readychange until the server connection is closed
4849 xhr . setRequestHeader ( 'X-Requested-With' , 'XMLHttpRequest' ) ;
4950
50- if ( lastEventId != null ) xhr . setRequestHeader ( 'Last-Event-ID' , lastEventId ) ;
51+ if ( lastEventId != null )
52+ xhr . setRequestHeader ( 'Last-Event-ID' , lastEventId ) ;
5153 cache = '' ;
5254
53- xhr . timeout = ( this . OPTIONS && this . OPTIONS . timeout !== undefined )
55+ xhr . timeout =
56+ this . OPTIONS && this . OPTIONS . timeout !== undefined
5457 ? this . OPTIONS . timeout
55- : 50000
58+ : 50000 ;
5659
57- xhr . onreadystatechange = function ( ) {
58- if ( this . readyState == 3 || ( this . readyState == 4 && this . status == 200 ) ) {
60+ xhr . onreadystatechange = function ( ) {
61+ if (
62+ this . readyState == 3 ||
63+ ( this . readyState == 4 && this . status == 200 )
64+ ) {
5965 // on success
6066 if ( eventsource . readyState == eventsource . CONNECTING ) {
6167 eventsource . readyState = eventsource . OPEN ;
@@ -68,11 +74,11 @@ var EventSource = function (url, options) {
6874 } catch ( e ) { }
6975
7076 // process this.responseText
71- var parts = responseText . substr ( cache . length ) . split ( "\n" ) ,
72- data = [ ] ,
73- i = 0 ,
74- retry = 0 ,
75- line = '' ;
77+ var parts = responseText . substr ( cache . length ) . split ( '\n' ) ,
78+ data = [ ] ,
79+ i = 0 ,
80+ retry = 0 ,
81+ line = '' ;
7682
7783 cache = responseText ;
7884
@@ -83,16 +89,23 @@ var EventSource = function (url, options) {
8389 eventType = line . replace ( / e v e n t : ? \s * / , '' ) ;
8490 } else if ( line . indexOf ( 'retry' ) == 0 ) {
8591 retry = parseInt ( line . replace ( / r e t r y : ? \s * / , '' ) ) ;
86- if ( ! isNaN ( retry ) ) { interval = retry ; }
92+ if ( ! isNaN ( retry ) ) {
93+ interval = retry ;
94+ }
8795 } else if ( line . indexOf ( 'data' ) == 0 ) {
8896 data . push ( line . replace ( / d a t a : ? \s * / , '' ) ) ;
8997 } else if ( line . indexOf ( 'id:' ) == 0 ) {
9098 lastEventId = line . replace ( / i d : ? \s * / , '' ) ;
91- } else if ( line . indexOf ( 'id' ) == 0 ) { // this resets the id
99+ } else if ( line . indexOf ( 'id' ) == 0 ) {
100+ // this resets the id
92101 lastEventId = null ;
93102 } else if ( line == '' ) {
94103 if ( data . length ) {
95- var event = new MessageEvent ( data . join ( '\n' ) , eventsource . url , lastEventId ) ;
104+ var event = new MessageEvent (
105+ data . join ( '\n' ) ,
106+ eventsource . url ,
107+ lastEventId ,
108+ ) ;
96109 eventsource . dispatchEvent ( eventType || 'message' , event ) ;
97110 data = [ ] ;
98111 eventType = undefined ;
@@ -102,11 +115,13 @@ var EventSource = function (url, options) {
102115
103116 if ( this . readyState == 4 ) pollAgain ( interval ) ;
104117
105- // don't need to poll again, because we're long-loading
118+ // don't need to poll again, because we're long-loading
106119 } else if ( eventsource . readyState !== eventsource . CLOSED ) {
107- if ( this . readyState == 4 ) { // and some other status
120+ if ( this . readyState == 4 ) {
121+ // and some other status
108122 pollAgain ( interval ) ;
109- } else if ( this . readyState == 0 ) { // likely aborted
123+ } else if ( this . readyState == 0 ) {
124+ // likely aborted
110125 pollAgain ( interval ) ;
111126 }
112127 }
@@ -116,9 +131,11 @@ var EventSource = function (url, options) {
116131 // dispatch error
117132 eventsource . readyState = eventsource . CONNECTING ;
118133
119- eventsource . dispatchEvent ( 'error' ,
120- { type : 'error' , message : this . responseText } ) ;
121- }
134+ eventsource . dispatchEvent ( 'error' , {
135+ type : 'error' ,
136+ message : this . responseText ,
137+ } ) ;
138+ } ;
122139
123140 if ( eventsource . OPTIONS . body ) {
124141 xhr . send ( eventsource . OPTIONS . body ) ;
@@ -127,23 +144,23 @@ var EventSource = function (url, options) {
127144 }
128145
129146 if ( xhr . timeout > 0 ) {
130- setTimeout ( function ( ) {
147+ setTimeout ( function ( ) {
131148 if ( true || xhr . readyState == 3 ) xhr . abort ( ) ;
132149 } , xhr . timeout ) ;
133150 }
134151
135152 eventsource . _xhr = xhr ;
136-
137- } catch ( e ) { // in an attempt to silence the errors
153+ } catch ( e ) {
154+ // in an attempt to silence the errors
138155 eventsource . dispatchEvent ( 'error' , { type : 'error' , data : e . message } ) ; // ???
139156 }
140- } ;
157+ }
141158
142159 poll ( ) ; // init now
143160} ;
144161
145162EventSource . prototype = {
146- close : function ( ) {
163+ close : function ( ) {
147164 // closes the connection - disabling the polling
148165 this . readyState = this . CLOSED ;
149166 clearInterval ( this . _pollTimer ) ;
@@ -152,7 +169,7 @@ EventSource.prototype = {
152169 CONNECTING : 0 ,
153170 OPEN : 1 ,
154171 CLOSED : 2 ,
155- dispatchEvent : function ( type , event ) {
172+ dispatchEvent : function ( type , event ) {
156173 var handlers = this [ '_' + type + 'Handlers' ] ;
157174 if ( handlers ) {
158175 for ( var i = 0 ; i < handlers . length ; i ++ ) {
@@ -164,14 +181,14 @@ EventSource.prototype = {
164181 this [ 'on' + type ] . call ( this , event ) ;
165182 }
166183 } ,
167- addEventListener : function ( type , handler ) {
184+ addEventListener : function ( type , handler ) {
168185 if ( ! this [ '_' + type + 'Handlers' ] ) {
169186 this [ '_' + type + 'Handlers' ] = [ ] ;
170187 }
171188
172189 this [ '_' + type + 'Handlers' ] . push ( handler ) ;
173190 } ,
174- removeEventListener : function ( type , handler ) {
191+ removeEventListener : function ( type , handler ) {
175192 var handlers = this [ '_' + type + 'Handlers' ] ;
176193 if ( ! handlers ) {
177194 return ;
@@ -187,10 +204,10 @@ EventSource.prototype = {
187204 onmessage : null ,
188205 onopen : null ,
189206 readyState : 0 ,
190- URL : ''
207+ URL : '' ,
191208} ;
192209
193- var MessageEvent = function ( data , origin , lastEventId ) {
210+ var MessageEvent = function ( data , origin , lastEventId ) {
194211 this . data = data ;
195212 this . origin = origin ;
196213 this . lastEventId = lastEventId || '' ;
@@ -200,7 +217,7 @@ MessageEvent.prototype = {
200217 data : null ,
201218 type : 'message' ,
202219 lastEventId : '' ,
203- origin : ''
220+ origin : '' ,
204221} ;
205222
206223export default EventSource ;
0 commit comments