[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 /** 2 * @requires javelin-install 3 * javelin-util 4 * javelin-request 5 * javelin-router 6 * @provides phabricator-shaped-request 7 * @javelin 8 */ 9 10 /** 11 * Send requests with rate limiting and retries, in response to some application 12 * trigger. This is used to implement comment previews in Differential and 13 * Maniphest. 14 */ 15 JX.install('PhabricatorShapedRequest', { 16 17 construct : function(uri, callback, data_callback) { 18 this._uri = uri; 19 this._callback = callback; 20 this._dataCallback = data_callback; 21 }, 22 23 events : ['error'], 24 25 members : { 26 _callback : null, 27 _dataCallback : null, 28 _request : null, 29 _min : null, 30 _defer : null, 31 _last : null, 32 start : function() { 33 this.trigger(); 34 }, 35 36 trigger : function() { 37 38 clearTimeout(this._defer); 39 var data = this._dataCallback(); 40 41 // Waiting on a request, rate-limit. 42 var waiting = (this._request); 43 44 // Just got a request back, rate-limit. 45 var recent = (this._min && (new Date().getTime() < this._min)); 46 47 if (!waiting && !recent && this.shouldSendRequest(this._last, data)) { 48 this._last = data; 49 this._request = new JX.Request(this._uri, JX.bind(this, function(r) { 50 this._callback(r); 51 52 this._min = new Date().getTime() + this.getRateLimit(); 53 clearTimeout(this._defer); 54 this._defer = setTimeout( 55 JX.bind(this, this.trigger), 56 this.getRateLimit() 57 ); 58 })); 59 this._request.listen('error', JX.bind(this, function(error) { 60 this.invoke('error', error, this); 61 })); 62 this._request.listen('finally', JX.bind(this, function() { 63 this._request = null; 64 })); 65 this._request.setData(data); 66 this._request.setTimeout(this.getRequestTimeout()); 67 68 var routable = this._request.getRoutable(); 69 70 routable 71 .setType('draft') 72 .setPriority(750); 73 74 JX.Router.getInstance().queue(routable); 75 } else { 76 this._defer = setTimeout( 77 JX.bind(this, this.trigger), 78 this.getFrequency() 79 ); 80 } 81 }, 82 83 shouldSendRequest : function(last, data) { 84 if (last === null) { 85 return true; 86 } 87 88 for (var k in last) { 89 if (data[k] !== last[k]) { 90 return true; 91 } 92 } 93 return false; 94 } 95 96 }, 97 98 properties : { 99 rateLimit : 500, 100 frequency : 1000, 101 requestTimeout : 20000 102 } 103 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |