From 934252d0b748cd9f249d87175777ae35086ef2cd Mon Sep 17 00:00:00 2001
From: rwldrn
Date: Tue, 14 Jun 2011 09:08:47 -0400
Subject: [PATCH 1/6] fix all quotes, single to double
---
test/jquery.eventsource.specit.js | 34 ++++----
test/jquery.eventsource.unit.js | 134 ++++++++++++++----------------
2 files changed, 79 insertions(+), 89 deletions(-)
diff --git a/test/jquery.eventsource.specit.js b/test/jquery.eventsource.specit.js
index d7d7377..eb2948c 100644
--- a/test/jquery.eventsource.specit.js
+++ b/test/jquery.eventsource.specit.js
@@ -12,11 +12,11 @@ describe("jQuery.EventSource", function() {
var streams = {}, labelfor, stypeOf;
before(function() {
- labelfor = 'text-event-source';
+ labelfor = "text-event-source";
stypeOf = window.EventSource ? EventSource : XMLHttpRequest;
streams = $.eventsource({
label: labelfor,
- url: '../test-event-sources/event-source-1.php'
+ url: "../test-event-sources/event-source-1.php"
});
});
@@ -24,17 +24,17 @@ describe("jQuery.EventSource", function() {
it("$.eventsource streams cache", function() {
- assert($.eventsource('streams')).should(beAn, Object);
- assert(sizeOf($.eventsource('streams'))).should(beLessThanOrEqualTo, 1);
+ assert($.eventsource("streams")).should(beAn, Object);
+ assert(sizeOf($.eventsource("streams"))).should(beLessThanOrEqualTo, 1);
});
it("$.eventsource stream object", function() {
- assert(streams[labelfor]).should(include, 'history');
- assert(streams[labelfor]).should(include, 'isHostApi');
- assert(streams[labelfor]).should(include, 'lastEventId');
- assert(streams[labelfor]).should(include, 'options');
- assert(streams[labelfor]).should(include, 'stream');
+ assert(streams[labelfor]).should(include, "history");
+ assert(streams[labelfor]).should(include, "isHostApi");
+ assert(streams[labelfor]).should(include, "lastEventId");
+ assert(streams[labelfor]).should(include, "options");
+ assert(streams[labelfor]).should(include, "stream");
});
it("$.eventsource stream object should be", function() {
@@ -49,10 +49,10 @@ describe("jQuery.EventSource", function() {
it("$.eventsource stream options object", function() {
- assert(streams[labelfor].options).should(include, 'url');
- assert(streams[labelfor].options).should(include, 'label');
- assert(streams[labelfor].options).should(include, 'message');
- assert(streams[labelfor].options).should(include, 'open');
+ assert(streams[labelfor].options).should(include, "url");
+ assert(streams[labelfor].options).should(include, "label");
+ assert(streams[labelfor].options).should(include, "message");
+ assert(streams[labelfor].options).should(include, "open");
});
it("$.eventsource stream options object should be", function() {
@@ -66,10 +66,10 @@ describe("jQuery.EventSource", function() {
it("$.eventsource stream closing", function() {
- $.eventsource('close', labelfor);
+ $.eventsource("close", labelfor);
- assert($.eventsource('streams')).should(beAn, Object);
- assert(sizeOf($.eventsource('streams'))).should(beLessThanOrEqualTo, 0);
+ assert($.eventsource("streams")).should(beAn, Object);
+ assert(sizeOf($.eventsource("streams"))).should(beLessThanOrEqualTo, 0);
});
@@ -78,4 +78,4 @@ describe("jQuery.EventSource", function() {
streams = {};
});
-});
\ No newline at end of file
+});
diff --git a/test/jquery.eventsource.unit.js b/test/jquery.eventsource.unit.js
index edd581f..3bf8ddd 100644
--- a/test/jquery.eventsource.unit.js
+++ b/test/jquery.eventsource.unit.js
@@ -11,7 +11,7 @@ function sizeOf(obj) {
test("$.eventsource is a function", function() {
ok( $.isFunction($.eventsource), "$.eventsource is a function" );
- equals(sizeOf($.eventsource('streams')), 0, 'There are no streams');
+ equals(sizeOf($.eventsource("streams")), 0, "There are no streams");
});
test("$.eventsource callbacks", function() {
@@ -19,8 +19,8 @@ test("$.eventsource is a function", function() {
stop();
// PLAIN TEXT EXAMPLE - NO CONTENT TYPE GIVEN
$.eventsource({
- label: 'text-event-source',
- url: '../test-event-sources/event-source-1.php',
+ label: "text-event-source",
+ url: "../test-event-sources/event-source-1.php",
open: function () {
ok( true, "#1 $.eventsource fires onopen callback" );
},
@@ -29,15 +29,15 @@ test("$.eventsource is a function", function() {
ok( data, "#1 $.eventsource returns data");
- ok( typeof $.eventsource('close', 'text-event-source') === 'object', '$.eventsource("close", "text-event-source") must return an object' );
+ ok( typeof $.eventsource("close", "text-event-source") === "object", '$.eventsource("close", "text-event-source") must return an object' );
}
});
// PLAIN TEXT EXAMPLE - HAS CONTENT TYPE
$.eventsource({
- label: 'text-event-source-ct',
- url: '../test-event-sources/event-source-1.php',
- dataType: 'text',
+ label: "text-event-source-ct",
+ url: "../test-event-sources/event-source-1.php",
+ dataType: "text",
open: function () {
ok( true, "#2 $.eventsource fires onopen callback" );
},
@@ -46,16 +46,16 @@ test("$.eventsource is a function", function() {
ok( data, "#2 $.eventsource returns data");
- ok( typeof $.eventsource('close', 'text-event-source-ct') === 'object', '$.eventsource("close", "text-event-source-ct") must return an object' );
+ ok( typeof $.eventsource("close", "text-event-source-ct") === "object", '$.eventsource("close", "text-event-source-ct") must return an object' );
}
});
// PLAIN TEXT EXAMPLE - HAS CONTENT TYPE
$.eventsource({
- label: 'json-event-source',
- url: '../test-event-sources/event-source-2.php',
- dataType: 'json',
+ label: "json-event-source",
+ url: "../test-event-sources/event-source-2.php",
+ dataType: "json",
open: function () {
ok( true, "#3 $.eventsource fires onopen callback" );
},
@@ -65,7 +65,7 @@ test("$.eventsource is a function", function() {
ok( data, "#3 $.eventsource returns data");
- ok( typeof $.eventsource('close', 'json-event-source') === 'object', '$.eventsource("close", "json-event-source") must return an object' );
+ ok( typeof $.eventsource("close", "json-event-source") === "object", '$.eventsource("close", "json-event-source") must return an object' );
}
});
@@ -82,9 +82,9 @@ test("$.eventsource is a function", function() {
stop();
$.eventsource({
- label: 'json-event-source-stream',
- url: '../test-event-sources/event-source-2.php',
- dataType: 'json',
+ label: "json-event-source-stream",
+ url: "../test-event-sources/event-source-2.php",
+ dataType: "json",
open: function () {
ok( true, "#4 $.eventsource fires onopen callback" );
},
@@ -94,11 +94,11 @@ test("$.eventsource is a function", function() {
ok( data, "#4 $.eventsource returns data");
- equals(sizeOf($.eventsource('streams')), 1, 'there is only 1 active stream');
+ equals(sizeOf($.eventsource("streams")), 1, "there is only 1 active stream");
- ok( typeof $.eventsource('close', 'json-event-source-stream') === 'object', '$.eventsource("close", "json-event-source-stream") must return an object' );
+ ok( typeof $.eventsource("close", "json-event-source-stream") === "object", '$.eventsource("close", "json-event-source-stream") must return an object' );
- equals(sizeOf($.eventsource('streams')), 0, 'there are 0 active streams');
+ equals(sizeOf($.eventsource("streams")), 0, "there are 0 active streams");
}
});
@@ -115,46 +115,46 @@ test("$.eventsource is a function", function() {
stop();
$.eventsource({
- label: 'event-source-1',
- url: '../test-event-sources/event-source-2.php',
- dataType: 'json',
+ label: "event-source-1",
+ url: "../test-event-sources/event-source-2.php",
+ dataType: "json",
open: function () {
ok( true, "event-source-1 fires onopen callback" );
},
message: function (data) {
- equals(this.label, 'event-source-1', 'Correct EventSource returned, looking for `event-source-1`');
+ equals(this.label, "event-source-1", "Correct EventSource returned, looking for `event-source-1`");
ok( true, "event-source-1 fires onmessage callback" );
}
});
$.eventsource({
- label: 'event-source-2',
- url: '../test-event-sources/event-source-2.php',
- dataType: 'json',
+ label: "event-source-2",
+ url: "../test-event-sources/event-source-2.php",
+ dataType: "json",
open: function () {
ok( true, "event-source-2 fires onopen callback" );
},
message: function (data) {
- equals(this.label, 'event-source-2', 'Correct EventSource returned, looking for `event-source-2`');
+ equals(this.label, "event-source-2", "Correct EventSource returned, looking for `event-source-2`");
ok( true, "event-source-2 fires onmessage callback" );
}
});
$.eventsource({
- label: 'event-source-3',
- url: '../test-event-sources/event-source-2.php',
- dataType: 'json',
+ label: "event-source-3",
+ url: "../test-event-sources/event-source-2.php",
+ dataType: "json",
open: function () {
ok( true, "event-source-3 fires onopen callback" );
},
message: function (data) {
- equals(this.label, 'event-source-3', 'Correct EventSource returned, looking for `event-source-3`');
+ equals(this.label, "event-source-3", "Correct EventSource returned, looking for `event-source-3`");
ok( true, "event-source-3 fires onmessage callback" );
}
});
@@ -171,9 +171,9 @@ test("$.eventsource is a function", function() {
setTimeout(function() {
- equals(2, sizeOf($.eventsource('close', 'event-source-1')), 'Closing `event-source-1`, 2 event sources remaining');
- equals(1, sizeOf($.eventsource('close', 'event-source-2')), 'Closing `event-source-2`, 1 event sources remaining');
- equals(0, sizeOf($.eventsource('close', 'event-source-3')), 'Closing `event-source-3`, 0 event sources remaining');
+ equals(2, sizeOf($.eventsource("close", "event-source-1")), "Closing `event-source-1`, 2 event sources remaining");
+ equals(1, sizeOf($.eventsource("close", "event-source-2")), "Closing `event-source-2`, 1 event sources remaining");
+ equals(0, sizeOf($.eventsource("close", "event-source-3")), "Closing `event-source-3`, 0 event sources remaining");
start();
}, 500);
});
@@ -187,7 +187,7 @@ test("$.eventsource is a function", function() {
$.eventsource({});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance not having any options set');
+ ok(true, "Caught the error thrown by the instance not having any options set");
}
try {
@@ -196,7 +196,7 @@ test("$.eventsource is a function", function() {
message: function (data) {}
});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance not being provided a url');
+ ok(true, "Caught the error thrown by the instance not being provided a url");
}
try {
@@ -206,7 +206,7 @@ test("$.eventsource is a function", function() {
message: function (data) {}
});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance options.url being `null`');
+ ok(true, "Caught the error thrown by the instance options.url being `null`");
}
@@ -217,18 +217,18 @@ test("$.eventsource is a function", function() {
message: function (data) {}
});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance options.url being `undefined`');
+ ok(true, "Caught the error thrown by the instance options.url being `undefined`");
}
try {
$.eventsource({
- url: '',
+ url: "",
open: function () {},
message: function (data) {}
});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance options.url being an empty string');
+ ok(true, "Caught the error thrown by the instance options.url being an empty string");
}
@@ -239,7 +239,7 @@ test("$.eventsource is a function", function() {
message: function (data) {}
});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance options.url being `false`');
+ ok(true, "Caught the error thrown by the instance options.url being `false`");
}
@@ -250,25 +250,15 @@ test("$.eventsource is a function", function() {
message: function (data) {}
});
} catch(err) {
- ok(true, 'Caught the error thrown by the instance options.url being `true`');
+ ok(true, "Caught the error thrown by the instance options.url being `true`");
}
setTimeout(function(){
start();
- }, 3000);
- /*
- // this will error, but not testing?
- $.eventsource({
- label: 'event-source-3',
- url: '../test-event-sources/event-source-10.php',
- dataType: 'json',
- open: function () {},
- message: function (data) {}
- });
- */
-
+ }, 3000);
- });
+
+ });
@@ -284,8 +274,8 @@ test("$.eventsource is a function", function() {
setTimeout(function(){
// labeled stream
$.eventsource({
- label: 'labeled-stream',
- url: '../test-event-sources/event-source-1.php',
+ label: "labeled-stream",
+ url: "../test-event-sources/event-source-1.php",
open: function () {
},
message: function (data) {
@@ -294,7 +284,7 @@ test("$.eventsource is a function", function() {
// unlabeled stream
$.eventsource({
- url: '../test-event-sources/event-source-2.php',
+ url: "../test-event-sources/event-source-2.php",
open: function () {
},
message: function (data) {
@@ -304,47 +294,47 @@ test("$.eventsource is a function", function() {
// no callbacks
$.eventsource({
- url: '../test-event-sources/event-source-2.php'
+ url: "../test-event-sources/event-source-2.php"
});
- var streamsObj = $.eventsource('streams');
+ var streamsObj = $.eventsource("streams");
- ok( typeof streamsObj === 'object', '$.eventsource("streams") must return an object' );
+ ok( typeof streamsObj === "object", "$.eventsource("streams") must return an object" );
$.each(streamsObj, function (i, obj) {
- equals( typeof obj.isHostApi, 'boolean', 'Stream.isHostApi exists and is a boolean value' );
+ equals( typeof obj.isHostApi, "boolean", "Stream.isHostApi exists and is a boolean value" );
- equals( typeof obj.lastEventId, 'number', 'Stream.lastEventId exists and is a boolean value' );
+ equals( typeof obj.lastEventId, "number", "Stream.lastEventId exists and is a boolean value" );
- equals( obj.options.label && obj.options.label !== '', true, 'Stream.options.label exists and not an empty string' );
+ equals( obj.options.label && obj.options.label !== "", true, "Stream.options.label exists and not an empty string" );
- equals( obj.options.message && $.isFunction(obj.options.message), true, 'Stream.options.message exists and is a function' );
+ equals( obj.options.message && $.isFunction(obj.options.message), true, "Stream.options.message exists and is a function" );
- equals( obj.options.open && $.isFunction(obj.options.open), true, 'Stream.options.message exists and is a function' );
+ equals( obj.options.open && $.isFunction(obj.options.open), true, "Stream.options.message exists and is a function" );
if ( obj.isHostApi ) {
- equals( obj.stream.toString(), '[object EventSource]', 'Native Streams are [object EventSource]' );
+ equals( obj.stream.toString(), "[object EventSource]", "Native Streams are [object EventSource]" );
}
if ( !obj.isHostApi ) {
if ( window.XMLHttpRequest ) {
- equals( obj.stream.toString(), '[object XMLHttpRequest]', 'Non-Native Streams are [object XMLHttpRequest]' );
+ equals( obj.stream.toString(), "[object XMLHttpRequest]", "Non-Native Streams are [object XMLHttpRequest]" );
}
else {
- ok( true, 'Missing IE Stream type test!!' );
+ ok( true, "Missing IE Stream type test!!" );
}
}
});
- equals( typeof $.eventsource('close'), 'object', '$.eventsource("close") must return an object' );
+ equals( typeof $.eventsource("close"), "object", "$.eventsource("close") must return an object" );
- equals( typeof $.eventsource('streams'), 'object', '$.eventsource("streams") must return an object' );
+ equals( typeof $.eventsource("streams"), "object", "$.eventsource("streams") must return an object" );
start();
@@ -365,9 +355,9 @@ test("$.eventsource is a function", function() {
stop();
- equals(sizeOf($.eventsource('streams')), 0, 'there are 0 active streams');
+ equals(sizeOf($.eventsource("streams")), 0, "there are 0 active streams");
- ok( typeof $.eventsource('streams') === 'object', '$.eventsource("streams") must return an object' );
+ ok( typeof $.eventsource("streams") === "object", '$.eventsource("streams") must return an object' );
setTimeout(function(){
From 9752ef5b9580c418ebad36962ffb6b297921bfc5 Mon Sep 17 00:00:00 2001
From: rwldrn
Date: Tue, 14 Jun 2011 10:41:39 -0400
Subject: [PATCH 2/6] Non-native us [object Object]. Fixes #6
---
jquery.eventsource.js | 44 ++-
test/jquery.eventsource.unit.js | 604 +++++++++++++++++---------------
2 files changed, 342 insertions(+), 306 deletions(-)
diff --git a/jquery.eventsource.js b/jquery.eventsource.js
index 51281a9..0a92dd0 100644
--- a/jquery.eventsource.js
+++ b/jquery.eventsource.js
@@ -35,26 +35,29 @@
pluginFns = {
public: {
- close: function ( label ) {
+ close: function( label ) {
- var cache = {};
-
- if ( label !== "*" ) {
+ var tmp = {};
- for ( var prop in stream.cache ) {
- if ( label !== prop ) {
- cache[ prop ] = stream.cache[ prop ];
- }
+ if ( !label || label === "*" ) {
+ stream.cache = {};
+
+ return stream.cache;
+ }
+
+ for ( var prop in stream.cache ) {
+ if ( label !== prop ) {
+ tmp[ prop ] = stream.cache[ prop ];
}
}
- stream.cache = cache;
+ stream.cache = tmp;
return stream.cache;
},
- streams: function ( label ) {
+ streams: function( label ) {
- if ( label === "*" ) {
+ if ( !label || label === "*" ) {
return stream.cache;
}
@@ -64,10 +67,10 @@
_private: {
// Open a host api event source
- openEventSource: function ( options ) {
+ openEventSource: function( options ) {
var label = options.label;
- stream.cache[ label ].stream.addEventListener("open", function (event) {
+ stream.cache[ label ].stream.addEventListener("open", function(event) {
if ( stream.cache[label] ) {
this.label = label;
@@ -76,7 +79,7 @@
}
}, false);
- stream.cache[label].stream.addEventListener("message", function (event) {
+ stream.cache[label].stream.addEventListener("message", function(event) {
var streamData = [];
@@ -100,7 +103,7 @@
return stream.cache[label].stream;
},
// open fallback event source
- openPollingSource: function ( options ) {
+ openPollingSource: function( options ) {
var label = options.label,
source;
@@ -110,13 +113,13 @@
type: "GET",
url: options.url,
data: options.data,
- beforeSend: function () {
+ beforeSend: function() {
if ( stream.cache[label] ) {
this.label = label;
stream.cache[label].options.open.call( this );
}
},
- success: function ( data ) {
+ success: function( data ) {
var tempdata,
label = options.label,
@@ -156,7 +159,7 @@
setTimeout(
- function () {
+ function() {
pluginFns._private.openPollingSource.call( this, options );
},
// matches speed of host api EventSource
@@ -236,5 +239,10 @@
return stream.cache;
};
+ jQuery.each( [ "close", "streams" ], function( idx, name ) {
+ jQuery.eventsource[ name ] = function( arg ) {
+ return jQuery.eventsource( name, arg || "*" );
+ };
+ });
})(jQuery, window);
diff --git a/test/jquery.eventsource.unit.js b/test/jquery.eventsource.unit.js
index 3bf8ddd..a205ad2 100644
--- a/test/jquery.eventsource.unit.js
+++ b/test/jquery.eventsource.unit.js
@@ -8,360 +8,388 @@ function sizeOf(obj) {
}
-test("$.eventsource is a function", function() {
- ok( $.isFunction($.eventsource), "$.eventsource is a function" );
-
- equals(sizeOf($.eventsource("streams")), 0, "There are no streams");
- });
-
- test("$.eventsource callbacks", function() {
-
- stop();
- // PLAIN TEXT EXAMPLE - NO CONTENT TYPE GIVEN
- $.eventsource({
- label: "text-event-source",
- url: "../test-event-sources/event-source-1.php",
- open: function () {
- ok( true, "#1 $.eventsource fires onopen callback" );
- },
- message: function (data) {
- ok( true, "#1 $.eventsource fires onmessage callback" );
-
- ok( data, "#1 $.eventsource returns data");
-
- ok( typeof $.eventsource("close", "text-event-source") === "object", '$.eventsource("close", "text-event-source") must return an object' );
- }
- });
+$(function() {
- // PLAIN TEXT EXAMPLE - HAS CONTENT TYPE
- $.eventsource({
- label: "text-event-source-ct",
- url: "../test-event-sources/event-source-1.php",
- dataType: "text",
- open: function () {
- ok( true, "#2 $.eventsource fires onopen callback" );
- },
- message: function (data) {
- ok( true, "#2 $.eventsource fires onmessage callback" );
-
- ok( data, "#2 $.eventsource returns data");
-
- ok( typeof $.eventsource("close", "text-event-source-ct") === "object", '$.eventsource("close", "text-event-source-ct") must return an object' );
- }
- });
-
- // PLAIN TEXT EXAMPLE - HAS CONTENT TYPE
+ var params = location.search.slice( 1 ).split( "&" ),
+ pairs = {};
- $.eventsource({
- label: "json-event-source",
- url: "../test-event-sources/event-source-2.php",
- dataType: "json",
- open: function () {
- ok( true, "#3 $.eventsource fires onopen callback" );
- },
- message: function (data) {
-
- ok( true, "#3 $.eventsource fires onmessage callback" );
-
- ok( data, "#3 $.eventsource returns data");
+ params.forEach(function(param) {
- ok( typeof $.eventsource("close", "json-event-source") === "object", '$.eventsource("close", "json-event-source") must return an object' );
- }
- });
+ var tmp = param.split("=");
- setTimeout(function(){
- start();
- }, 500);
+ pairs[ tmp[0] ] = tmp[1];
});
+
+ //document.querySelectorAll("iframe")[0].style.display = "none";
+
+ if ( pairs.nospecit ) {
+ $("iframe").hide();
+ }
+});
+
+
+test("jQuery.eventsource is a function", function() {
+
+ expect(7);
+ ok( jQuery.eventsource, "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" );
+
+ ok( jQuery.eventsource.close, "jQuery.eventsource.close exists" );
+ equal( typeof jQuery.eventsource.close, "function", "jQuery.eventsource.close() is a Function" );
+
+ equal( sizeOf( jQuery.eventsource.streams() ), 0, "There are no streams");
+});
+test("jQuery.eventsource callbacks", function() {
+
+ var expects = 12,
+ count = 0;
+
+ expect( expects );
+
+ function plus() {
+ if ( ++count === expects ) {
+ start();
+ }
+ }
+
+ function okPlus() {
+ ok.apply(null, arguments);
+ plus();
+ }
+
+ stop();
+ // 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" );
+ },
+ 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' );
+ }
+ });
+
+ // 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" );
+ },
+ 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' );
+
+ }
+ });
+
+ // 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" );
+ },
+ 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' );
+
+ }
+ });
+
+});
+
+test("jQuery.eventsource open/close", function() {
+ var expects = 5,
+ count = 0;
+
+ expect( expects );
+
+ function plus() {
+ if ( ++count === expects ) {
+ start();
+ }
+ }
+
+ stop();
+
+ jQuery.eventsource({
+ label: "json-event-source-stream",
+ url: "../test-event-sources/event-source-2.php",
+ dataType: "json",
+ open: function() {
+ ok( true, "jQuery.eventsource fires onopen callback" );
+ plus();
+ },
+ message: function(data) {
+
+ ok( true, "jQuery.eventsource fires onmessage callback" );
+ plus();
+
+ ok( data, "jQuery.eventsource returns data");
+ plus();
+
+ equal( typeof jQuery.eventsource.close("json-event-source-stream"), "object", 'jQuery.eventsource.close("json-event-source-stream") must return an object' );
+ plus();
+
+ equal( sizeOf( jQuery.eventsource.streams() ), 0, "there are 0 active streams");
+ plus();
+ }
+ });
+});
- test("$.eventsource open/close", function() {
- stop();
-
- $.eventsource({
- label: "json-event-source-stream",
- url: "../test-event-sources/event-source-2.php",
- dataType: "json",
- open: function () {
- ok( true, "#4 $.eventsource fires onopen callback" );
- },
- message: function (data) {
-
- ok( true, "#4 $.eventsource fires onmessage callback" );
-
- ok( data, "#4 $.eventsource returns data");
-
- equals(sizeOf($.eventsource("streams")), 1, "there is only 1 active stream");
-
- ok( typeof $.eventsource("close", "json-event-source-stream") === "object", '$.eventsource("close", "json-event-source-stream") must return an object' );
-
- equals(sizeOf($.eventsource("streams")), 0, "there are 0 active streams");
- }
- });
+test("jQuery.eventsource - multiple concurrent sources - scope tests", function() {
+ var expects = 12,
+ count = 0,
+ down = 3;
-
- setTimeout(function(){
+ expect( expects );
+
+ function plus() {
+ if ( ++count === expects ) {
start();
- }, 1500);
-
- });
+ }
+ }
+ stop();
- test("$.eventsource - multiple concurrent sources - scope tests", function() {
- stop();
-
- $.eventsource({
- label: "event-source-1",
- url: "../test-event-sources/event-source-2.php",
- dataType: "json",
- open: function () {
-
- ok( true, "event-source-1 fires onopen callback" );
- },
- message: function (data) {
-
- equals(this.label, "event-source-1", "Correct EventSource returned, looking for `event-source-1`");
- ok( true, "event-source-1 fires onmessage callback" );
- }
- });
-
- $.eventsource({
- label: "event-source-2",
- url: "../test-event-sources/event-source-2.php",
+ jQuery.each( [ 1, 2, 3 ], function( idx, stream ) {
+
+ jQuery.eventsource({
+ label: "event-source-" + stream ,
+ url: "../test-event-sources/event-source-2.php",
dataType: "json",
- open: function () {
-
- ok( true, "event-source-2 fires onopen callback" );
+ open: function() {
+
+ ok( true, "Stream #" + stream + ", event-source-" + stream + " fires onopen callback" );
+ plus();
},
- message: function (data) {
-
- equals(this.label, "event-source-2", "Correct EventSource returned, looking for `event-source-2`");
- ok( true, "event-source-2 fires onmessage callback" );
+ message: function(data) {
+
+ equal(this.label, "event-source-" + stream, "Stream #" + stream + ", Correct EventSource returned, looking for `event-source-" + stream + "`");
+ plus();
+
+ ok( true, "Stream #" + stream + ", event-source-" + stream + " fires onmessage callback" );
+ plus();
+
+ jQuery.eventsource.close( "event-source-" + stream );
+
+ equal( sizeOf( jQuery.eventsource.streams() ), --down, "Stream #" + stream + " is closed" );
+ plus();
}
});
+ });
+});
- $.eventsource({
- label: "event-source-3",
- url: "../test-event-sources/event-source-2.php",
- dataType: "json",
- open: function () {
- ok( true, "event-source-3 fires onopen callback" );
- },
- message: function (data) {
-
-
- equals(this.label, "event-source-3", "Correct EventSource returned, looking for `event-source-3`");
- ok( true, "event-source-3 fires onmessage callback" );
- }
- });
+test("jQuery.eventsource - breakage tests", function() {
+ var expects = 7,
+ count = 0;
- setTimeout(function(){
- start();
- }, 2000);
- });
-
+ expect( expects );
- test("$.eventsource - multiple concurrent sources - scope tests: closing sources one at a time", function() {
- stop();
-
-
- setTimeout(function() {
- equals(2, sizeOf($.eventsource("close", "event-source-1")), "Closing `event-source-1`, 2 event sources remaining");
- equals(1, sizeOf($.eventsource("close", "event-source-2")), "Closing `event-source-2`, 1 event sources remaining");
- equals(0, sizeOf($.eventsource("close", "event-source-3")), "Closing `event-source-3`, 0 event sources remaining");
+ function plus() {
+ if ( ++count === expects ) {
start();
- }, 500);
- });
-
+ }
+ }
- test("$.eventsource - breakage tests", function() {
-
- stop();
-
- try {
- $.eventsource({});
-
- } catch(err) {
- ok(true, "Caught the error thrown by the instance not having any options set");
- }
-
- try {
- $.eventsource({
- open: function () {},
- message: function (data) {}
- });
- } catch(err) {
- ok(true, "Caught the error thrown by the instance not being provided a url");
- }
-
- try {
- $.eventsource({
- url: null,
- open: function () {},
- message: function (data) {}
- });
- } catch(err) {
- ok(true, "Caught the error thrown by the instance options.url being `null`");
- }
-
-
- try {
- $.eventsource({
- url: undefined,
- open: function () {},
- message: function (data) {}
- });
- } catch(err) {
- ok(true, "Caught the error thrown by the instance options.url being `undefined`");
- }
-
-
- try {
- $.eventsource({
- url: "",
- open: function () {},
- message: function (data) {}
- });
- } catch(err) {
- ok(true, "Caught the error thrown by the instance options.url being an empty string");
- }
-
-
- try {
- $.eventsource({
- url: false,
- open: function () {},
- message: function (data) {}
- });
- } catch(err) {
- ok(true, "Caught the error thrown by the instance options.url being `false`");
- }
-
-
- try {
- $.eventsource({
- url: true,
- open: function () {},
- message: function (data) {}
- });
- } catch(err) {
- ok(true, "Caught the error thrown by the instance options.url being `true`");
- }
-
- setTimeout(function(){
- start();
- }, 3000);
+ function okPlus() {
+ ok.apply(null, arguments);
+ plus();
+ }
- });
+ stop();
+ try {
+ jQuery.eventsource({});
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance not having any options set");
+ }
+ try {
+ jQuery.eventsource({
+ open: function() {},
+ message: function(data) {}
+ });
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance not being provided a url");
+ }
+ try {
+ jQuery.eventsource({
+ url: null,
+ open: function() {},
+ message: function(data) {}
+ });
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance options.url being `null`");
+ }
- test("$.eventsource streams object", function() {
+ try {
+ jQuery.eventsource({
+ url: undefined,
+ open: function() {},
+ message: function(data) {}
+ });
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance options.url being `undefined`");
+ }
- stop();
- setTimeout(function(){
- // labeled stream
- $.eventsource({
- label: "labeled-stream",
- url: "../test-event-sources/event-source-1.php",
- open: function () {
- },
- message: function (data) {
- }
- });
+ try {
+ jQuery.eventsource({
+ url: "",
+ open: function() {},
+ message: function(data) {}
+ });
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance options.url being an empty string");
+ }
- // unlabeled stream
- $.eventsource({
- url: "../test-event-sources/event-source-2.php",
- open: function () {
- },
- message: function (data) {
- }
- });
+ try {
+ jQuery.eventsource({
+ url: false,
+ open: function() {},
+ message: function(data) {}
+ });
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance options.url being `false`");
+ }
- // no callbacks
- $.eventsource({
- url: "../test-event-sources/event-source-2.php"
- });
-
-
- var streamsObj = $.eventsource("streams");
+ try {
+ jQuery.eventsource({
+ url: true,
+ open: function() {},
+ message: function(data) {}
+ });
+ } catch(err) {
+ okPlus(true, "Caught the error thrown by the instance options.url being `true`");
+ }
+});
+
+test("jQuery.eventsource streams object", function() {
- ok( typeof streamsObj === "object", "$.eventsource("streams") must return an object" );
+ var expects = 13,
+ count = 0;
+ expect( expects );
- $.each(streamsObj, function (i, obj) {
+ function plus() {
+ if ( ++count === expects ) {
+ start();
+ }
+ }
- equals( typeof obj.isHostApi, "boolean", "Stream.isHostApi exists and is a boolean value" );
+ // labeled stream
+ jQuery.eventsource({
+ label: "labeled-stream",
+ url: "../test-event-sources/event-source-1.php",
+ open: function() {
+ },
+ message: function(data) {
+ }
+ });
- equals( typeof obj.lastEventId, "number", "Stream.lastEventId exists and is a boolean value" );
+ // unlabeled stream
+ jQuery.eventsource({
+ url: "../test-event-sources/event-source-2.php",
+ open: function() {
+ },
+ message: function(data) {
+ }
+ });
- equals( obj.options.label && obj.options.label !== "", true, "Stream.options.label exists and not an empty string" );
+ // no callbacks
+ jQuery.eventsource({
+ url: "../test-event-sources/event-source-2.php"
+ });
- equals( obj.options.message && $.isFunction(obj.options.message), true, "Stream.options.message exists and is a function" );
+ stop(5000);
- equals( obj.options.open && $.isFunction(obj.options.open), true, "Stream.options.message exists and is a function" );
+ var streamsObj = jQuery.eventsource.streams();
- if ( obj.isHostApi ) {
- equals( obj.stream.toString(), "[object EventSource]", "Native Streams are [object EventSource]" );
- }
- if ( !obj.isHostApi ) {
- if ( window.XMLHttpRequest ) {
- equals( obj.stream.toString(), "[object XMLHttpRequest]", "Non-Native Streams are [object XMLHttpRequest]" );
- }
- else {
- ok( true, "Missing IE Stream type test!!" );
- }
- }
+ ok( typeof streamsObj === "object", "jQuery.eventsource('streams') must return an object" );
+ plus();
- });
+ jQuery.each( streamsObj, function(i, obj) {
- equals( typeof $.eventsource("close"), "object", "$.eventsource("close") must return an object" );
+ equal( typeof obj.isHostApi, "boolean", "Stream.isHostApi exists and is a boolean value" );
+ plus();
- equals( typeof $.eventsource("streams"), "object", "$.eventsource("streams") must return an object" );
+ equal( typeof obj.lastEventId, "number", "Stream.lastEventId exists and is a boolean value" );
+ plus();
- start();
+ equal( obj.options.label && obj.options.label !== "", true, "Stream.options.label exists and not an empty string" );
+ plus();
- }, 4000);
+ equal( obj.options.message && jQuery.isFunction(obj.options.message), true, "Stream.options.message exists and is a function" );
+ plus();
+ equal( obj.options.open && jQuery.isFunction(obj.options.open), true, "Stream.options.message exists and is a function" );
+ plus();
+ if ( obj.isHostApi ) {
+ equal( obj.stream.toString(), "[object EventSource]", "Native Streams are [object EventSource]" );
+ plus();
+ } else {
+ equal( obj.stream.toString(), "[object Object]", "Non-Native Streams are [object Object]" );
+ plus();
+ }
+ });
+});
- // ADD TESTING FOR CONTENT TYPES, CHAR ENCODING
+test("jQuery.eventsource streams Are Closed", function() {
+ var expects = 2,
+ count = 0;
- });
+ expect( expects );
+ function plus() {
+ if ( ++count === expects ) {
+ start();
+ }
+ }
- test("$.eventsource streams Are Closed", function() {
-
- stop();
-
- equals(sizeOf($.eventsource("streams")), 0, "there are 0 active streams");
-
- ok( typeof $.eventsource("streams") === "object", '$.eventsource("streams") must return an object' );
+ stop();
+ jQuery.eventsource.close();
+
+ equal(sizeOf(jQuery.eventsource.streams()), 0, "there are 0 active streams");
+ plus();
- setTimeout(function(){
- start();
- }, 1000);
-
- });
+ ok( typeof jQuery.eventsource.streams() === "object", 'jQuery.eventsource.streams() must return an object' );
+ plus();
+});
From 5743baf4ed2d73bdcd44f87562dfefbca6926369 Mon Sep 17 00:00:00 2001
From: rwldrn
Date: Tue, 14 Jun 2011 11:37:47 -0400
Subject: [PATCH 3/6] Settable retry time in MS. Fixes #5
---
jquery.eventsource.js | 75 +++++++++++--------
test-event-sources/event-source-2.php | 38 +++++-----
test-event-sources/event-source-retry.php | 20 ++++++
test/jquery.eventsource.unit.js | 88 ++++++++++++++++-------
4 files changed, 145 insertions(+), 76 deletions(-)
create mode 100644 test-event-sources/event-source-retry.php
diff --git a/jquery.eventsource.js b/jquery.eventsource.js
index 0a92dd0..fe188f5 100644
--- a/jquery.eventsource.js
+++ b/jquery.eventsource.js
@@ -26,6 +26,7 @@
stream: {},
lastEventId: 0,
isHostApi: false,
+ retry: 500,
history: {},
options: {}
},
@@ -71,11 +72,11 @@
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);
@@ -83,40 +84,40 @@
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
}
}, 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 +125,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 ] ) {
+
+ stream.cache[ label ].retry = stream.cache[ label ].options.retry = +retries[ 1 ];
+ }
+
+ } else {
+ tempdata = streamData[ idx ].split("data: ")[ 1 ];
- parsedData[ parsedData.length ] = tempdata;
+ // 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
);
}
},
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/jquery.eventsource.unit.js b/test/jquery.eventsource.unit.js
index a205ad2..5df148a 100644
--- a/test/jquery.eventsource.unit.js
+++ b/test/jquery.eventsource.unit.js
@@ -13,26 +13,24 @@ $(function() {
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" );
@@ -45,7 +43,7 @@ test("jQuery.eventsource is a function", function() {
});
-test("jQuery.eventsource callbacks", function() {
+test("callbacks", function() {
var expects = 12,
count = 0;
@@ -64,52 +62,52 @@ test("jQuery.eventsource callbacks", function() {
}
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( true, "#1 fires onmessage callback" );
- okPlus( data, "#1 jQuery.eventsource returns data");
+ 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( 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( 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' );
}
@@ -117,7 +115,7 @@ test("jQuery.eventsource callbacks", function() {
});
-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,7 +155,7 @@ test("jQuery.eventsource open/close", function() {
});
-test("jQuery.eventsource - multiple concurrent sources - scope tests", function() {
+test("multiple concurrent sources scope tests", function() {
var expects = 12,
count = 0,
down = 3;
@@ -200,7 +198,7 @@ test("jQuery.eventsource - multiple concurrent sources - scope tests", function(
});
});
-test("jQuery.eventsource - breakage tests", function() {
+test("breakage tests", function() {
var expects = 7,
count = 0;
@@ -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,7 +368,45 @@ 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;
+
+ 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;
From cf2cd1caca6ea3ad9c1e896fa44e60f129a37239 Mon Sep 17 00:00:00 2001
From: Or Cohen
Date: Tue, 29 May 2012 19:36:40 +0300
Subject: [PATCH 4/6] Using close() on the stream when host API is used
---
jquery.eventsource.js | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/jquery.eventsource.js b/jquery.eventsource.js
index fe188f5..7599b77 100644
--- a/jquery.eventsource.js
+++ b/jquery.eventsource.js
@@ -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,6 +55,10 @@
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();
+ }
}
}
From 45f3e8aa99314ccf0405efd51725ff6e0aae8026 Mon Sep 17 00:00:00 2001
From: Rick Waldron
Date: Tue, 29 May 2012 13:03:13 -0400
Subject: [PATCH 5/6] Update stuff that I didn't commit many months ago
Signed-off-by: Rick Waldron
---
test/index.html | 13 ++++++-------
test/specit.html | 18 +++++++++---------
test/test.php | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 60 insertions(+), 16 deletions(-)
create mode 100644 test/test.php
diff --git a/test/index.html b/test/index.html
index c567c56..7cac823 100644
--- a/test/index.html
+++ b/test/index.html
@@ -1,12 +1,11 @@
-
+
jquery.eventsource.unit
-
+
-
-
+
+
@@ -17,7 +16,7 @@
-
+
-
+
\ No newline at end of file
diff --git a/test/specit.html b/test/specit.html
index 5de5621..347ae89 100644
--- a/test/specit.html
+++ b/test/specit.html
@@ -1,27 +1,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
+
+
+
+
+
+
+
Date: Tue, 29 May 2012 13:26:03 -0400
Subject: [PATCH 6/6] Update latest qunit
Signed-off-by: Rick Waldron
---
jquery.eventsource.js | 26 +++++++-------
test/index.html | 12 +++----
test/jquery.eventsource.unit.js | 64 ++++++++++++++++-----------------
test/specit.html | 14 ++++----
4 files changed, 58 insertions(+), 58 deletions(-)
diff --git a/jquery.eventsource.js b/jquery.eventsource.js
index 7599b77..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,
@@ -65,7 +65,7 @@
stream.cache = tmp;
return stream.cache;
- },
+ },
streams: function( label ) {
if ( !label || label === "*" ) {
@@ -77,7 +77,7 @@
},
_private: {
- // Open a host api event source
+ // Open a host api event source
openEventSource: function( options ) {
var label = options.label;
@@ -91,7 +91,7 @@
}, false);
stream.cache[label].stream.addEventListener("message", function(event) {
-
+
var streamData = [];
if ( stream.cache[ label ] ) {
@@ -107,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,
@@ -139,7 +139,7 @@
return !!sdata && sdata;
}),
idx = 0, length = streamData.length,
- rretryprefix = /retry/,
+ rretryprefix = /retry/,
retries;
if ( jQuery.isArray( streamData ) ) {
@@ -148,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 ] ) {
@@ -207,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] :
"*"
@@ -215,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;
@@ -240,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
-
-
+
+
-
+
-
+