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

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

Issue 1216123004: Update documentation of StreamIterator, improve tests. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comment. Created 5 years, 5 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 | « no previous file | tests/lib/async/stream_iterator_test.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 1655 matching lines...) Expand 10 before | Expand all | Expand 10 after
1666 /** 1666 /**
1667 * An [Iterable] like interface for the values of a [Stream]. 1667 * An [Iterable] like interface for the values of a [Stream].
1668 * 1668 *
1669 * This wraps a [Stream] and a subscription on the stream. It listens 1669 * This wraps a [Stream] and a subscription on the stream. It listens
1670 * on the stream, and completes the future returned by [moveNext] when the 1670 * on the stream, and completes the future returned by [moveNext] when the
1671 * next value becomes available. 1671 * next value becomes available.
1672 */ 1672 */
1673 abstract class StreamIterator<T> { 1673 abstract class StreamIterator<T> {
1674 1674
1675 /** Create a [StreamIterator] on [stream]. */ 1675 /** Create a [StreamIterator] on [stream]. */
1676 factory StreamIterator(Stream<T> stream) 1676 factory StreamIterator(Stream<T> stream) = _StreamIteratorImpl<T>;
1677 // TODO(lrn): use redirecting factory constructor when type
1678 // arguments are supported.
1679 => new _StreamIteratorImpl<T>(stream);
1680 1677
1681 /** 1678 /**
1682 * Wait for the next stream value to be available. 1679 * Wait for the next stream value to be available.
1683 * 1680 *
1684 * It is not allowed to call this function again until the future has 1681 * Returns a future which will complete with either `true` or `false`.
1685 * completed. If the returned future completes with anything except `true`, 1682 * Completing with `true` means that another event has been received and
1686 * the iterator is done, and no new value will ever be available. 1683 * can be read as [current].
1684 * Completing with `false` means that the stream itearation is done and
1685 * no further events will ever be available.
1686 * The future may complete with an error, if the stream produces an error,
1687 * which also ends iteration.
1687 * 1688 *
1688 * The future may complete with an error, if the stream produces an error. 1689 * The function must not be called again until the future returned by a
1690 * previous call is completed.
1689 */ 1691 */
1690 Future<bool> moveNext(); 1692 Future<bool> moveNext();
1691 1693
1692 /** 1694 /**
1693 * The current value of the stream. 1695 * The current value of the stream.
1694 * 1696 *
1695 * Only valid when the future returned by [moveNext] completes with `true` 1697 * Is `null` before the first call to [moveNext] and after a call to
1696 * as value, and only until the next call to [moveNext]. 1698 * `moveNext` completes with a `false` result or an error.
1699 *
1700 * When a `moveNext` call completes with `true`, the `current` field holds
1701 * the most recent event of the stream, and it stays like that until the next
1702 * call to `moveNext`.
1703 * Between a call to `moveNext` and when its returned future completes,
1704 * the value is unspecified.
1697 */ 1705 */
1698 T get current; 1706 T get current;
1699 1707
1700 /** 1708 /**
1701 * Cancels the stream iterator (and the underlying stream subscription) early. 1709 * Cancels the stream iterator (and the underlying stream subscription) early.
1702 * 1710 *
1703 * The stream iterator is automatically canceled if the [moveNext] future 1711 * The stream iterator is automatically canceled if the [moveNext] future
1704 * completes with either `false` or an error. 1712 * completes with either `false` or an error.
1705 * 1713 *
1706 * If a [moveNext] call has been made, it will complete with `false` as value,
1707 * as will all further calls to [moveNext].
1708 *
1709 * If you need to stop listening for values before the stream iterator is 1714 * If you need to stop listening for values before the stream iterator is
1710 * automatically closed, you must call [cancel] to ensure that the stream 1715 * automatically closed, you must call [cancel] to ensure that the stream
1711 * is properly closed. 1716 * is properly closed.
1712 * 1717 *
1718 * If [moveNext] has been called when the iterator is cancelled,
1719 * its returned future will complete with `false` as value,
1720 * as will all further calls to [moveNext].
1721 *
1713 * Returns a future if the cancel-operation is not completed synchronously. 1722 * Returns a future if the cancel-operation is not completed synchronously.
1714 * Otherwise returns `null`. 1723 * Otherwise returns `null`.
1715 */ 1724 */
1716 Future cancel(); 1725 Future cancel();
1717 } 1726 }
1718 1727
1719 1728
1720 /** 1729 /**
1721 * Wraps an [_EventSink] so it exposes only the [EventSink] interface. 1730 * Wraps an [_EventSink] so it exposes only the [EventSink] interface.
1722 */ 1731 */
1723 class _ControllerEventSinkWrapper<T> implements EventSink<T> { 1732 class _ControllerEventSinkWrapper<T> implements EventSink<T> {
1724 EventSink _sink; 1733 EventSink _sink;
1725 _ControllerEventSinkWrapper(this._sink); 1734 _ControllerEventSinkWrapper(this._sink);
1726 1735
1727 void add(T data) { _sink.add(data); } 1736 void add(T data) { _sink.add(data); }
1728 void addError(error, [StackTrace stackTrace]) { 1737 void addError(error, [StackTrace stackTrace]) {
1729 _sink.addError(error, stackTrace); 1738 _sink.addError(error, stackTrace);
1730 } 1739 }
1731 void close() { _sink.close(); } 1740 void close() { _sink.close(); }
1732 } 1741 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/async/stream_iterator_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698