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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 if (this.isSuspendedAtYield || this.isAdding) return; | 178 if (this.isSuspendedAtYield || this.isAdding) return; |
179 | 179 |
180 // Handle `await`: if we get a value passed to `yield` it means we are | 180 // Handle `await`: if we get a value passed to `yield` it means we are |
181 // waiting on this Future. Make sure to prevent scheduling, and pass the | 181 // waiting on this Future. Make sure to prevent scheduling, and pass the |
182 // value back as the result of the `yield`. | 182 // value back as the result of the `yield`. |
183 // | 183 // |
184 // TODO(jmesserly): is the timing here correct? The assumption here is | 184 // TODO(jmesserly): is the timing here correct? The assumption here is |
185 // that we should schedule `await` in `async*` the same as in `async`. | 185 // that we should schedule `await` in `async*` the same as in `async`. |
186 this.isWaiting = true; | 186 this.isWaiting = true; |
187 let future = iter.value; | 187 let future = iter.value; |
188 if (!$instanceOf(future, ${getGenericClass(Future)})) { | 188 // TODO(jmesserly): `async` uses a different check that looks for the |
| 189 // (private) implementation type of `Future`, rather than the public type. |
| 190 if (!$Future.is(future)) { |
189 future = $Future.value(future); | 191 future = $Future.value(future); |
190 } | 192 } |
191 return future.then($dynamic)((x) => this.runBody(x), | 193 return future.then($dynamic)((x) => this.runBody(x), |
192 { onError: (e, s) => this.throwError(e, s) }); | 194 { onError: (e, s) => this.throwError(e, s) }); |
193 } | 195 } |
194 | 196 |
195 // Adds element to stream, returns true if the caller should terminate | 197 // Adds element to stream, returns true if the caller should terminate |
196 // execution of the generator. | 198 // execution of the generator. |
197 add(event) { | 199 add(event) { |
198 // If stream is cancelled, tell caller to exit the async generator. | 200 // If stream is cancelled, tell caller to exit the async generator. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 } | 239 } |
238 if (!this.controller.hasListener) return; | 240 if (!this.controller.hasListener) return; |
239 this.controller.addError(error, stackTrace); | 241 this.controller.addError(error, stackTrace); |
240 } | 242 } |
241 } | 243 } |
242 '''); | 244 '''); |
243 | 245 |
244 /// Returns a Stream of T implemented by an async* function. | 246 /// Returns a Stream of T implemented by an async* function. |
245 asyncStar(gen, T, @rest args) => JS('', 'new #(#, #, #).controller.stream', | 247 asyncStar(gen, T, @rest args) => JS('', 'new #(#, #, #).controller.stream', |
246 _AsyncStarStreamController, gen, T, args); | 248 _AsyncStarStreamController, gen, T, args); |
OLD | NEW |