I'd like, very much like in ngrx's selector definition, where you're able to plug a number of observables and have a projection function to return something.
Example
export const selectCommunityByRouteId = createSelector(
selectRouteId,
communitySelectors.selectEntities,
(id, communities) => {
if (id) {
return communities.find(c => c.id === id);
}
}
);
^There is a selectRouteId
and communitySelectors.selectEntities
, 2 observable streams and a projection function that takes both inputs and returns something.
The below code is in my component. Essentially what I'm doing is determining if the owner of a community is the same that is visiting this community and if it is, set the admin only menu items to be seen.
Current
this.community$.pipe(
takeUntil(this.ngDestroyed$),
switchMap(community => this.store.select(selectProfileByID(community && community.owner_id))),
).subscribe(profile => {
if (profile) {
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// @ts-ignore
const sub = claims.sub;
if (sub === profile.subject_id) {
this.store.dispatch(setDrawerAdminNavigationItems({items: [
{title: 'New Forum', route: ['/new/forum']}
]}));
}
}
} else {
this.store.dispatch(setDrawerAdminNavigationItems({items: undefined}));
}
});
}
However now I'm 2 levels deeper in the nest and in a Community
> Container
> Forum
relationship, I'm trying to give the owner the possibility to create a new forum. For this however I need to know the id of the Container
I'm currently viewing. I have a
this.container$ = this.store.select(selectContainerByRouteId);
this is not the issue. This issue is, I need to access the information coming from this selector/observable. So, in my subscribe I need to have not just
this.store.select(selectProfileByID(community && community.owner_id)
but also
this.container$
// or
this.store.select(selectContainerByRouteId)
so I have something like
.subscribe([profile, container] => {
// do something with that
});
or, more elaborate
Desired:
this.community$.pipe(
takeUntil(this.ngDestroyed$),
switchMap(community => this.store.select(selectProfileByID(community && community.owner_id))),
).subscribe([profile, container] => {
if (profile) {
const CONTAINER_ID_HERE = container.id;
const claims = this.oAuthService.getIdentityClaims();
if (claims) {
// @ts-ignore
const sub = claims.sub;
if (sub === profile.subject_id) {
this.store.dispatch(setDrawerAdminNavigationItems({items: [
{title: 'New Forum', route: ['/new/forum', CONTAINER_ID_HERE]}
]}));
}
}
} else {
this.store.dispatch(setDrawerAdminNavigationItems({items: undefined}));
}
});
}
I know there's forkJoin, but this just combines all streams into a single one.
What I'm looking for is a way to take data from multiple streams and pass it to the next function. I believe combineLatest should do it.
What would the function have to look like with combineLatest? Could I still use switchMap? Can I even pass it in a pipe?