Create a replay subject :
const sub = new ReplaySubject(3);
Then make 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 contain the project as first value, the sites as second value, the person as thrid value.
You can do it with the combineLatest
format 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 value returned will be an array of all of your values.
Finally, you have the complicated syntax to concatenate them, without 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, map])))),
);