Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip isn't emitting values after the first values from both observables have been emitted

I'm trying to combine two observables, one of em being route params and the other one a custom one.

I'm using zip because forkJoin doesn't seem to work. But the zip is acting strange, cuz when I emit my custom one I don't get that value. I only get the first null value which has been given to my BehaviorSubject as a default emit value.

I read this

"The zip operator will subscribe to all inner observables, waiting for each to emit a value. Once this occurs, all values with the corresponding index will be emitted. This will continue until at least one inner observable completes."

Does that mean that it won't respond until both of the observables emit a value? Because the route params observable will only emit once, but the dataEmitter will continue to emit values. What would be the correct operator to use in this case?

Here's some of my code:

Emitter in my service:

private dataEmitter: BehaviorSubject<any> = new BehaviorSubject<any>(null);

Component subscription, only get null from the dataEmitter, never this.calculatedData:

Observable.zip(this.route.params, this.dataCalculator.dataEmitter$)
  .subscribe(data => console.log(data));

This is called when all of the calculations have been done:

this.dataEmitter.next(this.calculatedData);
like image 709
Chrillewoodz Avatar asked Oct 17 '22 09:10

Chrillewoodz


1 Answers

As pointed out by @cartant switching to combineLatest works as expected:

Observable.combineLatest(this.route.params, this.dataCalculator.dataEmitter$)
  .subscribe(data => console.log(data));
like image 147
Chrillewoodz Avatar answered Nov 15 '22 09:11

Chrillewoodz