Skip to content

Commit 853a850

Browse files
author
John Beppu
committed
allow $.ev.run to be overridden for those who want more control; loop method signature changed significantly
1 parent 94b3641 commit 853a850

File tree

2 files changed

+95
-27
lines changed

2 files changed

+95
-27
lines changed

README.textile

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,52 @@ h1. API
77

88
h2. Starting and Stopping the Event Loop
99

10-
h3. $.ev.loop(url, channels)
10+
h3. $.ev.loop(url, handlers)
11+
12+
*url* - the URL you want to long-poll on.
13+
14+
*handlers* - an object that maps event types to event handlers.
15+
16+
Example:
17+
18+
$.ev.loop('/comet/channel/foo/2894293942', {
19+
20+
// ev.type == "backgroundColor"
21+
backgroundColor: function(ev) {
22+
$('body').css({ backgroundColor: ev.color });
23+
},
24+
25+
// ev.type == "foregroundColor"
26+
foregroundColor: function(ev) {
27+
$('body').css({ color: ev.color });
28+
},
29+
30+
// ev.type == "debugMessage"
31+
debugMessage: function(ev) {
32+
console.log(ev.message);
33+
}
34+
35+
});
1136

1237
h3. $.ev.stop()
1338

14-
h2. Adding Event Handlers
39+
This will stop the long-polling loop.
1540

16-
I took a very low-tech approach to event handlers. Basically,
17-
$.ev.handlers is treated as a hash that's keyed by event.type and
18-
contains functions that will run when events of that type come in.
41+
Example:
1942

20-
$.ev.handlers['hello'] = function(ev){ console.log(ev.toSource()) };
43+
$.ev.stop();
2144

45+
h2. Event Handlers
2246

23-
h1. Example
47+
This library makes the assumption that every event from the COMET server
48+
will be an object with a *type* attribute. Based on the value of the
49+
*type* attribute, jQuery.ev will dispatch events to the event handlers.
50+
51+
h1. Code Examples
2452

2553
"http://github.com/beppu/mad-scientists-lab/tree/master/chat/":http://github.com/beppu/mad-scientists-lab/tree/master/chat/
2654

55+
"http://github.com/beppu/stardust/tree/master":http://github.com/beppu/stardust/tree/master
56+
2757
"http://bavl.org:4234/":http://bavl.org:4234/
2858

jquery.ev.js

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,93 @@
1+
/* Title: jQuery.ev
2+
*
3+
* A COMET event loop for jQuery
4+
*
5+
* $.ev.loop long-polls on a URL and expects to get an array of JSON-encoded
6+
* objects back. Each of these objects should represent a message from the COMET
7+
* server that's telling your client-side Javascript to do something.
8+
*
9+
*/
110
(function($){
211

3-
$.ev = $.ev || {
12+
$.ev = {
413

514
handlers : {},
615
running : false,
716
xhr : null,
817
verbose : true,
18+
timeout : null,
919

10-
run: function(events) {
11-
var i;
12-
for (i = 0; i < events.length; i++) {
13-
var e = events[i];
14-
if (!e) continue;
15-
var h = this.handlers[e.type];
16-
if (h) h(e);
20+
/* Method: run
21+
*
22+
* Respond to an array of messages using the object in this.handlers
23+
*
24+
*/
25+
run: function(messages) {
26+
var i, m, h; // index, event, handler
27+
for (i = 0; i < messages.length; i++) {
28+
m = messages[i];
29+
if (!m) continue;
30+
h = this.handlers[m.type];
31+
if (!h) h = this.handlers['*'];
32+
if ( h) h(m);
1733
}
1834
},
1935

36+
/* Method: stop
37+
*
38+
* Stop the loop
39+
*
40+
*/
2041
stop: function() {
21-
this.running = false;
2242
if (this.xhr) {
2343
this.xhr.abort();
2444
this.xhr = null;
2545
}
26-
// Maybe it should let the server side know that it stopped listening.
46+
this.running = false;
2747
},
2848

29-
loop: function(url, channels) {
49+
/*
50+
* Method: loop
51+
*
52+
* Long poll on a URL
53+
*
54+
* Arguments:
55+
*
56+
* url
57+
* handler
58+
*
59+
*/
60+
loop: function(url, handlers) {
3061
var self = this;
62+
if (handlers) {
63+
if (typeof handlers == "object") {
64+
this.handlers = handlers;
65+
} else if (typeof handlers == "function") {
66+
this.run = handlers;
67+
} else {
68+
throw("handlers must be an object or function");
69+
}
70+
}
3171
this.running = true;
32-
if (!channels) channels = [];
3372
this.xhr = $.ajax({
3473
type : 'GET',
3574
dataType : 'json',
3675
url : url,
37-
data : { channels: channels },
38-
success : function(events, status) {
39-
// self.log('success', events);
40-
self.run(events)
76+
timeout : self.timeout,
77+
success : function(messages, status) {
78+
// console.log('success', messages);
79+
self.run(messages)
4180
},
4281
complete : function(xhr, status) {
4382
var delay;
4483
if (status == 'success') {
4584
delay = 100;
4685
} else {
47-
// self.log('status: ' + status, '; waiting before long-polling again...');
86+
// console.log('status: ' + status, '; waiting before long-polling again...');
4887
delay = 5000;
4988
}
50-
window.setTimeout(function(){
51-
if (self.running) self.loop(url, channels);
52-
}, delay);
89+
// "recursively" loop
90+
window.setTimeout(function(){ if (self.running) self.loop(url); }, delay);
5391
}
5492
});
5593
}

0 commit comments

Comments
 (0)