OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
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.md file. | |
4 | |
5 part of fletch.vm_session; | |
6 | |
7 class SessionCommandTransformerBuilder | |
8 extends CommandTransformerBuilder<VmCommand> { | |
9 | |
10 VmCommand makeCommand(int code, ByteData payload) { | |
11 return new VmCommand.fromBuffer( | |
12 VmCommandCode.values[code], toUint8ListView(payload)); | |
13 } | |
14 } | |
15 | |
16 class VmCommandReader { | |
17 final StreamIterator<VmCommand> iterator; | |
18 | |
19 VmCommandReader( | |
20 Stream<List<int>> stream, | |
21 Sink<List<int>> stdoutSink, | |
22 Sink<List<int>> stderrSink) | |
23 : iterator = new StreamIterator<VmCommand>( | |
24 filterCommandStream(stream, stdoutSink, stderrSink)); | |
25 | |
26 static Stream<VmCommand> filterCommandStream( | |
27 Stream<List<int>> stream, | |
28 Sink<List<int>> stdoutSink, | |
29 Sink<List<int>> stderrSink) async* { | |
30 // When done with the data on the socket, we do not close | |
31 // stdoutSink and stderrSink. They are usually stdout and stderr | |
32 // and the user will probably want to add more on those streams | |
33 // independently of the messages added here. | |
34 StreamTransformer<List<int>, VmCommand> transformer = | |
35 new SessionCommandTransformerBuilder().build(); | |
36 await for (VmCommand command in stream.transform(transformer)) { | |
37 if (command is StdoutData) { | |
38 if (stdoutSink != null) stdoutSink.add(command.value); | |
39 } else if (command is StderrData) { | |
40 if (stderrSink != null) stderrSink.add(command.value); | |
41 } else { | |
42 yield command; | |
43 } | |
44 } | |
45 } | |
46 } | |
OLD | NEW |