Skip to content

Commit

Permalink
Handle streaming better
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Dec 8, 2010
1 parent eba6dd2 commit 722bfc4
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/jsgi/comet.js
Expand Up @@ -27,6 +27,7 @@ exports.Broadcaster = function(nextApp){
return function(request){ return function(request){
nextApp && nextApp(request); // ignore the response nextApp && nextApp(request); // ignore the response
var headers = request.headers; var headers = request.headers;
var streaming = !!headers.stream;
var clientConnection = exports.getClientConnection(request); var clientConnection = exports.getClientConnection(request);
if(!clientConnection){ if(!clientConnection){
throw new Error("No client connection"); throw new Error("No client connection");
Expand All @@ -39,23 +40,27 @@ exports.Broadcaster = function(nextApp){
forEach: function(callback){ forEach: function(callback){
if(clientConnection.length){ if(clientConnection.length){
clientConnection.splice(0, clientConnection.length).forEach(callback); clientConnection.splice(0, clientConnection.length).forEach(callback);
}else{ if(!streaming){
var promiseCallback; return;
var observer = clientConnection.observe("message", function(message){ }
}
var promiseCallback;
var observer = clientConnection.observe("message", function(message){
// TODO: maybe queue this up for the next event turn
clientConnection.splice(0, clientConnection.length).forEach(callback);
if(promiseCallback){
observer.dismiss(); observer.dismiss();
// TODO: maybe queue this up for the next event turn promiseCallback();
clientConnection.splice(0, clientConnection.length).forEach(callback); }
if(promiseCallback){ });
promiseCallback(); return {
} then: function(callback, errback){
}); if(!streaming){
return {
then: function(callback, errback){
promiseCallback = callback; promiseCallback = callback;
},
cancel: function(){
clientConnection.close();
} }
},
cancel: function(){
clientConnection.close();
} }
} }
} }
Expand Down

0 comments on commit 722bfc4

Please sign in to comment.