Extract the following value from the source. Observed when another Observed, notifier emits

I would expect my case to be common, but cannot find anything suitable. What I want to achieve in Angular2 / RxJS 5 :

 source: ---1--2--3--4---------5--------6-|--> notifier: -o------------o-----o---o--oo------> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ output: ---1----------2-----3---4--5---6-|--> 

So, I have an Observable source that emits values, and I want each of them to go to the output only when the second Observable is called (call its notifier). This, as one event from the notifier, means "allow passing through."

I tried delayWhen , but my main problem is that all the source values ​​expect the same event from the notifier, so for example, if three source values ​​are β€œqueued” and the notifier emits once, all 3 values ​​pass. that is not what i want.

+6
source share
2 answers

zip answer:

 const valueStream = Rx.Observable.from([0, 1, 2, 3, 4, 5, 6]); const notificationStream = Rx.Observable.interval(1000).take(7); Rx.Observable .zip(valueStream, notificationStream, (val, notification) => val) .subscribe(val => console.log(val)); 

A working example is here .

This creates value when a pair is created from both threads. Thus, the example will print the value from valueStream when notificationStream valueStream value.

+2
source

I think the zip operator is what you are looking for:

 sourceSubject:Subject = new Subject(); notifierSubject:Subject = new Subject(); index = 1; constructor() { Observable.zip( this.sourceSubject, this.notifierSubject ) .map(data => data[0]) .subscribe(data => { console.log('>> output = '+data.id); }); } emit() { this.sourceSubject.next({id: this.index}); this.index++; } notify() { this.notifierSubject.next(); } 

See this plunkr: https://plnkr.co/edit/MK30JR2qK8aJIGwNqMZ5?p=preview .

See also this question:

+1
source

All Articles