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 @@ jquery.eventsource.unit - - + + - - + + @@ -23,7 +23,7 @@

- + - + diff --git a/test/jquery.eventsource.unit.js b/test/jquery.eventsource.unit.js index 5df148a..e39eb38 100644 --- a/test/jquery.eventsource.unit.js +++ b/test/jquery.eventsource.unit.js @@ -10,7 +10,7 @@ function sizeOf(obj) { $(function() { - var params = location.search.slice( 1 ).split( "&" ), + var params = location.search.slice( 1 ).split( "&" ), pairs = {}; $.each( params, function(idx, param) { @@ -29,10 +29,10 @@ $(function() { test("is a function", function() { expect(7); - + ok( jQuery.eventsource, "exists" ); equal( typeof jQuery.eventsource, "function", "jQuery.eventsource() is a Function" ); - + ok( jQuery.eventsource.streams, "jQuery.eventsource.streams exists" ); equal( typeof jQuery.eventsource.streams, "function", "jQuery.eventsource.streams() is a Function" ); @@ -42,21 +42,21 @@ test("is a function", function() { equal( sizeOf( jQuery.eventsource.streams() ), 0, "There are no streams"); }); - -test("callbacks", function() { - - var expects = 12, + +test("callbacks", function() { + + var expects = 12, count = 0; expect( expects ); - - function plus() { + + function plus() { if ( ++count === expects ) { - start(); + start(); } } - function okPlus() { + function okPlus() { ok.apply(null, arguments); plus(); } @@ -71,10 +71,10 @@ test("callbacks", function() { }, message: function(data) { okPlus( true, "#1 fires onmessage callback" ); - + okPlus( data, "#1 returns data"); - - okPlus( typeof jQuery.eventsource("close", "text-event-source") === "object", 'jQuery.eventsource("close", "text-event-source") must return an object' ); + + okPlus( typeof jQuery.eventsource("close", "text-event-source") === "object", 'jQuery.eventsource("close", "text-event-source") must return an object' ); } }); @@ -90,11 +90,11 @@ test("callbacks", function() { okPlus( true, "#2 fires onmessage callback" ); okPlus( data, "#2 returns data"); - okPlus( typeof jQuery.eventsource("close", "text-event-source-ct") === "object", 'jQuery.eventsource("close", "text-event-source-ct") must return an object' ); + okPlus( typeof jQuery.eventsource("close", "text-event-source-ct") === "object", 'jQuery.eventsource("close", "text-event-source-ct") must return an object' ); } }); - + // PLAIN TEXT EXAMPLE HAS CONTENT TYPE jQuery.eventsource({ @@ -108,7 +108,7 @@ test("callbacks", function() { okPlus( true, "#3 fires onmessage callback" ); okPlus( data, "#3 returns data"); - okPlus( typeof jQuery.eventsource("close", "json-event-source") === "object", 'jQuery.eventsource("close", "json-event-source") must return an object' ); + okPlus( typeof jQuery.eventsource("close", "json-event-source") === "object", 'jQuery.eventsource("close", "json-event-source") must return an object' ); } }); @@ -156,15 +156,15 @@ test("open/close", function() { test("multiple concurrent sources scope tests", function() { - var expects = 12, - count = 0, + var expects = 12, + count = 0, down = 3; expect( expects ); - function plus() { + function plus() { if ( ++count === expects ) { - start(); + start(); } } @@ -200,18 +200,18 @@ test("multiple concurrent sources scope tests", function() { test("breakage tests", function() { - var expects = 7, + var expects = 7, count = 0; expect( expects ); - - function plus() { + + function plus() { if ( ++count === expects ) { start(); } } - function okPlus() { + function okPlus() { ok.apply(null, arguments); plus(); } @@ -292,7 +292,7 @@ test("breakage tests", function() { } }); -test("streams object", function() { +test("streams object", function() { var expects = 13, count = 0; @@ -408,24 +408,24 @@ test("settable retry time in ms", function() { test("streams Are Closed", function() { - var expects = 2, + var expects = 2, count = 0; expect( expects ); - - function plus() { + + function plus() { if ( ++count === expects ) { - start(); + start(); } } stop(); jQuery.eventsource.close(); - + equal(sizeOf(jQuery.eventsource.streams()), 0, "there are 0 active streams"); plus(); - ok( typeof jQuery.eventsource.streams() === "object", 'jQuery.eventsource.streams() must return an object' ); + ok( typeof jQuery.eventsource.streams() === "object", 'jQuery.eventsource.streams() must return an object' ); plus(); }); diff --git a/test/specit.html b/test/specit.html index 9214060..fef7eec 100644 --- a/test/specit.html +++ b/test/specit.html @@ -3,27 +3,27 @@ jquery.eventsource.specit - - + + - + - + diff --git a/test/test.php b/test/test.php new file mode 100644 index 0000000..3512aac --- /dev/null +++ b/test/test.php @@ -0,0 +1,45 @@ + + + + + + jQuery.eventsource.js + + + + + + +