| 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 |
| 11 /// stepping stone for proposed ES7 async/await, and uses ES6 Promises. | 11 /// stepping stone for proposed ES7 async/await, and uses ES6 Promises. |
| 12 part of dart._runtime; | 12 part of dart._runtime; |
| 13 | 13 |
| 14 final _jsIterator = JS('', 'Symbol("_jsIterator")'); | 14 final _jsIterator = JS('', 'Symbol("_jsIterator")'); |
| 15 final _current = JS('', 'Symbol("_current")'); | 15 final _current = JS('', 'Symbol("_current")'); |
| 16 | 16 |
| 17 syncStar(gen, E, @rest args) => JS('', '''(() => { | 17 syncStar(gen, E, @rest args) => JS( |
| 18 '', |
| 19 '''(() => { |
| 18 const SyncIterable_E = ${getGenericClass(SyncIterable)}($E); | 20 const SyncIterable_E = ${getGenericClass(SyncIterable)}($E); |
| 19 return new SyncIterable_E($gen, $args); | 21 return new SyncIterable_E($gen, $args); |
| 20 })()'''); | 22 })()'''); |
| 21 | 23 |
| 22 @JSExportName('async') | 24 @JSExportName('async') |
| 23 async_(gen, T, @rest args) => JS('', '''(() => { | 25 async_(gen, T, @rest args) => JS( |
| 26 '', |
| 27 '''(() => { |
| 24 let iter; | 28 let iter; |
| 25 function onValue(res) { | 29 function onValue(res) { |
| 26 if (res === void 0) res = null; | 30 if (res === void 0) res = null; |
| 27 return next(iter.next(res)); | 31 return next(iter.next(res)); |
| 28 } | 32 } |
| 29 function onError(err) { | 33 function onError(err) { |
| 30 // If the awaited Future throws, we want to convert this to an exception | 34 // If the awaited Future throws, we want to convert this to an exception |
| 31 // thrown from the `yield` point, as if it was thrown there. | 35 // thrown from the `yield` point, as if it was thrown there. |
| 32 // | 36 // |
| 33 // 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 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // return dart.asyncStar(function*(stream) { | 74 // return dart.asyncStar(function*(stream) { |
| 71 // if (stream.add(1)) return; | 75 // if (stream.add(1)) return; |
| 72 // yield; | 76 // yield; |
| 73 // if (stream.addStream(bar()) return; | 77 // if (stream.addStream(bar()) return; |
| 74 // yield; | 78 // yield; |
| 75 // print(yield baz()); | 79 // print(yield baz()); |
| 76 // }); | 80 // }); |
| 77 // } | 81 // } |
| 78 // | 82 // |
| 79 // TODO(ochafik): Port back to Dart (which it used to be in the past). | 83 // TODO(ochafik): Port back to Dart (which it used to be in the past). |
| 80 final _AsyncStarStreamController = JS('', ''' | 84 final _AsyncStarStreamController = JS( |
| 85 '', |
| 86 ''' |
| 81 class _AsyncStarStreamController { | 87 class _AsyncStarStreamController { |
| 82 constructor(generator, T, args) { | 88 constructor(generator, T, args) { |
| 83 this.isAdding = false; | 89 this.isAdding = false; |
| 84 this.isWaiting = false; | 90 this.isWaiting = false; |
| 85 this.isScheduled = false; | 91 this.isScheduled = false; |
| 86 this.isSuspendedAtYield = false; | 92 this.isSuspendedAtYield = false; |
| 87 this.canceler = null; | 93 this.canceler = null; |
| 88 this.iterator = generator(this, ...args)[Symbol.iterator](); | 94 this.iterator = generator(this, ...args)[Symbol.iterator](); |
| 89 this.controller = ${getGenericClass(StreamController)}(T).new({ | 95 this.controller = ${getGenericClass(StreamController)}(T).new({ |
| 90 onListen: () => this.scheduleGenerator(), | 96 onListen: () => this.scheduleGenerator(), |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 return; | 218 return; |
| 213 } | 219 } |
| 214 if (!this.controller.hasListener) return; | 220 if (!this.controller.hasListener) return; |
| 215 this.controller.addError(error, stackTrace); | 221 this.controller.addError(error, stackTrace); |
| 216 } | 222 } |
| 217 } | 223 } |
| 218 '''); | 224 '''); |
| 219 | 225 |
| 220 /// Returns a Stream of T implemented by an async* function. */ | 226 /// Returns a Stream of T implemented by an async* function. */ |
| 221 /// | 227 /// |
| 222 asyncStar(gen, T, @rest args) => JS('', '''(() => { | 228 asyncStar(gen, T, @rest args) => JS( |
| 229 '', |
| 230 '''(() => { |
| 223 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; | 231 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; |
| 224 })()'''); | 232 })()'''); |
| OLD | NEW |