Skip to content

Commit

Permalink
[learn_dart] Create Stream: async*
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed Jun 23, 2024
1 parent 9ce1b2d commit 203581c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion notes/learn_dart/test/dart_lang!sdk!io/process.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

for i in {1..3} ; do echo $i;sleep 1; done;
for i in {1..3} ; do echo "Hong文的最此打算"$i;sleep 1; done;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'dart:io';
import 'package:path/path.dart' as path;

main() async {
var cmd = path.join(path.current, "test/dart_lang!sdk!io/process.sh");
var cmd = path.join(path.current, "test/dart_lang!sdk!io/process.sh1");
await runProcess(cmd, []);
}

Expand All @@ -14,19 +14,26 @@ Future<void> runProcess(
List<String> args, {
String? cwd,
}) async {
print('\n$command ${args.join(' ')}');
print('\n cmd:$command args:${args.join(' ')}');

var process = await Process.start(command, args, workingDirectory: cwd);
var process = await Process.start(command, args, workingDirectory: cwd,runInShell: true);

process.stdout.transform(utf8.decoder).transform(LineSplitter()).listen((line) {
print(' $line');
});
process.stderr.transform(utf8.decoder).transform(LineSplitter()).listen((line) {
print(' $line');
});
printStream(process.stdout);
printStream(process.stderr);

var exitCode = await process.exitCode;
if (exitCode != 0) {
throw '$command exited with $exitCode';
}
}
Future<void> printStream(Stream<List<int>> stream) async {
var s = stream.transform(utf8.decoder).transform(LineSplitter());
await for (final line in s) {
print(' $line');
}

// stream.transform(utf8.decoder).transform(LineSplitter()).listen((line) {
// print(' $line');
// });

}
22 changes: 22 additions & 0 deletions notes/learn_dart/test/dart_lang!sdk!io/stream_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ignore_for_file: avoid_print

import 'package:checks/checks.dart';
import 'package:test/test.dart';


main() async {
group('Create Stream: async*', () {
test('create', () async {
Stream<int> range(int to) async* {
for (int i = 1; i <= to; i++) {
yield i;
}
}
List<int> collect = [];
await for (int i in range(3)) {
collect.add(i);
}
check(collect).deepEquals([1, 2, 3]);
});
});
}

0 comments on commit 203581c

Please sign in to comment.