diff --git a/jquery.eventsource.js b/jquery.eventsource.js index fe188f5..848d51f 100644 --- a/jquery.eventsource.js +++ b/jquery.eventsource.js @@ -1,6 +1,6 @@ /*! * jQuery.EventSource (jQuery.eventsource) - * + * * Copyright (c) 2011 Rick Waldron * Dual licensed under the MIT and GPL licenses. */ @@ -12,7 +12,7 @@ }); var stream = { - + defaults: { // Stream identity label: null, @@ -23,7 +23,7 @@ message: jQuery.noop }, setup: { - stream: {}, + stream: {}, lastEventId: 0, isHostApi: false, retry: 500, @@ -41,6 +41,12 @@ var tmp = {}; if ( !label || label === "*" ) { + for ( var prop in stream.cache ) { + if ( stream.cache[ prop ].isHostApi ) { + stream.cache[ prop ].stream.close(); + } + } + stream.cache = {}; return stream.cache; @@ -49,13 +55,17 @@ for ( var prop in stream.cache ) { if ( label !== prop ) { tmp[ prop ] = stream.cache[ prop ]; + } else { + if ( stream.cache[ prop ].isHostApi ) { + stream.cache[ prop ].stream.close(); + } } } stream.cache = tmp; return stream.cache; - }, + }, streams: function( label ) { if ( !label || label === "*" ) { @@ -67,7 +77,7 @@ }, _private: { - // Open a host api event source + // Open a host api event source openEventSource: function( options ) { var label = options.label; @@ -81,7 +91,7 @@ }, false); stream.cache[label].stream.addEventListener("message", function(event) { - + var streamData = []; if ( stream.cache[ label ] ) { @@ -97,12 +107,12 @@ lastEventId: stream.cache[ label ].lastEventId }, event); - // TODO: Add custom event triggering + // TODO: Add custom event triggering } }, false); return stream.cache[ label ].stream; - }, + }, // open fallback event source openPollingSource: function( options ) { var label = options.label, @@ -129,7 +139,7 @@ return !!sdata && sdata; }), idx = 0, length = streamData.length, - rretryprefix = /retry/, + rretryprefix = /retry/, retries; if ( jQuery.isArray( streamData ) ) { @@ -138,7 +148,7 @@ if ( streamData[ idx ] ) { - if ( rretryprefix.test( streamData[ idx ] ) && + if ( rretryprefix.test( streamData[ idx ] ) && (retries = streamData[ idx ].split("retry: ")).length ) { if ( retries.length === 2 && !retries[ 0 ] ) { @@ -197,7 +207,7 @@ // Plugin sub function if ( options && !jQuery.isPlainObject( options ) && pluginFns.public[ options ] ) { // If no label was passed, send message to all streams - return pluginFns.public[ options ]( + return pluginFns.public[ options ]( arguments[1] ? arguments[1] : "*" @@ -205,7 +215,7 @@ } // If params were passed in as an object, normalize to a query string - options.data = options.data && jQuery.isPlainObject( options.data ) ? + options.data = options.data && jQuery.isPlainObject( options.data ) ? jQuery.param( options.data ) : options.data; @@ -230,7 +240,7 @@ }; - // Determine and declare `event stream` source, + // Determine and declare `event stream` source, // whether will be host api or XHR fallback streamType = !isHostApi ? // If not host api, open a polling fallback diff --git a/test/index.html b/test/index.html index 41b7f08..b5ff890 100644 --- a/test/index.html +++ b/test/index.html @@ -3,12 +3,12 @@