Permalink
Browse files

Merge branch 'master' of https://github.com/lightpriest/jquery.events…

…ource into close

* 'master' of https://github.com/lightpriest/jquery.eventsource:
  Using close() on the stream when host API is used
  Settable retry time in MS. Fixes #5
  Non-native us [object Object]. Fixes #6
  fix all quotes, single to double
  Updates to test runners
  Make sure streamData is valid before attempting to parse. Fixes #4

Conflicts:
	test/index.html
	test/specit.html

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information...
2 parents 45f3e8a + cf2cd1c commit 280b7574ff281b968b635c638fe0b9d65ee55cc4 @rwaldron committed May 29, 2012
View
@@ -26,6 +26,7 @@
stream: {},
lastEventId: 0,
isHostApi: false,
+ retry: 500,
history: {},
options: {}
},
@@ -35,26 +36,39 @@
pluginFns = {
public: {
- close: function ( label ) {
+ close: function( label ) {
- var cache = {};
-
- if ( label !== "*" ) {
+ var tmp = {};
+ if ( !label || label === "*" ) {
for ( var prop in stream.cache ) {
- if ( label !== prop ) {
- cache[ prop ] = stream.cache[ prop ];
+ if ( stream.cache[ prop ].isHostApi ) {
+ stream.cache[ prop ].stream.close();
+ }
+ }
+
+ stream.cache = {};
+
+ return stream.cache;
+ }
+
+ 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 = cache;
+ stream.cache = tmp;
return stream.cache;
},
- streams: function ( label ) {
+ streams: function( label ) {
- if ( label === "*" ) {
+ if ( !label || label === "*" ) {
return stream.cache;
}
@@ -64,101 +78,115 @@
_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) {
- if ( stream.cache[label] ) {
+ stream.cache[ label ].stream.addEventListener("open", function(event) {
+ 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) {
+ 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
}
}, false);
- return stream.cache[label].stream;
+ return stream.cache[ label ].stream;
},
// open fallback event source
- openPollingSource: function ( options ) {
- var label = options.label,
+ openPollingSource: function( options ) {
+ 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] ) {
+ beforeSend: function() {
+ if ( stream.cache[ label ] ) {
this.label = label;
- stream.cache[label].options.open.call( this );
+ stream.cache[ label ].options.open.call( this );
}
},
- success: function ( data ) {
+ success: function( data ) {
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++ ) {
- 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 ) {
- parsedData[ parsedData.length ] = tempdata;
+ if ( retries.length === 2 && !retries[ 0 ] ) {
+
+ 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 () {
+ 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
);
}
},
@@ -234,5 +262,10 @@
return stream.cache;
};
+ jQuery.each( [ "close", "streams" ], function( idx, name ) {
+ jQuery.eventsource[ name ] = function( arg ) {
+ return jQuery.eventsource( name, arg || "*" );
+ };
+ });
})(jQuery, window);
@@ -1,19 +1,19 @@
-<?php
-header("Content-Type: text/event-stream\n\n");
-
-// despite not having the while(true){}
-// this seems to repeat pushing messages to the client
-echo 'data: ' . json_encode(
- array(
- 0 => array(
- 'time' => time(),
- 'message' => 'Some kind of foo'
- ),
- 1 => array(
- 'time' => time(),
- 'message' => 'Some kind of quux'
- )
- )
- ) . "\n";
-
-?>
+<?php
+header("Content-Type: text/event-stream\n\n");
+
+// despite not having the while(true){}
+// this seems to repeat pushing messages to the client
+echo 'data: ' . json_encode(
+ array(
+ 0 => array(
+ 'time' => time(),
+ 'message' => 'Some kind of foo'
+ ),
+ 1 => array(
+ 'time' => time(),
+ 'message' => 'Some kind of quux'
+ )
+ )
+ ) . "\n";
+
+?>
@@ -0,0 +1,20 @@
+<?php
+header("Content-Type: text/event-stream\n\n");
+
+echo 'retry: 1000' . "\n\n";
+
+echo 'data: ' . json_encode(
+ array(
+ 0 => array(
+ 'time' => time(),
+ 'message' => 'Some kind of foo'
+ ),
+ 1 => array(
+ 'time' => time(),
+ 'message' => 'Some kind of quux'
+ )
+ )
+ ) . "\n";
+
+
+?>
View
@@ -1,22 +1,29 @@
-<!DOCTYPE html>
+<!doctype html>
<html>
<head>
+ <meta charset="UTF-8" />
<title>jquery.eventsource.unit</title>
- <link rel="stylesheet" href="https://raw.github.com/jquery/qunit/master/qunit/qunit.css" type="text/css" media="screen" />
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- <script src="https://raw.github.com/jquery/qunit/master/qunit/qunit.js"></script>
+
+ <link href="qunit/qunit.css" rel="stylesheet">
+ <script src="http://code.jquery.com/jquery.js"></script>
+ <script src="qunit/qunit.js"></script>
+
<script src="../jquery.eventsource.js"></script>
<script src="jquery.eventsource.unit.js"></script>
</head>
<body>
<h1 id="qunit-header">jquery.eventsource.unit.js - must pass 100% in: Chrome, FF, Opera, Safari & IE <!--(<a href="specit.html">specit tests</a>)--></h1>
<h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
- </body>
-
- <iframe src="specit.html" style="width:100%;height:600px;border:0px;"></iframe>
+ <div id="qunit-fixture">
+ </div>
-</html>
+ </body>
+
+<iframe src="specit.html" style="width:100%;height:600px;border:0px;"></iframe>
+
+</html>
@@ -12,29 +12,29 @@ 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"
});
});
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 = {};
});
-});
+});
Oops, something went wrong.

0 comments on commit 280b757

Please sign in to comment.