Permalink
Browse files

Handle streaming better

  • Loading branch information...
1 parent eba6dd2 commit 722bfc47b6e912116838932080cfa43c83b496b8 @kriszyp committed Dec 8, 2010
Showing with 19 additions and 14 deletions.
  1. +19 −14 lib/jsgi/comet.js
View
33 lib/jsgi/comet.js
@@ -27,6 +27,7 @@ exports.Broadcaster = function(nextApp){
return function(request){
nextApp && nextApp(request); // ignore the response
var headers = request.headers;
+ var streaming = !!headers.stream;
var clientConnection = exports.getClientConnection(request);
if(!clientConnection){
throw new Error("No client connection");
@@ -39,23 +40,27 @@ exports.Broadcaster = function(nextApp){
forEach: function(callback){
if(clientConnection.length){
clientConnection.splice(0, clientConnection.length).forEach(callback);
- }else{
- var promiseCallback;
- var observer = clientConnection.observe("message", function(message){
+ if(!streaming){
+ return;
+ }
+ }
+ 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();
- // TODO: maybe queue this up for the next event turn
- clientConnection.splice(0, clientConnection.length).forEach(callback);
- if(promiseCallback){
- promiseCallback();
- }
- });
- return {
- then: function(callback, errback){
+ promiseCallback();
+ }
+ });
+ return {
+ then: function(callback, errback){
+ if(!streaming){
promiseCallback = callback;
- },
- cancel: function(){
- clientConnection.close();
}
+ },
+ cancel: function(){
+ clientConnection.close();
}
}
}

0 comments on commit 722bfc4

Please sign in to comment.