Skip to content

Commit

Permalink
Fixed message interpretation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas committed Nov 5, 2010
1 parent 6e0a897 commit 6779d2a
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions src/gallery-eventsource/js/eventsource.js
Expand Up @@ -136,6 +136,22 @@
*/
this._lastEventId = null;

/**
* Tracks the last piece of data from the messages stream.
* @type String
* @property _data
* @private
*/
this._data = "";

/**
* Tracks the last event name in the message stream.
* @type String
* @property _eventName
* @private
*/
this._eventName = "";

//use appropriate XHR object as transport
if (typeof XMLHttpRequest != "undefined"){ //most browsers
src = new XMLHttpRequest();
Expand Down Expand Up @@ -175,6 +191,7 @@
//noop
}
} else if (src.readyState == 4 && that.readyState < 2){
that._fireMessageEvent(); //just in case
that._signalOpen();
that._validateResponse();
}
Expand Down Expand Up @@ -254,8 +271,7 @@
parts,
i = 0,
len = lines.length,
data = "",
eventName = "";
tempData;

while (i < len){

Expand All @@ -267,11 +283,15 @@
//keep in mind that "data: a:b" is a valid value
switch(parts.shift()){
case "data":
data += parts.join(":") + "\n";
tempData = parts.join(":") + "\n";
if (tempData.charAt(0) == " "){
tempData = tempData.substring(1);
}
this._data += tempData;
break;

case "event":
eventName = parts[1];
this._eventName = parts[1];
break;

case "id":
Expand All @@ -283,25 +303,7 @@
//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);
}

//per spec, removing leading space character, strip off leading white space
if (data.charAt(0) == " "){
data = data.substring(1);
}

//an empty line means a message is complete
this.fire({type: "message", data: data});

//clear the existing data
data = "";
eventName = "";
}
this._fireMessageEvent();

}

Expand All @@ -310,6 +312,31 @@

},

/**
* Fires the message event with appropriate data, but only if
* there is actual data to share. This uses the stored
* event name and data value to fire the appropriate event.
* @return {void}
* @method _fireMessageEvent
* @private
*/
_fireMessageEvent: function(){
if (this._data != ""){

//per spec, strip off last newline
if (this._data.charAt(this._data.length-1) == "\n"){
this._data = this._data.substring(0,this._data.length-1);
}

//an empty line means a message is complete
this.fire({type: "message", data: this._data});

//clear the existing data
this._data = "";
this._eventName = "";
}
},

/**
* Permanently close the connection with the server.
* @method close
Expand Down

0 comments on commit 6779d2a

Please sign in to comment.