Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dartlang rxdart - merge different type streams

here is my use case:

I have two different Observables:

Observable<MobileAdEvent> adEventStream;
Observable<Color> colorStream;

I need to receive notification when either of those two emits an event. zipWith does not work for me, since it waits until both streams has an event to emit. mergeWith also does not seem to work, since it expects both stream to be from the same type. Any hints would be greatly appreciated, Thanks.

like image 547
Angel Todorov Avatar asked Oct 26 '25 10:10

Angel Todorov


2 Answers

What you want is combineLatest operator

Observable<Foo> foo;
Observable<Bar> bar;

final output = Observable.combineLatest2(foo, bar, (Foo foo, Bar bar) {
  // TODO: somehow merge them
});
like image 78
Rémi Rousselet Avatar answered Oct 28 '25 23:10

Rémi Rousselet


Have you tried Observable.race ? it's perfect for notifications :

Given two or more source streams, emit all of the items from only the first of these streams to emit an item or notification.

new Observable.race([
  new Observable.timer(1, new Duration(days: 1)),
  new Observable.timer(2, new Duration(days: 2)),
  new Observable.timer(3, new Duration(seconds: 1))
]).listen(print); // prints 3

learn more here https://pub.dev/documentation/rxdart/latest/rx/Observable/Observable.race.html

like image 39
jfab fab Avatar answered Oct 29 '25 01:10

jfab fab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!