|
| 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 | + */ |
1 | 10 | (function($){ |
2 | 11 |
|
3 | | - $.ev = $.ev || { |
| 12 | + $.ev = { |
4 | 13 |
|
5 | 14 | handlers : {}, |
6 | 15 | running : false, |
7 | 16 | xhr : null, |
8 | 17 | verbose : true, |
| 18 | + timeout : null, |
9 | 19 |
|
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); |
17 | 33 | } |
18 | 34 | }, |
19 | 35 |
|
| 36 | + /* Method: stop |
| 37 | + * |
| 38 | + * Stop the loop |
| 39 | + * |
| 40 | + */ |
20 | 41 | stop: function() { |
21 | | - this.running = false; |
22 | 42 | if (this.xhr) { |
23 | 43 | this.xhr.abort(); |
24 | 44 | this.xhr = null; |
25 | 45 | } |
26 | | - // Maybe it should let the server side know that it stopped listening. |
| 46 | + this.running = false; |
27 | 47 | }, |
28 | 48 |
|
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) { |
30 | 61 | 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 | + } |
31 | 71 | this.running = true; |
32 | | - if (!channels) channels = []; |
33 | 72 | this.xhr = $.ajax({ |
34 | 73 | type : 'GET', |
35 | 74 | dataType : 'json', |
36 | 75 | 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) |
41 | 80 | }, |
42 | 81 | complete : function(xhr, status) { |
43 | 82 | var delay; |
44 | 83 | if (status == 'success') { |
45 | 84 | delay = 100; |
46 | 85 | } else { |
47 | | - // self.log('status: ' + status, '; waiting before long-polling again...'); |
| 86 | + // console.log('status: ' + status, '; waiting before long-polling again...'); |
48 | 87 | delay = 5000; |
49 | 88 | } |
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); |
53 | 91 | } |
54 | 92 | }); |
55 | 93 | } |
|
0 commit comments