forked from wycats/yui3-gallery
/
gallery-eventsource-debug.js
274 lines (216 loc) · 8.87 KB
/
gallery-eventsource-debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
YUI.add('gallery-eventsource', function(Y) {
/*global EventSource*/
var useNative = typeof EventSource != "undefined",
YUIEvenSourceProto;
function YUIEventSource(url){
Y.Event.Target.call(this);
/**
* The URL or the server-sent events.
* @type String
* @property url
*/
this.url = url;
/**
* The current state of the object. Possible values are 0 for connecting,
* 1 for connected, and 2 for disconnected.
* @type int
* @property readyState
*/
this.readyState = 0;
/**
* Object used to communicate with the server. May be an XHR or an
* EventSource.
* @type XMLHttpRequest|EventSource
* @property _transport
* @private
*/
this._transport = null;
//initialize the object
this._init();
}
var YUIEventSourceProto;
//use native if available
if (useNative){
YUIEventSourceProto = {
_init: function(){
var src = new EventSource(this.url),
that = this;
//map events to custom events
src.onopen = function(event){
that.fire("open");
that.readyState = 1;
};
src.onmessage = function(event){
that.fire({type: "message", data: event.data});
};
src.onerror = function(event){
that.fire("error");
that.readyState = 2;
};
this._transport = src;
},
close: function(){
this._transport.close();
that.readyState = 2;
}
};
} else {
YUIEventSourceProto = {
/**
* Initializes the EventSource object. Either creates an EventSource
* instance or an XHR to mimic the functionality.
* @method _init
* @return {void}
* @private
*/
_init: function(){
var src,
that = this;
/**
* Keeps track of where in the response buffer to start
* evaluating new data. Only used when native EventSource
* is not available.
* @type int
* @property _lastIndex
* @private
*/
this._lastIndex = 0;
//use appropriate XHR object as transport
if (typeof XMLHttpRequest != "undefined"){ //most browsers
src = new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined"){ //IE6
src = new ActiveXObject("MSXML2.XMLHttp");
}
src.open("get", this.url, true);
/*
* IE < 8 will not have multiple readyState 3 calls, so
* those will go to readyState 4 and effectively become
* long-polling requests. All others will have a hanging
* GET request that receives continual information over
* the same connection.
*/
src.onreadystatechange = function(){
if (src.readyState == 3){
that._updateReadyState();
//IE6 and IE7 throw an error when trying to access responseText here
try {
that._processIncomingData(src.responseText);
} catch(ex){
//noop
}
} else if (src.readyState == 4){
that._updateReadyState();
that._validateResponse();
}
};
this._transport = src;
//wait until this JS task is done before firing
//so as not to lose any events
setTimeout(function(){
src.send(null);
}, 0);
},
/**
* Called when XHR readyState 4 occurs. Processes the response,
* then reopens the connection to the server unless close()
* has been called.
* @method _validateResponse
* @return {void}
* @private
*/
_validateResponse: function(){
var src = this._transport;
if (src.status >= 200 && src.status < 300){
this._processIncomingData(src.responseText);
//readyState will be 2 if close() was called
if (this.readyState != 2){
this._transport.onreadystatechange = function(){};
this._init();
}
} else {
this.fire("error");
this.readyState = 2;
}
//prevent memory leaks due to closure
src = null;
},
/**
* Updates the readyState property to 1 if it's still
* set at 0.
* @method _updateReadyState
* @return {void}
* @private
*/
_updateReadyState: function(){
if (this.readyState == 0){
this.readyState == 1;
}
},
/**
* Processes the data stream as server-sent events. Goes line by
* line looking for event information and fires the message
* event where appropriate.
* @param {String} text The text to parse.
* @return {void}
* @private
* @method _processIncomingData
*/
_processIncomingData: function(text){
text = text.substring(this._lastIndex);
this._lastIndex += text.length;
var lines = text.split("\n"),
parts,
i = 0,
len = lines.length,
data = "",
eventName = "";
while (i < len){
if (lines[i].indexOf(":") > -1){
parts = lines[i].split(":");
//shift off the first item to check the value
//keep in mind that "data: a:b" is a valid value
switch(parts.shift()){
case "data":
data += parts.join(":") + "\n";
break;
case "event":
eventName = parts[1];
break;
case "id":
//todo
break;
}
} else if (lines[i].replace(/\s/g, "") == ""){
//an empty line means to flush the event buffer of data
//but only if there's data to send
if (data != ""){
//per spec, strip off last newline
if (data.charAt(data.length-1) == "\n"){
data = data.substring(0,data.length-1);
}
//an empty line means a message is complete
this.fire({type: "message", data: data});
//clear the existing data
data = "";
eventName = "";
}
}
i++;
}
},
/**
* Permanently close the connection with the server.
* @method close
* @return {void}
*/
close: function(){
this.readyState = 2;
this._transport.abort();
}
};
}
//inherit from Event.Target to get events, and assign instance methods
Y.extend(YUIEventSource, Y.Event.Target, YUIEventSourceProto);
//publish to Y object
Y.EventSource = YUIEventSource;
}, '@VERSION@' ,{requires:['event-base','event-custom']});