OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * A module that contains basic utility components and methods for the | 7 * A module that contains basic utility components and methods for the |
8 * chromoting project | 8 * chromoting project |
9 * | 9 * |
10 */ | 10 */ |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 function() { | 263 function() { |
264 return Promise.reject(); | 264 return Promise.reject(); |
265 }, | 265 }, |
266 /** @return {Promise} */ | 266 /** @return {Promise} */ |
267 function() { | 267 function() { |
268 return Promise.resolve(); | 268 return Promise.resolve(); |
269 }); | 269 }); |
270 }; | 270 }; |
271 | 271 |
272 /** | 272 /** |
| 273 * Converts a |method| with callbacks into a Promise. |
| 274 * |
| 275 * @param {Function} method |
| 276 * @param {Array} params |
| 277 * @param {*=} opt_context |
| 278 * @param {boolean=} opt_hasErrorHandler whether the method has an error handler |
| 279 * @return {Promise} |
| 280 */ |
| 281 base.Promise.as = function(method, params, opt_context, opt_hasErrorHandler) { |
| 282 return new Promise(function(resolve, reject) { |
| 283 params.push(resolve); |
| 284 if (opt_hasErrorHandler) { |
| 285 params.push(reject); |
| 286 } |
| 287 try { |
| 288 method.apply(opt_context, params); |
| 289 } catch (/** @type {*} */ e) { |
| 290 reject(e); |
| 291 } |
| 292 }); |
| 293 }; |
| 294 |
| 295 /** |
273 * A mixin for classes with events. | 296 * A mixin for classes with events. |
274 * | 297 * |
275 * For example, to create an alarm event for SmokeDetector: | 298 * For example, to create an alarm event for SmokeDetector: |
276 * functionSmokeDetector() { | 299 * functionSmokeDetector() { |
277 * this.defineEvents(['alarm']); | 300 * this.defineEvents(['alarm']); |
278 * }; | 301 * }; |
279 * base.extend(SmokeDetector, base.EventSource); | 302 * base.extend(SmokeDetector, base.EventSource); |
280 * | 303 * |
281 * To fire an event: | 304 * To fire an event: |
282 * SmokeDetector.prototype.onCarbonMonoxideDetected = function() { | 305 * SmokeDetector.prototype.onCarbonMonoxideDetected = function() { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 * @return {Object|undefined} The decoded object, or undefined if the string | 461 * @return {Object|undefined} The decoded object, or undefined if the string |
439 * cannot be parsed. | 462 * cannot be parsed. |
440 */ | 463 */ |
441 base.jsonParseSafe = function(jsonString) { | 464 base.jsonParseSafe = function(jsonString) { |
442 try { | 465 try { |
443 return /** @type {Object} */ (JSON.parse(jsonString)); | 466 return /** @type {Object} */ (JSON.parse(jsonString)); |
444 } catch (err) { | 467 } catch (err) { |
445 return undefined; | 468 return undefined; |
446 } | 469 } |
447 } | 470 } |
OLD | NEW |