将事件添加到Dart中的现有流

时间:2019-04-15 08:48:36

标签: dart dart-async

我有一个从Websocket流派生的流A(转换后,...)

  • 是否有用于向流A“添加事件/数据”的API?

  • 如果这不是直接可能的话,我是否必须使用自定义事件创建自定义流B,并将A + B合并到新的流C中?我也不知道该怎么做(合并位)。

下面是到目前为止我测试过的东西,它似乎可以正常工作,但是我对Dart还是陌生的,它看起来有些过分了:

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

main() async {
  var stream = countStream(0, 30);

  final multiStream = StreamSplitter(stream);
  var even1 = multiStream.split().where((i) => i.isEven);
  var odd1 = multiStream.split().where((i) => i.isOdd);
  var other = multiStream.split().where((i) => i % 3 == 0);
  multiStream.close();

/*
What I just want to do at the end :
other.listen((i) => add $i to odd1 or even1 after complex transformation);
*/

  var other2 = other.map((n) => -n);

  final multiStream2 = StreamSplitter(other2);
  var even2 = multiStream2.split().where((i) => i.isEven);
  var odd2 = multiStream2.split().where((i) => i.isOdd);
  multiStream2.close();

  var even = StreamGroup.merge([even1, even2]);
  var odd = StreamGroup.merge([odd1, odd2]);

  even.listen((i) => print("even $i"));
  odd.listen((i) => print("odd $i"));


}

0 个答案:

没有答案