| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Core Stream types | 8 // Core Stream types |
| 9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
| 10 | 10 |
| (...skipping 1320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1331 * [futureValue]. | 1331 * [futureValue]. |
| 1332 */ | 1332 */ |
| 1333 Future asFuture([var futureValue]); | 1333 Future asFuture([var futureValue]); |
| 1334 } | 1334 } |
| 1335 | 1335 |
| 1336 | 1336 |
| 1337 /** | 1337 /** |
| 1338 * An interface that abstracts creation or handling of [Stream] events. | 1338 * An interface that abstracts creation or handling of [Stream] events. |
| 1339 */ | 1339 */ |
| 1340 abstract class EventSink<T> implements Sink<T> { | 1340 abstract class EventSink<T> implements Sink<T> { |
| 1341 /** Create a data event */ | 1341 /** Send a data event to a stream. */ |
| 1342 void add(T event); | 1342 void add(T event); |
| 1343 /** Create an async error. */ | 1343 /** Send an async error to a stream. */ |
| 1344 void addError(errorEvent, [StackTrace stackTrace]); | 1344 void addError(errorEvent, [StackTrace stackTrace]); |
| 1345 /** Request a stream to close. */ | 1345 /** Send a done event to a stream.*/ |
| 1346 void close(); | 1346 void close(); |
| 1347 } | 1347 } |
| 1348 | 1348 |
| 1349 | 1349 |
| 1350 /** [Stream] wrapper that only exposes the [Stream] interface. */ | 1350 /** [Stream] wrapper that only exposes the [Stream] interface. */ |
| 1351 class StreamView<T> extends Stream<T> { | 1351 class StreamView<T> extends Stream<T> { |
| 1352 Stream<T> _stream; | 1352 Stream<T> _stream; |
| 1353 | 1353 |
| 1354 StreamView(this._stream); | 1354 StreamView(this._stream); |
| 1355 | 1355 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1380 /** | 1380 /** |
| 1381 * Consumes the elements of [stream]. | 1381 * Consumes the elements of [stream]. |
| 1382 * | 1382 * |
| 1383 * Listens on [stream] and does something for each event. | 1383 * Listens on [stream] and does something for each event. |
| 1384 * | 1384 * |
| 1385 * The consumer may stop listening after an error, or it may consume | 1385 * The consumer may stop listening after an error, or it may consume |
| 1386 * all the errors and only stop at a done event. | 1386 * all the errors and only stop at a done event. |
| 1387 */ | 1387 */ |
| 1388 Future addStream(Stream<S> stream); | 1388 Future addStream(Stream<S> stream); |
| 1389 | 1389 |
| 1390 /** |
| 1391 * Tell the consumer that no futher streams will be added. |
| 1392 * |
| 1393 * Returns a future that is completed when the consumer is done handling |
| 1394 * events. |
| 1395 */ |
| 1390 Future close(); | 1396 Future close(); |
| 1391 } | 1397 } |
| 1392 | 1398 |
| 1393 | 1399 |
| 1394 /** | 1400 /** |
| 1395 * A [StreamSink] unifies the asynchronous methods from [StreamConsumer] and | 1401 * A [StreamSink] unifies the asynchronous methods from [StreamConsumer] and |
| 1396 * the synchronous methods from [EventSink]. | 1402 * the synchronous methods from [EventSink]. |
| 1397 * | 1403 * |
| 1398 * The [EventSink] methods can't be used while the [addStream] is called. | 1404 * The [EventSink] methods can't be used while the [addStream] is called. |
| 1399 * As soon as the [addStream]'s [Future] completes with a value, the | 1405 * As soon as the [addStream]'s [Future] completes with a value, the |
| 1400 * [EventSink] methods can be used again. | 1406 * [EventSink] methods can be used again. |
| 1401 * | 1407 * |
| 1402 * If [addStream] is called after any of the [EventSink] methods, it'll | 1408 * If [addStream] is called after any of the [EventSink] methods, it'll |
| 1403 * be delayed until the underlying system has consumed the data added by the | 1409 * be delayed until the underlying system has consumed the data added by the |
| 1404 * [EventSink] methods. | 1410 * [EventSink] methods. |
| 1405 * | 1411 * |
| 1406 * When [EventSink] methods are used, the [done] [Future] can be used to | 1412 * When [EventSink] methods are used, the [done] [Future] can be used to |
| 1407 * catch any errors. | 1413 * catch any errors. |
| 1408 * | 1414 * |
| 1409 * When [close] is called, it will return the [done] [Future]. | 1415 * When [close] is called, it will return the [done] [Future]. |
| 1410 */ | 1416 */ |
| 1411 abstract class StreamSink<S> implements StreamConsumer<S>, EventSink<S> { | 1417 abstract class StreamSink<S> implements StreamConsumer<S>, EventSink<S> { |
| 1412 /** | 1418 /** |
| 1413 * Close the [StreamSink]. It'll return the [done] Future. | 1419 * As [EventSink.close], but returns a future. |
| 1420 * |
| 1421 * Returns the same future as [done]. |
| 1414 */ | 1422 */ |
| 1415 Future close(); | 1423 Future close(); |
| 1416 | 1424 |
| 1417 /** | 1425 /** |
| 1418 * The [done] Future completes with the same values as [close], except | 1426 * Return a future which is completed when the [StreamSink] is finished. |
| 1419 * for the following case: | |
| 1420 * | 1427 * |
| 1421 * * The synchronous methods of [EventSink] were called, resulting in an | 1428 * If the `StreamSink` fails with an error, |
| 1422 * error. If there is no active future (like from an addStream call), the | 1429 * perhaps in response to adding events using [add], [addError] or [close], |
| 1423 * [done] future will complete with that error | 1430 * the [done] future will complete with that error. |
| 1431 * |
| 1432 * Otherwise, the returned future will complete when either: |
| 1433 * |
| 1434 * * all events have been processed and the sink has been closed, or |
| 1435 * * the sink has otherwise been stopped from handling more events |
| 1436 * (for example by cancelling a stream subscription). |
| 1424 */ | 1437 */ |
| 1425 Future get done; | 1438 Future get done; |
| 1426 } | 1439 } |
| 1427 | 1440 |
| 1428 | 1441 |
| 1429 /** | 1442 /** |
| 1430 * The target of a [Stream.transform] call. | 1443 * The target of a [Stream.transform] call. |
| 1431 * | 1444 * |
| 1432 * The [Stream.transform] call will pass itself to this object and then return | 1445 * The [Stream.transform] call will pass itself to this object and then return |
| 1433 * the resulting stream. | 1446 * the resulting stream. |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1583 class _ControllerEventSinkWrapper<T> implements EventSink<T> { | 1596 class _ControllerEventSinkWrapper<T> implements EventSink<T> { |
| 1584 EventSink _sink; | 1597 EventSink _sink; |
| 1585 _ControllerEventSinkWrapper(this._sink); | 1598 _ControllerEventSinkWrapper(this._sink); |
| 1586 | 1599 |
| 1587 void add(T data) { _sink.add(data); } | 1600 void add(T data) { _sink.add(data); } |
| 1588 void addError(error, [StackTrace stackTrace]) { | 1601 void addError(error, [StackTrace stackTrace]) { |
| 1589 _sink.addError(error, stackTrace); | 1602 _sink.addError(error, stackTrace); |
| 1590 } | 1603 } |
| 1591 void close() { _sink.close(); } | 1604 void close() { _sink.close(); } |
| 1592 } | 1605 } |
| OLD | NEW |