| 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 console.log("ignoring failure because a second request happened."); | 95 console.log("ignoring failure because a second request happened."); |
| 96 this.set(busy, false); | 96 this.set(busy, false); |
| 97 return; | 97 return; |
| 98 } | 98 } |
| 99 this.set(bindTo, false); | 99 this.set(bindTo, false); |
| 100 this.set(busy, false); | 100 this.set(busy, false); |
| 101 return Promise.reject(reason); | 101 return Promise.reject(reason); |
| 102 }.bind(this)); | 102 }.bind(this)); |
| 103 }, | 103 }, |
| 104 | 104 |
| 105 // _getJsonAsyncArr is the array analog to _getJsonAsync. For example, |
| 106 // if there is an array that will contain multiple results rendered with |
| 107 // a dom-repeat, _getJsonAsyncArr will make an XHR GET request and put |
| 108 // the results into the given array at the index specified by idx. |
| 109 // |
| 110 // Prior to making this call, it is assumed that arr is initialized to be |
| 111 // an array of empty (or filler) objects. busyArr be initialized in the |
| 112 // same way. Due to how Polymer deals with arrays, arrays of primitives |
| 113 // are not supported, thus clients observing busyArr should iterate over |
| 114 // all objects in busyArr and test to see if busyArr[i].status is true. |
| 115 // |
| 116 // To avoid multiple requests clobering one another, an object _jsonAsync |
| 117 // is created on "this" to debounce requests - the most recent request |
| 118 // will win out. |
| 119 _getJsonAsyncArr: function(idx, arr, url, busyArr, headers, params) { |
| 120 if (!arr || !url || !busyArr) { |
| 121 console.log("Need at least a polymer array to bind to, a busy element,
and a url"); |
| 122 return; |
| 123 } |
| 124 var key = arr + ":" + idx; |
| 125 this.splice(busyArr, idx, 1, {status:true}); |
| 126 var now = new Date(); |
| 127 this._jsonAsync = this._jsonAsync || {}; |
| 128 this._jsonAsync[key] = now; |
| 129 if (params) { |
| 130 url = url + "?" + sk.query.fromParamSet(params); |
| 131 } |
| 132 return sk.request("GET", url, "", headers).then(JSON.parse).then(functio
n(json){ |
| 133 if (this._jsonAsync[key] !== now) { |
| 134 console.log("ignoring result because a second request happened for "
, key); |
| 135 this.splice(busyArr, idx, 1, {status:false}); |
| 136 return; |
| 137 } |
| 138 this.splice(arr, idx, 1, json); |
| 139 this.splice(busyArr, idx, 1, {status:false}); |
| 140 }.bind(this)).catch(function(reason){ |
| 141 console.log("Reason for failure of request to " + url, reason); |
| 142 |
| 143 if (this._jsonAsync[key] !== now) { |
| 144 console.log("ignoring failure because a second request happened."); |
| 145 this.splice(busyArr, idx, 1, {status:false}); |
| 146 return; |
| 147 } |
| 148 this.splice(busyArr, idx, 1, {status:false}); |
| 149 return Promise.reject(reason); |
| 150 }.bind(this)); |
| 151 }, |
| 152 |
| 105 _humanDuration: function(timeInSecs) { | 153 _humanDuration: function(timeInSecs) { |
| 106 return sk.human.strDuration(timeInSecs) || "0s"; | 154 return sk.human.strDuration(timeInSecs) || "0s"; |
| 107 }, | 155 }, |
| 108 | 156 |
| 109 _not: function(a) { | 157 _not: function(a) { |
| 110 return !a; | 158 return !a; |
| 111 }, | 159 }, |
| 112 | 160 |
| 113 _or: function() { | 161 _or: function() { |
| 114 var result = false; | 162 var result = false; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 203 } |
| 156 return this._humanDuration((second.getTime() - first.getTime())/1000); | 204 return this._humanDuration((second.getTime() - first.getTime())/1000); |
| 157 }, | 205 }, |
| 158 | 206 |
| 159 _truthy: function(a){ | 207 _truthy: function(a){ |
| 160 return !!a; | 208 return !!a; |
| 161 } | 209 } |
| 162 }; | 210 }; |
| 163 })(); | 211 })(); |
| 164 </script> | 212 </script> |
| OLD | NEW |