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 29 matching lines...) Expand all Loading... |
40 function next(ret) { | 40 function next(ret) { |
41 if (ret.done) return ret.value; | 41 if (ret.done) return ret.value; |
42 // Checks if the awaited value is a Future. | 42 // Checks if the awaited value is a Future. |
43 let future = ret.value; | 43 let future = ret.value; |
44 if (!$instanceOf(future, ${getGenericClass(Future)})) { | 44 if (!$instanceOf(future, ${getGenericClass(Future)})) { |
45 future = $Future.value(future); | 45 future = $Future.value(future); |
46 } | 46 } |
47 // Chain the Future so `await` receives the Future's value. | 47 // Chain the Future so `await` receives the Future's value. |
48 return future.then($dynamic)(onValue, {onError: onError}); | 48 return future.then($dynamic)(onValue, {onError: onError}); |
49 } | 49 } |
50 return ${getGenericClass(Future)}($T).new(function() { | 50 return ${getGenericClass(Future)}($T).microtask(function() { |
51 iter = $gen.apply(null, $args)[Symbol.iterator](); | 51 iter = $gen.apply(null, $args)[Symbol.iterator](); |
52 return onValue(); | 52 return onValue(); |
53 }); | 53 }); |
54 })()'''); | 54 })()'''); |
55 | 55 |
56 // Implementation inspired by _AsyncStarStreamController in | 56 // Implementation inspired by _AsyncStarStreamController in |
57 // dart-lang/sdk's runtime/lib/core_patch.dart | 57 // dart-lang/sdk's runtime/lib/core_patch.dart |
58 // | 58 // |
59 // Given input like: | 59 // Given input like: |
60 // | 60 // |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 this.controller.addError(error, stackTrace); | 215 this.controller.addError(error, stackTrace); |
216 } | 216 } |
217 } | 217 } |
218 '''); | 218 '''); |
219 | 219 |
220 /// Returns a Stream of T implemented by an async* function. */ | 220 /// Returns a Stream of T implemented by an async* function. */ |
221 /// | 221 /// |
222 asyncStar(gen, T, @rest args) => JS('', '''(() => { | 222 asyncStar(gen, T, @rest args) => JS('', '''(() => { |
223 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; | 223 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; |
224 })()'''); | 224 })()'''); |
OLD | NEW |