Set up a replay subject like this:
const sub = new ReplaySubject(3);
Now, proceed with your calls:
this.getProject(1).pipe(
tap(project => sub.next(project)),
switchMap(project => this.getSites(1)),
tap(sites => sub.next(sites)),
switchMap(sites => this.getPersons(sites[0].id)),
tap(person => sub.next(person))
);
Your replay subject will store the project as the first value, sites as the second value, and person as the third value.
If you prefer using the combineLatest
approach with a BehaviorSubject
:
const obs = new BehaviorSubject([]);
const add = val => obs.pipe(
take(1),
map(v => ([...v, val]))
).subscribe(v => obs.next(v));
this.getProject(1).pipe(
tap(project => add(project)),
switchMap(project => this.getSites(1)),
tap(sites => add(sites)),
switchMap(sites => this.getPersons(sites[0].id)),
tap(person => add(person))
);
This time, the returned value will be an array containing all values together.
Lastly, here is a way to concatenate them without using a subject:
this.getProject(1).pipe(
switchMap(project => this.getSites(1).pipe(map(sites => ([project, sites])))),
switchMap(([project, sites]) => this.getPersons(sites[0].id).pipe(map(person => ([project, sites, person])))),
);