| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// This library adapts ES6 generators to implement Dart's async/await. | 5 /// This library adapts ES6 generators to implement Dart's async/await. |
| 6 /// It's designed to interact with Dart's Future/Stream and follow Dart | 6 /// It's designed to interact with Dart's Future/Stream and follow Dart |
| 7 /// async/await semantics. | 7 /// async/await semantics. |
| 8 /// See https://github.com/dart-lang/sdk/issues/27315 for ideas on | 8 /// See https://github.com/dart-lang/sdk/issues/27315 for ideas on |
| 9 /// reconciling Dart's Future and ES6 Promise. | 9 /// reconciling Dart's Future and ES6 Promise. |
| 10 /// Inspired by `co`: https://github.com/tj/co/blob/master/index.js, which is a | 10 /// Inspired by `co`: https://github.com/tj/co/blob/master/index.js, which is a |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // thrown from the `yield` point, as if it was thrown there. | 35 // thrown from the `yield` point, as if it was thrown there. |
| 36 // | 36 // |
| 37 // If the exception is not caught inside `gen`, it will emerge here, which | 37 // If the exception is not caught inside `gen`, it will emerge here, which |
| 38 // will send it to anyone listening on this async function's Future<T>. | 38 // will send it to anyone listening on this async function's Future<T>. |
| 39 // | 39 // |
| 40 // In essence, we are giving the code inside the generator a chance to | 40 // In essence, we are giving the code inside the generator a chance to |
| 41 // use try-catch-finally. | 41 // use try-catch-finally. |
| 42 return next(iter.throw(err)); | 42 return next(iter.throw(err)); |
| 43 } | 43 } |
| 44 function next(ret) { | 44 function next(ret) { |
| 45 if (ret.done) return ret.value; | 45 let future = ret.value; |
| 46 if (ret.done) { |
| 47 return ret.value; |
| 48 } |
| 46 // Checks if the awaited value is a Future. | 49 // Checks if the awaited value is a Future. |
| 47 let future = ret.value; | 50 if (!$instanceOf(future, $Future)) { |
| 48 if (!$instanceOf(future, ${getGenericClass(Future)})) { | |
| 49 future = $Future.value(future); | 51 future = $Future.value(future); |
| 50 } | 52 } |
| 51 // Chain the Future so `await` receives the Future's value. | 53 // Chain the Future so `await` receives the Future's value. |
| 52 return future.then($dynamic)(onValue, {onError: onError}); | 54 return future.then($dynamic)(onValue, {onError: onError}); |
| 53 } | 55 } |
| 54 return ${getGenericClass(Future)}($T).microtask(function() { | 56 let FutureT = ${getGenericClass(Future)(T)}; |
| 57 return FutureT.microtask(function() { |
| 55 iter = $gen.apply(null, $args)[Symbol.iterator](); | 58 iter = $gen.apply(null, $args)[Symbol.iterator](); |
| 56 return onValue(); | 59 var result = onValue(); |
| 60 if ($strongInstanceOf(result, FutureT) == null) { |
| 61 // Chain the Future<dynamic> to a Future<T> to produce the correct |
| 62 // final type. |
| 63 return result.then($T)((x) => x, {onError: onError}); |
| 64 } else { |
| 65 return result; |
| 66 } |
| 57 }); | 67 }); |
| 58 })()'''); | 68 })()'''); |
| 59 | 69 |
| 60 // Implementation inspired by _AsyncStarStreamController in | 70 // Implementation inspired by _AsyncStarStreamController in |
| 61 // dart-lang/sdk's runtime/lib/core_patch.dart | 71 // dart-lang/sdk's runtime/lib/core_patch.dart |
| 62 // | 72 // |
| 63 // Given input like: | 73 // Given input like: |
| 64 // | 74 // |
| 65 // foo() async* { | 75 // foo() async* { |
| 66 // yield 1; | 76 // yield 1; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } | 233 } |
| 224 '''); | 234 '''); |
| 225 | 235 |
| 226 /// Returns a Stream of T implemented by an async* function. */ | 236 /// Returns a Stream of T implemented by an async* function. */ |
| 227 /// | 237 /// |
| 228 asyncStar(gen, T, @rest args) => JS( | 238 asyncStar(gen, T, @rest args) => JS( |
| 229 '', | 239 '', |
| 230 '''(() => { | 240 '''(() => { |
| 231 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; | 241 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; |
| 232 })()'''); | 242 })()'''); |
| OLD | NEW |