| 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 | 6 |
| 7 namespace luci { | 7 namespace luci { |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * An Operation wraps a Promise, adding methods to cancel that Promise. | 10 * An Operation wraps a Promise, adding methods to cancel that Promise. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 static CANCELLED = new Error('Operation is cancelled'); | 25 static CANCELLED = new Error('Operation is cancelled'); |
| 26 | 26 |
| 27 private _cancelled = false; | 27 private _cancelled = false; |
| 28 private cancelCallbacks = new Array<() => void>(); | 28 private cancelCallbacks = new Array<() => void>(); |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * Wraps a Promise, returning a Promise whose resolve and reject methods | 31 * Wraps a Promise, returning a Promise whose resolve and reject methods |
| 32 * will throw a CANCELLED error if this Operation has been cancelled. | 32 * will throw a CANCELLED error if this Operation has been cancelled. |
| 33 */ | 33 */ |
| 34 async wrap<T>(p: Promise<T>) { | 34 async wrap<T>(p: Promise<T>) { |
| 35 return new Promise((resolve, reject) => { | 35 return new Promise<T>((resolve, reject) => { |
| 36 p.then( | 36 p.then( |
| 37 result => { | 37 result => { |
| 38 this.assert(); | 38 this.assert(); |
| 39 resolve(result); | 39 resolve(result); |
| 40 }, | 40 }, |
| 41 err => { | 41 err => { |
| 42 this.assert(); | 42 this.assert(); |
| 43 reject(err); | 43 reject(err); |
| 44 }); | 44 }); |
| 45 }); | 45 }); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 this.cancelCallbacks.splice(index, 1); | 101 this.cancelCallbacks.splice(index, 1); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 /** Perform an asynchronous call. */ | 106 /** Perform an asynchronous call. */ |
| 107 function asyncCall(fn: () => void) { | 107 function asyncCall(fn: () => void) { |
| 108 window.setTimeout(fn, 0); | 108 window.setTimeout(fn, 0); |
| 109 } | 109 } |
| 110 } | 110 } |
| OLD | NEW |