OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 /** | 7 /** |
8 * A [Future] represents a delayed computation. It is used to obtain a not-yet | 8 * A [Future] represents a delayed computation. It is used to obtain a not-yet |
9 * available value, or error, sometime in the future. Receivers of a | 9 * available value, or error, sometime in the future. Receivers of a |
10 * [Future] can register callbacks that handle the value or error once it is | 10 * [Future] can register callbacks that handle the value or error once it is |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 Future<T> whenComplete(action()); | 315 Future<T> whenComplete(action()); |
316 | 316 |
317 /** | 317 /** |
318 * Creates a [Stream] that sends [this]' completion value, data or error, to | 318 * Creates a [Stream] that sends [this]' completion value, data or error, to |
319 * its subscribers. The stream closes after the completion value. | 319 * its subscribers. The stream closes after the completion value. |
320 */ | 320 */ |
321 Stream<T> asStream(); | 321 Stream<T> asStream(); |
322 } | 322 } |
323 | 323 |
324 /** | 324 /** |
325 * A [Completer] is used to produce [Future]s and supply their value when it | 325 * A [Completer] is used to produce [Future]s and to complete those futures |
326 * becomes available. | 326 * with a value or error at a later time. |
327 * | 327 * |
328 * A service that provides values to callers, and wants to return [Future]s can | 328 * A class that wants to return [Future]s can use a [Completer] as follows: |
329 * use a [Completer] as follows: | |
330 * | 329 * |
331 * Completer completer = new Completer(); | 330 * Class myAsyncOperation { |
332 * // send future object back to client... | 331 * Completer _completer = new Completer(); |
333 * return completer.future; | |
334 * ... | |
335 * | 332 * |
336 * // later when value is available, call: | 333 * Future<T> myOp() { |
337 * completer.complete(value); | 334 * _startOperation(); |
| 335 * // send future object back to client... |
| 336 * return _completer.future; |
| 337 * } |
338 * | 338 * |
339 * // alternatively, if the service cannot produce the value, it | 339 * // Something calls this when the value is ready. |
340 * // can provide an error: | 340 * _finishOperation(T result) { |
341 * completer.completeError(error); | 341 * _completer.complete(value); |
| 342 * } |
| 343 * |
| 344 * // If something goes wrong, call this. |
| 345 * _errorHappened(error) { |
| 346 * _completer.completeError(error); |
| 347 * } |
| 348 * } |
342 * | 349 * |
343 */ | 350 */ |
344 abstract class Completer<T> { | 351 abstract class Completer<T> { |
345 | 352 |
| 353 /** |
| 354 * Creates a completer whose future is completed asynchronously, sometime |
| 355 * after [complete] is called on it. This allows a call to [complete] to |
| 356 * be in the middle of other code, without running an unknown amount of |
| 357 * future completion and [then] callbacks synchronously at the point that |
| 358 * [complete] is called. |
| 359 * |
| 360 * Example: |
| 361 * |
| 362 * var completer = new Completer.sync(); |
| 363 * completer.future.then((_) { bar(); }); |
| 364 * // The completion is the result of the asynchronous onDone event. |
| 365 * // However, there is code executed after the call to complete, |
| 366 * // but before completer.future runs its completion callback. |
| 367 * stream.listen(print, onDone: () { |
| 368 * completer.complete("done"); |
| 369 * foo(); // In this case, foo() runs before bar(). |
| 370 * }); |
| 371 */ |
346 factory Completer() => new _AsyncCompleter<T>(); | 372 factory Completer() => new _AsyncCompleter<T>(); |
347 | 373 |
348 /** | 374 /** |
349 * Completes the future synchronously. | 375 * Completes the future synchronously. |
350 * | 376 * |
351 * This constructor should be avoided unless the completion of the future is | 377 * This constructor should be avoided unless the completion of the future is |
352 * known to be the final result of another asynchronous operation. If in doubt | 378 * known to be the final result of another asynchronous operation. If in doubt |
353 * use the default [Completer] constructor. | 379 * use the default [Completer] constructor. |
354 * | 380 * |
355 * Example: | 381 * Example: |
356 * | 382 * |
357 * var completer = new Completer.sync(); | 383 * var completer = new Completer.sync(); |
358 * // The completion is the result of the asynchronous onDone event. | 384 * // The completion is the result of the asynchronous onDone event. |
359 * // No other operation is performed after the completion. It is safe | 385 * // No other operation is performed after the completion. It is safe |
360 * // to use the Completer.sync constructor. | 386 * // to use the Completer.sync constructor. |
361 * stream.listen(print, onDone: () { completer.complete("done"); }); | 387 * stream.listen(print, onDone: () { completer.complete("done"); }); |
362 * | 388 * |
363 * Bad example. Do not use this code. Only for illustrative purposes: | 389 * Bad example. Do not use this code. Only for illustrative purposes: |
364 * | 390 * |
365 * var completer = new Completer.sync(); | 391 * var completer = new Completer.sync(); |
| 392 * completer.future.then((_) { bar(); }); |
366 * // The completion is the result of the asynchronous onDone event. | 393 * // The completion is the result of the asynchronous onDone event. |
367 * // However, there is still code executed after the completion. This | 394 * // However, there is still code executed after the completion. This |
368 * // operation is *not* safe. | 395 * // operation is *not* safe. |
369 * stream.listen(print, onDone: () { | 396 * stream.listen(print, onDone: () { |
370 * completer.complete("done"); | 397 * completer.complete("done"); |
371 * foo(); // This operation follows the completion. | 398 * foo(); // In this case, foo() runs after bar(). |
372 * }); | 399 * }); |
373 * | |
374 * *WARNING* This constructor is experimental and could disappear or change | |
375 * behavior. | |
376 */ | 400 */ |
377 factory Completer.sync() => new _SyncCompleter<T>(); | 401 factory Completer.sync() => new _SyncCompleter<T>(); |
378 | 402 |
379 /** The future that will contain the result provided to this completer. */ | 403 /** The future that will contain the result provided to this completer. */ |
380 Future get future; | 404 Future get future; |
381 | 405 |
382 /** | 406 /** |
383 * Completes [future] with the supplied values. | 407 * Completes [future] with the supplied values. |
384 * | 408 * |
385 * All listeners on the future will be immediately informed about the value. | 409 * All listeners on the future will be immediately informed about the value. |
386 */ | 410 */ |
387 void complete([T value]); | 411 void complete([T value]); |
388 | 412 |
389 /** | 413 /** |
390 * Complete [future] with an error. | 414 * Complete [future] with an error. |
391 * | 415 * |
392 * Completing a future with an error indicates that an exception was thrown | 416 * Completing a future with an error indicates that an exception was thrown |
393 * while trying to produce a value. | 417 * while trying to produce a value. |
394 * | 418 * |
395 * The argument [exception] should not be `null`. | 419 * The argument [exception] should not be `null`. |
396 */ | 420 */ |
397 void completeError(Object exception, [Object stackTrace]); | 421 void completeError(Object exception, [Object stackTrace]); |
398 | 422 |
399 /** | 423 /** |
400 * Whether the future has been completed. | 424 * Whether the future has been completed. |
401 */ | 425 */ |
402 bool get isCompleted; | 426 bool get isCompleted; |
403 } | 427 } |
OLD | NEW |