As someone who is relatively new to Angular (6 months), I am facing a challenge with my code. Currently, I have two observables that are working independently of each other:
this.siteService.siteChanged$
.pipe(takeUntil(this.disconnect$))
.subscribe(_ => this.getGroups());
this.uiService.currentTab$
.pipe(takeUntil(this.disconnect$))
.subscribe(tab => {
if (tab != Tab.Groups) {
this.windowService.pushToHistory({ group: this.groupService.selectedGroup?.groupId }, true);
}
else {
this.totalCountChange.emit(this.allGroups.length);
this.filteredCountChange.emit(this.filteredGroups.length);
}
});
The issue I am encountering is that the method "this.getGroups()
" from the first observable needs to be called only when the "tab" returned in the second observable is equal to "Tab.Groups
". However, I am struggling to find a way to combine them efficiently. I attempted using "forkJoin", but I couldn't successfully retrieve the value of "tab" when the first one returns void, resulting in errors:
forkJoin(
[this.siteService.siteChanged$,
this.uiService.currentTab$]
).subscribe([void,tab] => {
if(tab == Tab.Groups){
this.getGroups();
}
})