initial commit
[namibia] / public / js / vendor / ape-source / Transport / Transport.longPolling.js
1 Request = new Class({
2
3         Extends: Request,
4
5         send: function(options) {
6                 //mootools set onreadystatechange after xhr.open, in webkit, this cause readyState 1 to be never fired
7                 if (Browser.Engine.webkit) this.xhr.onreadystatechange = this.onStateChange.bind(this);
8                 return this.parent(options);
9         },
10
11         onStateChange: function() {
12                 if (this.xhr.readyState == 1) this.dataSent = true;
13                 this.parent();
14         }
15 });
16
17 APE.Transport.longPolling = new Class({
18
19         initialize: function(ape) { 
20                 this.ape = ape;
21                 this.requestFailObserver = [];
22         },
23
24         send: function(queryString, options) {
25                 var request = new Request({
26                         url: this.ape.serverUri,
27                         onFailure: this.ape.requestFail.bind(this.ape, [-2, this]),
28                         onComplete: function(resp) {
29                                 $clear(this.requestFailObserver.shift());
30                                 this.ape.parseResponse(resp, options.requestCallback);
31                         }.bind(this)
32                 }).send(queryString);
33                 request.id = $time();
34
35                 this.request = request;
36
37                 this.requestFailObserver.push(this.ape.requestFail.delay(this.ape.options.pollTime + 10000, this.ape, [-1, request]));
38
39                 return request;
40         },
41
42         running: function() {
43                 return this.request ? this.request.running : false;
44         },
45
46         cancel: function() {
47                 if (this.request) this.request.cancel();
48                 $clear(this.requestFailObserver.shift());
49         }
50 });
51
52 APE.Transport.longPolling.browserSupport = function() { return Browser.Features.xhr ? true : 2; };