I want to make several periodic REST API calls and merge all the responses into one single stream. This is possible with StreamGroup, but is it possible to merge a new stream to the already existing stream at runtime? Let's say I have two periodic streams that I merge into one stream with StreamGroup. How can I add/merge a new stream.periodic
(for example getC()
) to the already existing Stream stream
after it was created (so after StreamGroup.merge(streamList)
) ?
import 'package:async/async.dart' show StreamGroup;
// ...
Stream<dynamic> stream;
var streamList = <Stream>[];
// ...
void addPeriodicCallToA({Duration period}) {
streamList.add(Stream<dynamic>.periodic(period, (x) async {
return await getA();
}));
}
// ...
void addPeriodicCallToB({String symbol, Duration period}) {
streamList.add(Stream<dynamic>.periodic(period, (x) async {
return await getB(symbol: symbol);
}));
}
// ...
addPeriodicCallToA(period: Duration(seconds: 1));
addPeriodicCallToA(period: Duration(symbol: 'yoloandswaglol', seconds: 2));
stream = StreamGroup.merge(streamList);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…