| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | 2 Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | 3 Use of this source code is governed under the Apache License, Version 2.0 |
| 4 that can be found in the LICENSE file. | 4 that can be found in the LICENSE file. |
| 5 | 5 |
| 6 It contains the definition of the following Behaviors: | 6 It contains the definition of the following Behaviors: |
| 7 | 7 |
| 8 SwarmingBehaviors.CommonBehavior | 8 SwarmingBehaviors.CommonBehavior |
| 9 | 9 |
| 10 To use it, include | 10 To use it, include |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 // _getJsonAsync makes an XHR to a url, parses the response as JSON | 32 // _getJsonAsync makes an XHR to a url, parses the response as JSON |
| 33 // and sticks the resulting object into the property with the name given | 33 // and sticks the resulting object into the property with the name given |
| 34 // by "bindTo". If busy is defined, the property with that name will be | 34 // by "bindTo". If busy is defined, the property with that name will be |
| 35 // set to true while the request is in flight and false afterwards. | 35 // set to true while the request is in flight and false afterwards. |
| 36 // request headers (e.g. authentication) and query params will be used if | 36 // request headers (e.g. authentication) and query params will be used if |
| 37 // provided. Query params is an object like {String:Array<String>}. On | 37 // provided. Query params is an object like {String:Array<String>}. On |
| 38 // error, bindTo will be set to false. It is not set to undefined | 38 // error, bindTo will be set to false. It is not set to undefined |
| 39 // because computed values in Polymer don't fire if a property is | 39 // because computed values in Polymer don't fire if a property is |
| 40 // undefined. Clients should check that bindTo is not falsey. | 40 // undefined. Clients should check that bindTo is not falsey. |
| 41 // To avoid multiple requests clobering one another, an object _jsonAsync |
| 42 // is created on "this" to debounce requests - the most recent request |
| 43 // will win out. |
| 41 _getJsonAsync: function(bindTo, url, busy, headers, params) { | 44 _getJsonAsync: function(bindTo, url, busy, headers, params) { |
| 42 if (!bindTo || !url) { | 45 if (!bindTo || !url || !busy) { |
| 43 console.log("Need at least a polymer element to bind to and a url"); | 46 console.log("Need at least a polymer element to bind to, a busy elemen
t, and a url"); |
| 44 return; | 47 return; |
| 45 } | 48 } |
| 46 if (busy) { | 49 this.set(busy, true); |
| 47 this.set(busy, true); | 50 var now = new Date(); |
| 48 } | 51 this._jsonAsync = this._jsonAsync || {}; |
| 52 this._jsonAsync[bindTo] = now; |
| 49 if (params) { | 53 if (params) { |
| 50 url = url + "?" + sk.query.fromParamSet(params); | 54 url = url + "?" + sk.query.fromParamSet(params); |
| 51 } | 55 } |
| 52 sk.request("GET", url, "", headers).then(JSON.parse).then(function(json)
{ | 56 sk.request("GET", url, "", headers).then(JSON.parse).then(function(json)
{ |
| 57 if (this._jsonAsync[bindTo] !== now) { |
| 58 console.log("ignoring result because a second request happened."); |
| 59 this.set(busy, false); |
| 60 return; |
| 61 } |
| 53 this.set(bindTo, json); | 62 this.set(bindTo, json); |
| 54 if (busy) { | 63 this.set(busy, false); |
| 55 this.set(busy, false); | |
| 56 } | |
| 57 }.bind(this)).catch(function(reason){ | 64 }.bind(this)).catch(function(reason){ |
| 58 console.log("Reason for failure of request to " + url, reason); | 65 console.log("Reason for failure of request to " + url, reason); |
| 66 |
| 67 if (this._jsonAsync[bindTo] !== now) { |
| 68 console.log("ignoring failure because a second request happened."); |
| 69 this.set(busy, false); |
| 70 return; |
| 71 } |
| 59 this.set(bindTo, false); | 72 this.set(bindTo, false); |
| 60 if (busy) { | 73 this.set(busy, false); |
| 61 this.set(busy, false); | |
| 62 } | |
| 63 }.bind(this)); | 74 }.bind(this)); |
| 64 }, | 75 }, |
| 65 | 76 |
| 66 _humanDuration: function(timeInSecs) { | 77 _humanDuration: function(timeInSecs) { |
| 67 return sk.human.strDuration(timeInSecs) || "0s"; | 78 return sk.human.strDuration(timeInSecs) || "0s"; |
| 68 }, | 79 }, |
| 69 | 80 |
| 70 _not: function(a) { | 81 _not: function(a) { |
| 71 return !a; | 82 return !a; |
| 72 }, | 83 }, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 return "eons"; | 115 return "eons"; |
| 105 } | 116 } |
| 106 if (!second) { | 117 if (!second) { |
| 107 second = new Date(); | 118 second = new Date(); |
| 108 } | 119 } |
| 109 return this._humanDuration((second.getTime() - first.getTime())/1000); | 120 return this._humanDuration((second.getTime() - first.getTime())/1000); |
| 110 }, | 121 }, |
| 111 }; | 122 }; |
| 112 })(); | 123 })(); |
| 113 </script> | 124 </script> |
| OLD | NEW |