Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 result.push(value); | 69 result.push(value); |
| 70 }); | 70 }); |
| 71 return result; | 71 return result; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 base.flattenArray = function(arrayOfArrays) | 74 base.flattenArray = function(arrayOfArrays) |
| 75 { | 75 { |
| 76 if (!arrayOfArrays.length) | 76 if (!arrayOfArrays.length) |
| 77 return []; | 77 return []; |
| 78 return arrayOfArrays.reduce(function(left, right) { | 78 return arrayOfArrays.reduce(function(left, right) { |
| 79 return left.concat(right); | 79 return left.concat(right); |
| 80 }); | 80 }); |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 base.filterDictionary = function(dictionary, predicate) | 83 base.filterDictionary = function(dictionary, predicate) |
| 84 { | 84 { |
| 85 var result = {}; | 85 var result = {}; |
| 86 | 86 |
| 87 for (var key in dictionary) { | 87 for (var key in dictionary) { |
| 88 if (predicate(key)) | 88 if (predicate(key)) |
| 89 result[key] = dictionary[key]; | 89 result[key] = dictionary[key]; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 if (!jsonp.match(/^[^{[]*\(/)) | 149 if (!jsonp.match(/^[^{[]*\(/)) |
| 150 return JSON.parse(jsonp); | 150 return JSON.parse(jsonp); |
| 151 | 151 |
| 152 var startIndex = jsonp.indexOf('(') + 1; | 152 var startIndex = jsonp.indexOf('(') + 1; |
| 153 var endIndex = jsonp.lastIndexOf(')'); | 153 var endIndex = jsonp.lastIndexOf(')'); |
| 154 if (startIndex == 0 || endIndex == -1) | 154 if (startIndex == 0 || endIndex == -1) |
| 155 return {}; | 155 return {}; |
| 156 return JSON.parse(jsonp.substr(startIndex, endIndex - startIndex)); | 156 return JSON.parse(jsonp.substr(startIndex, endIndex - startIndex)); |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 base.RequestTracker = function(requestsInFlight, callback, args) | |
|
abarth-chromium
2014/02/20 06:18:52
\o/
| |
| 160 { | |
| 161 this._requestsInFlight = requestsInFlight; | |
| 162 this._callback = callback; | |
| 163 this._args = args || []; | |
| 164 this._tryCallback(); | |
| 165 }; | |
| 166 | |
| 167 base.RequestTracker.prototype = { | |
| 168 _tryCallback: function() | |
| 169 { | |
| 170 if (!this._requestsInFlight && this._callback) | |
| 171 this._callback.apply(null, this._args); | |
| 172 }, | |
| 173 requestComplete: function() | |
| 174 { | |
| 175 --this._requestsInFlight; | |
| 176 this._tryCallback(); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 base.callInParallel = function(functionList, callback) | |
| 181 { | |
| 182 var requestTracker = new base.RequestTracker(functionList.length, callback); | |
| 183 | |
| 184 $.each(functionList, function(index, func) { | |
| 185 func(function() { | |
| 186 requestTracker.requestComplete(); | |
| 187 }); | |
| 188 }); | |
| 189 }; | |
| 190 | |
| 191 base.AsynchronousCache = function(fetch) | 159 base.AsynchronousCache = function(fetch) |
| 192 { | 160 { |
| 193 this._fetch = fetch; | 161 this._fetch = fetch; |
| 194 this._dataCache = {}; | 162 this._dataCache = {}; |
| 195 this._callbackCache = {}; | 163 this._callbackCache = {}; |
| 196 }; | 164 }; |
| 197 | 165 |
| 198 base.AsynchronousCache.prototype.get = function(key, callback) | 166 base.AsynchronousCache.prototype.get = function(key) |
| 199 { | 167 { |
| 200 var self = this; | 168 var self = this; |
| 169 return new Promise(function(resolve, reject) { | |
| 201 | 170 |
| 202 if (self._dataCache[key]) { | 171 if (self._dataCache[key]) { |
| 203 // FIXME: Consider always calling callback asynchronously. | 172 // FIXME: Consider always calling callback asynchronously. |
| 204 callback(self._dataCache[key]); | 173 resolve(self._dataCache[key]); |
| 205 return; | 174 return; |
| 206 } | 175 } |
| 207 | 176 |
| 208 if (key in self._callbackCache) { | 177 if (key in self._callbackCache) { |
| 209 self._callbackCache[key].push(callback); | 178 self._callbackCache[key].push(resolve); |
| 210 return; | 179 return; |
| 211 } | 180 } |
| 212 | 181 |
| 213 self._callbackCache[key] = [callback]; | 182 self._callbackCache[key] = [resolve]; |
| 214 | 183 |
| 215 self._fetch.call(null, key, function(data) { | 184 self._fetch.call(null, key).then(function(data) { |
| 216 self._dataCache[key] = data; | 185 self._dataCache[key] = data; |
| 217 | 186 |
| 218 var callbackList = self._callbackCache[key]; | 187 var callbackList = self._callbackCache[key]; |
| 219 delete self._callbackCache[key]; | 188 delete self._callbackCache[key]; |
| 220 | 189 |
| 221 callbackList.forEach(function(cachedCallback) { | 190 callbackList.forEach(function(cachedCallback) { |
| 222 cachedCallback(data); | 191 cachedCallback(data); |
| 192 }); | |
| 223 }); | 193 }); |
| 224 }); | 194 }); |
| 225 }; | 195 }; |
| 226 | 196 |
| 227 base.AsynchronousCache.prototype.clear = function() | 197 base.AsynchronousCache.prototype.clear = function() |
| 228 { | 198 { |
| 229 this._dataCache = {}; | 199 this._dataCache = {}; |
| 230 this._callbackCache = {}; | 200 this._callbackCache = {}; |
| 231 } | 201 }; |
| 232 | 202 |
| 233 /* | 203 /* |
| 234 Maintains a dictionary of items, tracking their updates and removing items t hat haven't been updated. | 204 Maintains a dictionary of items, tracking their updates and removing items t hat haven't been updated. |
| 235 An "update" is a call to the "update" method. | 205 An "update" is a call to the "update" method. |
| 236 To remove stale items, call the "remove" method. It will remove all | 206 To remove stale items, call the "remove" method. It will remove all |
| 237 items that have not been been updated since the last call of "remove". | 207 items that have not been been updated since the last call of "remove". |
| 238 */ | 208 */ |
| 239 base.UpdateTracker = function() | 209 base.UpdateTracker = function() |
| 240 { | 210 { |
| 241 this._items = {}; | 211 this._items = {}; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 { | 346 { |
| 377 var link = document.createElement('a'); | 347 var link = document.createElement('a'); |
| 378 link.href = url; | 348 link.href = url; |
| 379 if (opt_target) | 349 if (opt_target) |
| 380 link.target = opt_target; | 350 link.target = opt_target; |
| 381 link.appendChild(document.createTextNode(textContent)); | 351 link.appendChild(document.createTextNode(textContent)); |
| 382 return link; | 352 return link; |
| 383 } | 353 } |
| 384 | 354 |
| 385 })(); | 355 })(); |
| OLD | NEW |