Skip to content

Commit

Permalink
Tutorial(streams): update for Dart 2 and analayze/test code
Browse files Browse the repository at this point in the history
Contributes to #407, #637, #664
  • Loading branch information
chalin committed Apr 23, 2018
1 parent 8ba98c7 commit 998a37a
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 128 deletions.
14 changes: 14 additions & 0 deletions examples/misc/lib/tutorial/cat_no_hash.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

Future<void> main(List<String> args) async {
var file = new File(args[0]);
var lines = file
.openRead()
.transform(utf8.decoder) //!<br>
.transform(const LineSplitter());
await for (var line in lines) {
if (!line.startsWith('#')) print(line);
}
}
49 changes: 48 additions & 1 deletion examples/misc/lib/tutorial/misc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: type_annotate_public_apis, unused_element
// ignore_for_file: annotate_overrides, type_annotate_public_apis, unused_element
// NOTE: Declarations in this file are analyzed but not tested.

import 'dart:async';
Expand Down Expand Up @@ -33,3 +33,50 @@ void futuresTutorial() {
.catchError((e) => handleError(e));
// #enddocregion Future-wait
}

void streamsTutorial() {
// #docregion lastPositive
Future<int> lastPositive(Stream<int> stream) =>
stream.lastWhere((x) => x >= 0);
// #enddocregion lastPositive

void log(e) {}

// #docregion mapLogErrors
Stream<S> mapLogErrors<S, T>(
Stream<T> stream,
S Function(T event) convert,
) async* {
var streamWithoutErrors = stream.handleError((e) => log(e));
await for (var event in streamWithoutErrors) {
yield convert(event);
}
}
// #enddocregion mapLogErrors
}

abstract class MyStream<T> extends Stream<T> {
// #docregion mock-stream-method-implementations
Future<bool> contains(Object needle) async {
await for (var event in this) {
if (event == needle) return true;
}
return false;
}

Future forEach(void Function(T element) action) async {
await for (var event in this) {
action(event);
}
}

Future<List<T>> toList() async {
final result = <T>[];
await this.forEach(result.add);
return result;
}

Future<String> join([String separator = ""]) async =>
(await this.toList()).join(separator);
// #enddocregion mock-stream-method-implementations
}
60 changes: 60 additions & 0 deletions examples/misc/lib/tutorial/stream_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ignore_for_file: annotate_overrides, type_annotate_public_apis
import 'dart:async';

// TODO(chalin): I believe that handleError's test argument should be declared
// as follows, but it isn't: {bool Function(dynamic error) test}.

abstract class MyStream<T> implements Stream<T> {
// #docregion main-stream-members
Future<T> get first;
Future<bool> get isEmpty;
Future<T> get last;
Future<int> get length;
Future<T> get single;
Future<bool> any(bool Function(T element) test);
Stream<E> asyncExpand<E>(Stream<E> Function(T event) convert);
Stream<E> asyncMap<E>(FutureOr<E> Function(T event) convert);
Stream<R> cast<R>();
Future<bool> contains(Object needle);
Stream<T> distinct([bool Function(T previous, T next) equals]);
Future<E> drain<E>([E futureValue]);
Future<T> elementAt(int index);
Future<bool> every(bool Function(T element) test);
Stream<S> expand<S>(Iterable<S> Function(T element) convert);
Future<T> firstWhere(bool Function(T element) test, {T Function() orElse});
Future<S> fold<S>(S initialValue, S Function(S previous, T element) combine);
Future forEach(void Function(T element) action);
Future<String> join([String separator = ""]);
Future<T> lastWhere(bool Function(T element) test, {T Function() orElse});
Stream<S> map<S>(S Function(T event) convert);
Future pipe(StreamConsumer<T> streamConsumer);
Future<T> reduce(T Function(T previous, T element) combine);
Stream<R> retype<R>();
Future<T> singleWhere(bool Function(T element) test, {T Function() orElse});
Stream<T> skip(int count);
Stream<T> skipWhile(bool Function(T element) test);
Stream<T> take(int count);
Stream<T> takeWhile(bool Function(T element) test);
Future<List<T>> toList();
Future<Set<T>> toSet();
Stream<T> where(bool Function(T event) test);
// #enddocregion main-stream-members

bool get isBroadcast;

Stream<T> asBroadcastStream(
{void Function(StreamSubscription<T> subscription) onListen,
void Function(StreamSubscription<T> subscription) onCancel});

// #docregion special-stream-members
Stream<T> handleError(Function onError, {bool test(error)});
Stream<T> timeout(Duration timeLimit,
{void Function(EventSink<T> sink) onTimeout});
Stream<S> transform<S>(StreamTransformer<T, S> streamTransformer);
// #enddocregion special-stream-members

// #docregion listen
StreamSubscription<T> listen(void Function(T event) onData,
{Function onError, void Function() onDone, bool cancelOnError});
// #enddocregion listen
}
25 changes: 25 additions & 0 deletions examples/misc/lib/tutorial/sum_stream.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ignore_for_file: type_annotate_public_apis
// #docregion
import 'dart:async';

// #docregion sumStream
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
await for (var value in stream) {
sum += value;
}
return sum;
}
// #enddocregion sumStream

Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i++) {
yield i;
}
}

main() async {
var stream = countStream(10);
var sum = await sumStream(stream);
print(sum); // 55
}
31 changes: 31 additions & 0 deletions examples/misc/lib/tutorial/sum_stream_with_catch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ignore_for_file: type_annotate_public_apis
// #docregion
import 'dart:async';

Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
try {
await for (var value in stream) {
sum += value;
}
} catch (e) {
return -1;
}
return sum;
}

Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i++) {
if (i == 4) {
throw new Exception('Intentional exception');
} else {
yield i;
}
}
}

main() async {
var stream = countStream(10);
var sum = await sumStream(stream);
print(sum); // -1
}
11 changes: 11 additions & 0 deletions examples/misc/test/tutorial/streams_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ignore_for_file: type_annotate_public_apis
import 'package:test/test.dart';
import 'package:examples/tutorial/sum_stream.dart' as sum_stream;
import 'package:examples/tutorial/sum_stream_with_catch.dart'
as sum_stream_with_catch;

void main() {
test('sumStream', () => expect(sum_stream.main, prints('55\n')));
test('sumStream with catch',
() => expect(sum_stream_with_catch.main, prints('-1\n')));
}
Loading

0 comments on commit 998a37a

Please sign in to comment.