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 "async.dart"; | 5 part of "async.dart"; |
6 | 6 |
7 // ------------------------------------------------------------------- | 7 // ------------------------------------------------------------------- |
8 // Core Stream types | 8 // Core Stream types |
9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
10 | 10 |
(...skipping 969 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 * | 980 * |
981 * In case of a `done` event the future completes with the given | 981 * In case of a `done` event the future completes with the given |
982 * [futureValue]. | 982 * [futureValue]. |
983 */ | 983 */ |
984 Future<E> drain<E>([E futureValue]) => | 984 Future<E> drain<E>([E futureValue]) => |
985 listen(null, cancelOnError: true).asFuture<E>(futureValue); | 985 listen(null, cancelOnError: true).asFuture<E>(futureValue); |
986 | 986 |
987 /** | 987 /** |
988 * Provides at most the first [count] data events of this stream. | 988 * Provides at most the first [count] data events of this stream. |
989 * | 989 * |
990 * Forwards all events of this stream to the returned stream | 990 * Returns a stream that emits the same events that this stream would |
991 * until [count] data events have been forwarded or this stream ends, | 991 * if listened to at the same time, |
992 * then ends the returned stream with a done event. | 992 * until either this stream ends or it has emitted [count] data events, |
| 993 * at which point the returned stream is done. |
993 * | 994 * |
994 * If this stream produces fewer than [count] data events before it's done, | 995 * If this stream produces fewer than [count] data events before it's done, |
995 * so will the returned stream. | 996 * so will the returned stream. |
996 * | 997 * |
997 * Starts listening to this stream when the returned stream is listened to | 998 * Starts listening to this stream when the returned stream is listened to |
998 * and stops listening when the first [count] data events have been received. | 999 * and stops listening when the first [count] data events have been received. |
999 * | 1000 * |
1000 * This means that if this is a single-subscription (non-broadcast) streams | 1001 * This means that if this is a single-subscription (non-broadcast) streams |
1001 * it cannot be reused after the returned stream has been listened to. | 1002 * it cannot be reused after the returned stream has been listened to. |
1002 * | 1003 * |
1003 * If this is a broadcast stream, the returned stream is a broadcast stream. | 1004 * If this is a broadcast stream, the returned stream is a broadcast stream. |
1004 * In that case, the events are only counted from the time | 1005 * In that case, the events are only counted from the time |
1005 * the returned stream is listened to. | 1006 * the returned stream is listened to. |
1006 */ | 1007 */ |
1007 Stream<T> take(int count) { | 1008 Stream<T> take(int count) { |
1008 return new _TakeStream<T>(this, count); | 1009 return new _TakeStream<T>(this, count); |
1009 } | 1010 } |
1010 | 1011 |
1011 /** | 1012 /** |
1012 * Forwards data events while [test] is successful. | 1013 * Forwards data events while [test] is successful. |
1013 * | 1014 * |
1014 * The returned stream provides the same events as this stream as long | 1015 * Returns a stream that provides the same events as this stream |
1015 * as [test] returns `true` for the event data. The stream is done | 1016 * until [test] fails for a data event. |
1016 * when either this stream is done, or when this stream first provides | 1017 * The returned stream is done when either this stream is done, |
1017 * a value that [test] doesn't accept. | 1018 * or when this stream first emits a data event that fails [test]. |
1018 * | 1019 * |
1019 * Stops listening to the stream after the accepted elements. | 1020 * The `test` call is considered failing if it returns a non-`true` value |
| 1021 * or if it throws. If the `test` call throws, the error is emitted as the |
| 1022 * last event on the returned streams. |
| 1023 * |
| 1024 * Stops listening to this stream after the accepted elements. |
1020 * | 1025 * |
1021 * Internally the method cancels its subscription after these elements. This | 1026 * Internally the method cancels its subscription after these elements. This |
1022 * means that single-subscription (non-broadcast) streams are closed and | 1027 * means that single-subscription (non-broadcast) streams are closed and |
1023 * cannot be reused after a call to this method. | 1028 * cannot be reused after a call to this method. |
1024 * | 1029 * |
1025 * The returned stream is a broadcast stream if this stream is. | 1030 * The returned stream is a broadcast stream if this stream is. |
1026 * For a broadcast stream, the events are only tested from the time | 1031 * For a broadcast stream, the events are only tested from the time |
1027 * the returned stream is listened to. | 1032 * the returned stream is listened to. |
1028 */ | 1033 */ |
1029 Stream<T> takeWhile(bool test(T element)) { | 1034 Stream<T> takeWhile(bool test(T element)) { |
1030 return new _TakeWhileStream<T>(this, test); | 1035 return new _TakeWhileStream<T>(this, test); |
1031 } | 1036 } |
1032 | 1037 |
1033 /** | 1038 /** |
1034 * Skips the first [count] data events from this stream. | 1039 * Skips the first [count] data events from this stream. |
1035 * | 1040 * |
| 1041 * Returns a stream that emits the same events as this stream would |
| 1042 * if listened to at the same time, except that the first [count] |
| 1043 * data events are not emitted. |
| 1044 * The returned stream is done when this stream is. |
| 1045 * |
| 1046 * If this stream emits fewer than [count] data events |
| 1047 * before being done, the returned stream emits no data events. |
| 1048 * |
1036 * The returned stream is a broadcast stream if this stream is. | 1049 * The returned stream is a broadcast stream if this stream is. |
1037 * For a broadcast stream, the events are only counted from the time | 1050 * For a broadcast stream, the events are only counted from the time |
1038 * the returned stream is listened to. | 1051 * the returned stream is listened to. |
1039 */ | 1052 */ |
1040 Stream<T> skip(int count) { | 1053 Stream<T> skip(int count) { |
1041 return new _SkipStream<T>(this, count); | 1054 return new _SkipStream<T>(this, count); |
1042 } | 1055 } |
1043 | 1056 |
1044 /** | 1057 /** |
1045 * Skip data events from this stream while they are matched by [test]. | 1058 * Skip data events from this stream while they are matched by [test]. |
1046 * | 1059 * |
| 1060 * Returns a stream that emits the same events as this stream, |
| 1061 * except that data events are not emitted until a data event fails `test`. |
| 1062 * The test fails when called with a data event |
| 1063 * if it returns a non-`true` value or if the call to `test` throws. |
| 1064 * If the call throws, the error is emitted as an error event |
| 1065 * on the returned stream instead of the data event, |
| 1066 * otherwise the event that made `test` return non-true is emitted as the |
| 1067 * first data event. |
| 1068 * |
1047 * Error and done events are provided by the returned stream unmodified. | 1069 * Error and done events are provided by the returned stream unmodified. |
1048 * | 1070 * |
1049 * Starting with the first data event where [test] returns false for the | |
1050 * event data, the returned stream will have the same events as this stream. | |
1051 * | |
1052 * The returned stream is a broadcast stream if this stream is. | 1071 * The returned stream is a broadcast stream if this stream is. |
1053 * For a broadcast stream, the events are only tested from the time | 1072 * For a broadcast stream, the events are only tested from the time |
1054 * the returned stream is listened to. | 1073 * the returned stream is listened to. |
1055 */ | 1074 */ |
1056 Stream<T> skipWhile(bool test(T element)) { | 1075 Stream<T> skipWhile(bool test(T element)) { |
1057 return new _SkipWhileStream<T>(this, test); | 1076 return new _SkipWhileStream<T>(this, test); |
1058 } | 1077 } |
1059 | 1078 |
1060 /** | 1079 /** |
1061 * Skips data events if they are equal to the previous data event. | 1080 * Skips data events if they are equal to the previous data event. |
1062 * | 1081 * |
1063 * The returned stream provides the same events as this stream, except | 1082 * The returned stream provides the same events as this stream, except |
1064 * that it never provides two consecutive data events that are equal. | 1083 * that it never provides two consecutive data events that are equal. |
1065 * That is, errors are passed through to the returned stream, and | 1084 * That is, errors are passed through to the returned stream, and |
1066 * data events are passed through if they are distinct from the most | 1085 * data events are passed through if they are distinct from the most |
1067 * recently emitted data event. | 1086 * recently emitted data event. |
1068 * | 1087 * |
1069 * Equality is determined by the provided [equals] method. If that is | 1088 * Equality is determined by the provided [equals] method. If that is |
1070 * omitted, the '==' operator on the last provided data element is used. | 1089 * omitted, the '==' operator on the last provided data element is used. |
1071 * | 1090 * |
1072 * If [equals] throws, the data event is replaced by an error event | 1091 * If [equals] throws, the data event is replaced by an error event |
1073 * containing the thrown error. The behavior is equivalent to the | 1092 * containing the thrown error. The behavior is equivalent to the |
1074 * original stream emitting the error event. | 1093 * original stream emitting the error event, and it doesn't change |
| 1094 * the what the most recently emitted data event is. |
1075 * | 1095 * |
1076 * The returned stream is a broadcast stream if this stream is. | 1096 * The returned stream is a broadcast stream if this stream is. |
1077 * If a broadcast stream is listened to more than once, each subscription | 1097 * If a broadcast stream is listened to more than once, each subscription |
1078 * will individually perform the `equals` test. | 1098 * will individually perform the `equals` test. |
1079 */ | 1099 */ |
1080 Stream<T> distinct([bool equals(T previous, T next)]) { | 1100 Stream<T> distinct([bool equals(T previous, T next)]) { |
1081 return new _DistinctStream<T>(this, equals); | 1101 return new _DistinctStream<T>(this, equals); |
1082 } | 1102 } |
1083 | 1103 |
1084 /** | 1104 /** |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1374 new RangeError.index(index, this, "index", null, elementIndex)); | 1394 new RangeError.index(index, this, "index", null, elementIndex)); |
1375 }, | 1395 }, |
1376 cancelOnError: true); | 1396 cancelOnError: true); |
1377 return future; | 1397 return future; |
1378 } | 1398 } |
1379 | 1399 |
1380 /** | 1400 /** |
1381 * Creates a new stream with the same events as this stream. | 1401 * Creates a new stream with the same events as this stream. |
1382 * | 1402 * |
1383 * Whenever more than [timeLimit] passes between two events from this stream, | 1403 * Whenever more than [timeLimit] passes between two events from this stream, |
1384 * the [onTimeout] function is called. | 1404 * the [onTimeout] function is called, which can emit further events on |
| 1405 * the returned stream. |
1385 * | 1406 * |
1386 * The countdown doesn't start until the returned stream is listened to. | 1407 * The countdown doesn't start until the returned stream is listened to. |
1387 * The countdown is reset every time an event is forwarded from this stream, | 1408 * The countdown is reset every time an event is forwarded from this stream, |
1388 * or when the stream is paused and resumed. | 1409 * or when the stream is paused and resumed. |
1389 * | 1410 * |
1390 * The [onTimeout] function is called with one argument: an | 1411 * The [onTimeout] function is called with one argument: an |
1391 * [EventSink] that allows putting events into the returned stream. | 1412 * [EventSink] that allows putting events into the returned stream. |
1392 * This `EventSink` is only valid during the call to `onTimeout`. | 1413 * This `EventSink` is only valid during the call to [onTimeout]. |
| 1414 * Calling [EventSink.close] on the sink passed to [onTimeout] closes the |
| 1415 * returned stream, and no futher events are processed. |
1393 * | 1416 * |
1394 * If `onTimeout` is omitted, a timeout will just put a [TimeoutException] | 1417 * If [onTimeout] is omitted, a timeout will just put a [TimeoutException] |
1395 * into the error channel of the returned stream. | 1418 * into the error channel of the returned stream. |
| 1419 * If the call to [onTimeout] throws, the error is emitted on the returned |
| 1420 * stream. |
1396 * | 1421 * |
1397 * The returned stream is a broadcast stream if this stream is. | 1422 * The returned stream is a broadcast stream if this stream is. |
1398 * If a broadcast stream is listened to more than once, each subscription | 1423 * If a broadcast stream is listened to more than once, each subscription |
1399 * will have its individually timer that starts counting on listen, | 1424 * will have its individually timer that starts counting on listen, |
1400 * and the subscriptions' timers can be paused individually. | 1425 * and the subscriptions' timers can be paused individually. |
1401 */ | 1426 */ |
1402 Stream<T> timeout(Duration timeLimit, {void onTimeout(EventSink<T> sink)}) { | 1427 Stream<T> timeout(Duration timeLimit, {void onTimeout(EventSink<T> sink)}) { |
1403 StreamController<T> controller; | 1428 StreamController<T> controller; |
1404 // The following variables are set on listen. | 1429 // The following variables are set on listen. |
1405 StreamSubscription<T> subscription; | 1430 StreamSubscription<T> subscription; |
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1990 } | 2015 } |
1991 | 2016 |
1992 void addError(error, [StackTrace stackTrace]) { | 2017 void addError(error, [StackTrace stackTrace]) { |
1993 _sink.addError(error, stackTrace); | 2018 _sink.addError(error, stackTrace); |
1994 } | 2019 } |
1995 | 2020 |
1996 void close() { | 2021 void close() { |
1997 _sink.close(); | 2022 _sink.close(); |
1998 } | 2023 } |
1999 } | 2024 } |
OLD | NEW |