Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: sdk/lib/async/stream.dart

Issue 1177343004: Add Stream.empty. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Remove the null future "optimization". Fix some missing type parameters. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/async/future_impl.dart ('k') | sdk/lib/async/stream_controller.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 * their input to pause too. 68 * their input to pause too.
69 * 69 *
70 * The default implementation of [isBroadcast] returns false. 70 * The default implementation of [isBroadcast] returns false.
71 * A broadcast stream inheriting from [Stream] must override [isBroadcast] 71 * A broadcast stream inheriting from [Stream] must override [isBroadcast]
72 * to return `true`. 72 * to return `true`.
73 */ 73 */
74 abstract class Stream<T> { 74 abstract class Stream<T> {
75 Stream(); 75 Stream();
76 76
77 /** 77 /**
78 * Internal use only. We do not want to promise that Stream stays const.
79 *
80 * If mixins become compatible with const constructors, we may use a
81 * stream mixin instead of extending Stream from a const class.
82 */
83 const Stream._internal();
84
85 /**
86 * Creates an empty broadcast stream.
87 *
88 * This is a stream which does nothing except sending a done event
89 * when it's listened to.
90 */
91 const factory Stream.empty() = _EmptyStream<T>;
92
93 /**
78 * Creates a new single-subscription stream from the future. 94 * Creates a new single-subscription stream from the future.
79 * 95 *
80 * When the future completes, the stream will fire one event, either 96 * When the future completes, the stream will fire one event, either
81 * data or error, and then close with a done-event. 97 * data or error, and then close with a done-event.
82 */ 98 */
83 factory Stream.fromFuture(Future<T> future) { 99 factory Stream.fromFuture(Future<T> future) {
84 // Use the controller's buffering to fill in the value even before 100 // Use the controller's buffering to fill in the value even before
85 // the stream has a listener. For a single value, it's not worth it 101 // the stream has a listener. For a single value, it's not worth it
86 // to wait for a listener before doing the `then` on the future. 102 // to wait for a listener before doing the `then` on the future.
87 _StreamController<T> controller = new StreamController<T>(sync: true); 103 _StreamController<T> controller = new StreamController<T>(sync: true);
(...skipping 1328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 { Function onError, 1432 { Function onError,
1417 void onDone(), 1433 void onDone(),
1418 bool cancelOnError }) { 1434 bool cancelOnError }) {
1419 return _stream.listen(onData, onError: onError, onDone: onDone, 1435 return _stream.listen(onData, onError: onError, onDone: onDone,
1420 cancelOnError: cancelOnError); 1436 cancelOnError: cancelOnError);
1421 } 1437 }
1422 } 1438 }
1423 1439
1424 1440
1425 /** 1441 /**
1426 * The target of a [Stream.pipe] call. 1442 * Abstract interface for a "sink" accepting multiple entire streams.
1427 * 1443 *
1428 * The [Stream.pipe] call will pass itself to this object, and then return 1444 * A consumer can accept a number of consequtive streams using [addStream],
1429 * the resulting [Future]. The pipe should complete the future when it's 1445 * and when no further data need to be added, the [close] method tells the
1430 * done. 1446 * consumer to complete its work and shut down.
1447 *
1448 * This class is not just a [Sink<Stream>] because it is also combined with
1449 * other [Sink] classes, like it's combined with [EventSink] in the
1450 * [StreamSink] class.
1451 *
1452 * The [Stream.pipe] accepts a `StreamConsumer` and will pass the stream
1453 * to the consumer's [addStream] method. When that completes, it will
1454 * call [close] and then complete its own returned future.
1431 */ 1455 */
1432 abstract class StreamConsumer<S> { 1456 abstract class StreamConsumer<S> {
1433 /** 1457 /**
1434 * Consumes the elements of [stream]. 1458 * Consumes the elements of [stream].
1435 * 1459 *
1436 * Listens on [stream] and does something for each event. 1460 * Listens on [stream] and does something for each event.
1437 * 1461 *
1438 * The consumer may stop listening after an error, or it may consume 1462 * Returns a future which is completed when the stream is done being added,
1439 * all the errors and only stop at a done event. 1463 * and the consumer is ready to accept a new stream.
1464 * No further calls to [addStream] or [close] should happen before the
1465 * returned future has completed.
1466 *
1467 * The consumer may stop listening to the stream after an error,
1468 * it may consume all the errors and only stop at a done event,
1469 * or it may be canceled early if the receiver don't want any further events.
1470 *
1471 * If the consumer stops listening because of some error preventing it
1472 * from continuing, it may report this error in the returned future,
1473 * otherwise it will just complete the future with `null`.
1440 */ 1474 */
1441 Future addStream(Stream<S> stream); 1475 Future addStream(Stream<S> stream);
1442 1476
1443 /** 1477 /**
1444 * Tell the consumer that no futher streams will be added. 1478 * Tells the consumer that no futher streams will be added.
1445 * 1479 *
1446 * Returns a future that is completed when the consumer is done handling 1480 * This allows the consumer to complete any remaining work and release
1447 * events. 1481 * resources that are no longer needed
1482 *
1483 * Returns a future which is completed when the consumer has shut down.
1484 * If cleaning up can fail, the error may be reported in the returned future,
1485 * otherwise it completes with `null`.
1448 */ 1486 */
1449 Future close(); 1487 Future close();
1450 } 1488 }
1451 1489
1452 1490
1453 /** 1491 /**
1492 * A object that accepts stream events both synchronously and asynchronously.
1493 *
1454 * A [StreamSink] unifies the asynchronous methods from [StreamConsumer] and 1494 * A [StreamSink] unifies the asynchronous methods from [StreamConsumer] and
1455 * the synchronous methods from [EventSink]. 1495 * the synchronous methods from [EventSink].
1456 * 1496 *
1457 * The [EventSink] methods can't be used while the [addStream] is called. 1497 * The [EventSink] methods can't be used while the [addStream] is called.
1458 * As soon as the [addStream]'s [Future] completes with a value, the 1498 * As soon as the [addStream]'s [Future] completes with a value, the
1459 * [EventSink] methods can be used again. 1499 * [EventSink] methods can be used again.
1460 * 1500 *
1461 * If [addStream] is called after any of the [EventSink] methods, it'll 1501 * If [addStream] is called after any of the [EventSink] methods, it'll
1462 * be delayed until the underlying system has consumed the data added by the 1502 * be delayed until the underlying system has consumed the data added by the
1463 * [EventSink] methods. 1503 * [EventSink] methods.
1464 * 1504 *
1465 * When [EventSink] methods are used, the [done] [Future] can be used to 1505 * When [EventSink] methods are used, the [done] [Future] can be used to
1466 * catch any errors. 1506 * catch any errors.
1467 * 1507 *
1468 * When [close] is called, it will return the [done] [Future]. 1508 * When [close] is called, it will return the [done] [Future].
1469 */ 1509 */
1470 abstract class StreamSink<S> implements StreamConsumer<S>, EventSink<S> { 1510 abstract class StreamSink<S> implements EventSink<S>, StreamConsumer<S> {
1471 /** 1511 /**
1472 * As [EventSink.close], but returns a future. 1512 * Tells the stream sink that no futher streams will be added.
1513 *
1514 * This allows the stream sink to complete any remaining work and release
1515 * resources that are no longer needed
1516 *
1517 * Returns a future which is completed when the stream sink has shut down.
1518 * If cleaning up can fail, the error may be reported in the returned future,
1519 * otherwise it completes with `null`.
1473 * 1520 *
1474 * Returns the same future as [done]. 1521 * Returns the same future as [done].
1522 *
1523 * The stream sink may close before the [close] method is called, either due
1524 * to an error or because it is itself provding events to someone who has
1525 * stopped listening. In that case, the [done] future is completed first,
1526 * and the `close` method will return the `done` future when called.
1527 *
1528 * Unifies [StreamConsumer.close] and [EventSink.close] which both mark their
1529 * object as not expecting any further events.
1475 */ 1530 */
1476 Future close(); 1531 Future close();
1477 1532
1478 /** 1533 /**
1479 * Return a future which is completed when the [StreamSink] is finished. 1534 * Return a future which is completed when the [StreamSink] is finished.
1480 * 1535 *
1481 * If the `StreamSink` fails with an error, 1536 * If the `StreamSink` fails with an error,
1482 * perhaps in response to adding events using [add], [addError] or [close], 1537 * perhaps in response to adding events using [add], [addError] or [close],
1483 * the [done] future will complete with that error. 1538 * the [done] future will complete with that error.
1484 * 1539 *
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
1658 class _ControllerEventSinkWrapper<T> implements EventSink<T> { 1713 class _ControllerEventSinkWrapper<T> implements EventSink<T> {
1659 EventSink _sink; 1714 EventSink _sink;
1660 _ControllerEventSinkWrapper(this._sink); 1715 _ControllerEventSinkWrapper(this._sink);
1661 1716
1662 void add(T data) { _sink.add(data); } 1717 void add(T data) { _sink.add(data); }
1663 void addError(error, [StackTrace stackTrace]) { 1718 void addError(error, [StackTrace stackTrace]) {
1664 _sink.addError(error, stackTrace); 1719 _sink.addError(error, stackTrace);
1665 } 1720 }
1666 void close() { _sink.close(); } 1721 void close() { _sink.close(); }
1667 } 1722 }
OLDNEW
« no previous file with comments | « sdk/lib/async/future_impl.dart ('k') | sdk/lib/async/stream_controller.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698