diff --git a/jquery.eventsource.js b/jquery.eventsource.js index 0a92dd0..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,9 +23,10 @@ message: jQuery.noop }, setup: { - stream: {}, + stream: {}, lastEventId: 0, isHostApi: false, + retry: 500, history: {}, options: {} }, @@ -40,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; @@ -48,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 === "*" ) { @@ -66,57 +77,57 @@ }, _private: { - // Open a host api event source + // Open a host api event source openEventSource: function( options ) { var label = options.label; stream.cache[ label ].stream.addEventListener("open", function(event) { - if ( stream.cache[label] ) { + if ( stream.cache[ label ] ) { this.label = label; - stream.cache[label].options.open.call(this, event); + stream.cache[ label ].options.open.call(this, event); } }, false); stream.cache[label].stream.addEventListener("message", function(event) { - + var streamData = []; - if ( stream.cache[label] ) { + if ( stream.cache[ label ] ) { streamData[ streamData.length ] = jQuery.parseJSON( event.data ); this.label = label; - stream.cache[label].lastEventId = +event.lastEventId; - stream.cache[label].history[stream.cache[label].lastEventId] = streamData; - stream.cache[label].options.message.call(this, streamData[0] ? streamData[0] : null, { + stream.cache[ label ].lastEventId = +event.lastEventId; + stream.cache[ label ].history[stream.cache[ label ].lastEventId] = streamData; + stream.cache[ label ].options.message.call(this, streamData[0] ? streamData[0] : null, { data: streamData, - lastEventId: stream.cache[label].lastEventId + lastEventId: stream.cache[ label ].lastEventId }, event); - // TODO: Add custom event triggering + // TODO: Add custom event triggering } }, false); - return stream.cache[label].stream; - }, + return stream.cache[ label ].stream; + }, // open fallback event source openPollingSource: function( options ) { - var label = options.label, + var label = options.label, source; - if ( stream.cache[label] ) { + if ( stream.cache[ label ] ) { source = jQuery.ajax({ type: "GET", url: options.url, data: options.data, beforeSend: function() { - if ( stream.cache[label] ) { + if ( stream.cache[ label ] ) { this.label = label; - stream.cache[label].options.open.call( this ); + stream.cache[ label ].options.open.call( this ); } }, success: function( data ) { @@ -124,46 +135,58 @@ var tempdata, label = options.label, parsedData = [], - streamData = jQuery.map( data.split("\n"), function(sdata, i) { + streamData = jQuery.map( data.split("\n\n"), function(sdata, i) { return !!sdata && sdata; - }), - idx = 0, length = streamData.length; + }), + idx = 0, length = streamData.length, + rretryprefix = /retry/, + retries; - if ( jQuery.isArray(streamData) ) { + if ( jQuery.isArray( streamData ) ) { for ( ; idx < length; idx++ ) { - if ( streamData[idx] ) { - tempdata = streamData[idx].split("data: ")[ 1 ]; + if ( streamData[ idx ] ) { - // Convert `dataType` here - if ( options.dataType === "json" ) { - tempdata = jQuery.parseJSON( tempdata ); - } + if ( rretryprefix.test( streamData[ idx ] ) && + (retries = streamData[ idx ].split("retry: ")).length ) { + + if ( retries.length === 2 && !retries[ 0 ] ) { - parsedData[ parsedData.length ] = tempdata; + stream.cache[ label ].retry = stream.cache[ label ].options.retry = +retries[ 1 ]; + } + + } else { + tempdata = streamData[ idx ].split("data: ")[ 1 ]; + + // Convert `dataType` here + if ( options.dataType === "json" ) { + tempdata = jQuery.parseJSON( tempdata ); + } + + parsedData[ parsedData.length ] = tempdata; + } } } } - if ( stream.cache[label] ) { + if ( stream.cache[ label ] ) { this.label = label; - stream.cache[label].lastEventId++; - stream.cache[label].history[stream.cache[label].lastEventId] = parsedData; - stream.cache[label].options.message.call(this, parsedData[0] ? parsedData[0] : null, { + stream.cache[ label ].lastEventId++; + stream.cache[ label ].history[ stream.cache[ label ].lastEventId ] = parsedData; + stream.cache[ label ].options.message.call(this, parsedData[0] ? parsedData[0] : null, { data: parsedData, - lastEventId: stream.cache[label].lastEventId + lastEventId: stream.cache[ label ].lastEventId }); - setTimeout( function() { pluginFns._private.openPollingSource.call( this, options ); }, - // matches speed of host api EventSource - 500 + // Use server sent retry time if exists or default retry time if not + ( stream.cache[ label ] && stream.cache[ label ].retry ) || 500 ); } }, @@ -184,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] : "*" @@ -192,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; @@ -217,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-event-sources/event-source-2.php b/test-event-sources/event-source-2.php index 66e7121..0d71cb1 100644 --- a/test-event-sources/event-source-2.php +++ b/test-event-sources/event-source-2.php @@ -1,19 +1,19 @@ - array( - 'time' => time(), - 'message' => 'Some kind of foo' - ), - 1 => array( - 'time' => time(), - 'message' => 'Some kind of quux' - ) - ) - ) . "\n"; - -?> \ No newline at end of file + array( + 'time' => time(), + 'message' => 'Some kind of foo' + ), + 1 => array( + 'time' => time(), + 'message' => 'Some kind of quux' + ) + ) + ) . "\n"; + +?> diff --git a/test-event-sources/event-source-retry.php b/test-event-sources/event-source-retry.php new file mode 100644 index 0000000..5a44f36 --- /dev/null +++ b/test-event-sources/event-source-retry.php @@ -0,0 +1,20 @@ + array( + 'time' => time(), + 'message' => 'Some kind of foo' + ), + 1 => array( + 'time' => time(), + 'message' => 'Some kind of quux' + ) + ) + ) . "\n"; + + +?> 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 a205ad2..e39eb38 100644 --- a/test/jquery.eventsource.unit.js +++ b/test/jquery.eventsource.unit.js @@ -10,31 +10,29 @@ function sizeOf(obj) { $(function() { - var params = location.search.slice( 1 ).split( "&" ), + var params = location.search.slice( 1 ).split( "&" ), pairs = {}; - params.forEach(function(param) { + $.each( params, function(idx, param) { var tmp = param.split("="); pairs[ tmp[0] ] = tmp[1]; }); - //document.querySelectorAll("iframe")[0].style.display = "none"; - if ( pairs.nospecit ) { $("iframe").hide(); } }); -test("jQuery.eventsource is a function", function() { +test("is a function", function() { expect(7); - - ok( jQuery.eventsource, "jQuery.eventsource exists" ); + + 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" ); @@ -44,80 +42,80 @@ test("jQuery.eventsource is a function", function() { equal( sizeOf( jQuery.eventsource.streams() ), 0, "There are no streams"); }); - -test("jQuery.eventsource 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(); } stop(); - // PLAIN TEXT EXAMPLE - NO CONTENT TYPE GIVEN + // PLAIN TEXT EXAMPLE NO CONTENT TYPE GIVEN jQuery.eventsource({ label: "text-event-source", url: "../test-event-sources/event-source-1.php", open: function() { - okPlus( true, "#1 jQuery.eventsource fires onopen callback" ); + okPlus( true, "#1 fires onopen callback" ); }, message: function(data) { - okPlus( true, "#1 jQuery.eventsource fires onmessage callback" ); - - okPlus( data, "#1 jQuery.eventsource returns data"); - - okPlus( typeof jQuery.eventsource("close", "text-event-source") === "object", 'jQuery.eventsource("close", "text-event-source") must return an object' ); + 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' ); } }); - // PLAIN TEXT EXAMPLE - HAS CONTENT TYPE + // PLAIN TEXT EXAMPLE HAS CONTENT TYPE jQuery.eventsource({ label: "text-event-source-ct", url: "../test-event-sources/event-source-1.php", dataType: "text", open: function() { - okPlus( true, "#2 jQuery.eventsource fires onopen callback" ); + okPlus( true, "#2 fires onopen callback" ); }, message: function(data) { - okPlus( true, "#2 jQuery.eventsource fires onmessage callback" ); - okPlus( data, "#2 jQuery.eventsource returns data"); - okPlus( typeof jQuery.eventsource("close", "text-event-source-ct") === "object", 'jQuery.eventsource("close", "text-event-source-ct") must return an object' ); + 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' ); } }); - - // PLAIN TEXT EXAMPLE - HAS CONTENT TYPE + + // PLAIN TEXT EXAMPLE HAS CONTENT TYPE jQuery.eventsource({ label: "json-event-source", url: "../test-event-sources/event-source-2.php", dataType: "json", open: function() { - okPlus( true, "#3 jQuery.eventsource fires onopen callback" ); + okPlus( true, "#3 fires onopen callback" ); }, message: function(data) { - okPlus( true, "#3 jQuery.eventsource fires onmessage callback" ); - okPlus( data, "#3 jQuery.eventsource returns data"); - okPlus( typeof jQuery.eventsource("close", "json-event-source") === "object", 'jQuery.eventsource("close", "json-event-source") must return an object' ); + 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' ); } }); }); -test("jQuery.eventsource open/close", function() { +test("open/close", function() { var expects = 5, count = 0; @@ -136,15 +134,15 @@ test("jQuery.eventsource open/close", function() { url: "../test-event-sources/event-source-2.php", dataType: "json", open: function() { - ok( true, "jQuery.eventsource fires onopen callback" ); + ok( true, "fires onopen callback" ); plus(); }, message: function(data) { - ok( true, "jQuery.eventsource fires onmessage callback" ); + ok( true, "fires onmessage callback" ); plus(); - ok( data, "jQuery.eventsource returns data"); + ok( data, "returns data"); plus(); equal( typeof jQuery.eventsource.close("json-event-source-stream"), "object", 'jQuery.eventsource.close("json-event-source-stream") must return an object' ); @@ -157,16 +155,16 @@ test("jQuery.eventsource open/close", function() { }); -test("jQuery.eventsource - multiple concurrent sources - scope tests", function() { - var expects = 12, - count = 0, +test("multiple concurrent sources scope tests", function() { + var expects = 12, + count = 0, down = 3; expect( expects ); - function plus() { + function plus() { if ( ++count === expects ) { - start(); + start(); } } @@ -200,20 +198,20 @@ test("jQuery.eventsource - multiple concurrent sources - scope tests", function( }); }); -test("jQuery.eventsource - breakage 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(); } @@ -294,7 +292,7 @@ test("jQuery.eventsource - breakage tests", function() { } }); -test("jQuery.eventsource streams object", function() { +test("streams object", function() { var expects = 13, count = 0; @@ -370,26 +368,64 @@ test("jQuery.eventsource streams object", function() { }); -test("jQuery.eventsource streams Are Closed", function() { +test("settable retry time in ms", function() { + + var expects = 2, + count = 0, + stream; - var expects = 2, + expect( expects ); + + function plus() { + if ( ++count === expects ) { + jQuery.eventsource.close(); + start(); + } + } + + stop(); + + // labeled stream + jQuery.eventsource({ + label: "retry-stream", + url: "../test-event-sources/event-source-retry.php", + dataType: "json", + message: function() { + + var stream = jQuery.eventsource.streams("retry-stream"), + streamretry = jQuery.eventsource.streams("retry-stream").retry; + + if ( !stream.isHostApi ) { + equal( streamretry, 1000, "jQuery.eventsource.streams('retry-stream') has a retry time of 1000ms" ); + } else { + ok( true, "retry time is managed by the implementation when provided from a server message" ); + } + plus(); + } + }); +}); + + +test("streams Are Closed", function() { + + 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 + + + + + + +