| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Support for asynchronous programming, | |
| 7 * with classes such as Future and Stream. | |
| 8 * | |
| 9 * Understanding [Future]s and [Stream]s is a prerequisite for | |
| 10 * writing just about any Dart program. | |
| 11 * | |
| 12 * To use this library in your code: | |
| 13 * | |
| 14 * import 'dart:async'; | |
| 15 * | |
| 16 * ## Future | |
| 17 * | |
| 18 * A Future object represents a computation whose return value | |
| 19 * might not yet be available. | |
| 20 * The Future returns the value of the computation | |
| 21 * when it completes at some time in the future. | |
| 22 * Futures are often used for potentially lengthy computations | |
| 23 * such as I/O and interaction with users. | |
| 24 * | |
| 25 * Many methods in the Dart libraries return Futures when | |
| 26 * performing tasks. For example, when binding an HttpServer | |
| 27 * to a host and port, the `bind()` method returns a Future. | |
| 28 * | |
| 29 * HttpServer.bind('127.0.0.1', 4444) | |
| 30 * .then((server) => print('${server.isBroadcast}')) | |
| 31 * .catchError(print); | |
| 32 * | |
| 33 * [Future.then] registers a callback function that runs | |
| 34 * when the Future's operation, in this case the `bind()` method, | |
| 35 * completes successfully. | |
| 36 * The value returned by the operation | |
| 37 * is passed into the callback function. | |
| 38 * In this example, the `bind()` method returns the HttpServer | |
| 39 * object. The callback function prints one of its properties. | |
| 40 * [Future.catchError] registers a callback function that | |
| 41 * runs if an error occurs within the Future. | |
| 42 * | |
| 43 * ## Stream | |
| 44 * | |
| 45 * A Stream provides an asynchronous sequence of data. | |
| 46 * Examples of data sequences include individual events, like mouse clicks, | |
| 47 * or sequential chunks of larger data, like multiple byte lists with the | |
| 48 * contents of a file | |
| 49 * such as mouse clicks, and a stream of byte lists read from a file. | |
| 50 * The following example opens a file for reading. | |
| 51 * [Stream.listen] registers a callback function that runs | |
| 52 * each time more data is available. | |
| 53 * | |
| 54 * Stream<List<int>> stream = new File('quotes.txt').openRead(); | |
| 55 * stream.transform(UTF8.decoder).listen(print); | |
| 56 * | |
| 57 * The stream emits a sequence of a list of bytes. | |
| 58 * The program must interpret the bytes or handle the raw byte data. | |
| 59 * Here, the code uses a UTF8 decoder (provided in the `dart:convert` library) | |
| 60 * to convert the sequence of bytes into a sequence | |
| 61 * of Dart strings. | |
| 62 * | |
| 63 * Another common use of streams is for user-generated events | |
| 64 * in a web app: The following code listens for mouse clicks on a button. | |
| 65 * | |
| 66 * querySelector('#myButton').onClick.listen((_) => print('Click.')); | |
| 67 * | |
| 68 * ## Other resources | |
| 69 * | |
| 70 * * The [dart:async section of the library tour] | |
| 71 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-as
ynchronous-programming): | |
| 72 * A brief overview of asynchronous programming. | |
| 73 * | |
| 74 * * [Use Future-Based APIs] | |
| 75 * (https://www.dartlang.org/docs/tutorials/futures/): A closer look at | |
| 76 * Futures and how to use them to write asynchronous Dart code. | |
| 77 * | |
| 78 * * [Futures and Error Handling] | |
| 79 * (https://www.dartlang.org/articles/futures-and-error-handling/): Everything | |
| 80 * you wanted to know about handling errors and exceptions when working with | |
| 81 * Futures (but were afraid to ask). | |
| 82 * | |
| 83 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | |
| 84 * Learn how Dart handles the event queue and microtask queue, so you can write | |
| 85 * better asynchronous code with fewer surprises. | |
| 86 * | |
| 87 * * [Asynchronous Unit Testing with Dart] | |
| 88 * (https://www.dartlang.org/articles/dart-unit-tests/#asynchronous-tests): How | |
| 89 * to test asynchronous code. | |
| 90 */ | |
| 91 library dart.async; | |
| 92 | |
| 93 import "dart:collection"; | |
| 94 import "dart:_internal" show deprecated, printToZone, printToConsole, | |
| 95 IterableElementError; | |
| 96 import 'dart:_js_helper' show | |
| 97 patch, | |
| 98 Primitives, | |
| 99 convertDartClosureToJS, | |
| 100 requiresPreamble; | |
| 101 import 'dart:_isolate_helper' show | |
| 102 IsolateNatives, | |
| 103 TimerImpl, | |
| 104 leaveJsAsync, | |
| 105 enterJsAsync, | |
| 106 isWorker; | |
| 107 import 'dart:_foreign_helper' show JS; | |
| 108 | |
| 109 part 'async_error.dart'; | |
| 110 part 'broadcast_stream_controller.dart'; | |
| 111 part 'deferred_load.dart'; | |
| 112 part 'future.dart'; | |
| 113 part 'future_impl.dart'; | |
| 114 part 'schedule_microtask.dart'; | |
| 115 part 'stream.dart'; | |
| 116 part 'stream_controller.dart'; | |
| 117 part 'stream_impl.dart'; | |
| 118 part 'stream_pipe.dart'; | |
| 119 part 'stream_transformers.dart'; | |
| 120 part 'timer.dart'; | |
| 121 part 'zone.dart'; | |
| 122 | |
| 123 bool get _hasDocument => JS('String', 'typeof document') == 'object'; | |
| OLD | NEW |