user6600549
user6600549

Reputation:

The Rxjs forkJoin is not working on Multiple Subjects

I want to subscribe to multiple subjects at once and I dont get the expected data. For some reason, the subjects and the forkJoin is not working as I expect.

Can someone help me on how to subscribe to multiple subjects?

My code so far.

public scene$: ReplaySubject<THREE.Scene>;
public camera$: ReplaySubject<THREE.PerspectiveCamera>;
public renderer$: ReplaySubject<THREE.WebGLRenderer>;


// then i push some data
this.scene$.next({data: {} });
this.camera$.next({data: {} });
this.renderer$.next({data: {} });


// consuming the data
const a = forkJoin({
   scene: this.scene$,
   camera:  this.camera$,
   renderer: this.renderer$
}).subscribe( res => {
   console.log('=== out ===', {res});  // I dont get any response.
});


Upvotes: 2

Views: 950

Answers (1)

Forkjoin will give you result when the observables are completed. For your code, try combineLatest.

Check this out: combineLatest

Do it like this:

const a = combineLatest( this.scene$, this.camera$, this.renderer$ )
    .subscribe( ([scene, camera, renderer]) => {
        console.log('scene', scene, 'camera', camera, 'renderer', renderer 
    )}
);

Upvotes: 5

Related Questions