Reputation: 16575
I am using switchMapTo
to create an inner stream that is triggered by an outer observer.
// a change in value of categoryId triggers the inner observer to reinitialize
this.category$ = this.categoryId$.pipe(
switchMapTo((newCategoryId) =>
// inner stream is reinitialized using result from outer stream
this.categoriesQuery.selectEntity(newCategoryId)
)
)
.switchMapTo
doesn't actually return the result from the outer observer to the inner observer. As far as I can tell, the inner stream is initialized just once and then it is triggered by each new emission from the outer observer
How .switchMapTo
actually works:
this.category$ = this.categoryId$.pipe(
switchMapTo(
this.categoriesQuery.selectEntity(newCategoryId) // <= where does newCategoryId come from now?
)
)
Unfortunately this doesn't work either:
this.category$ = this.categoryId$.pipe(
tap((newValue) => {
this.currentCategoryId = newValue
}),
switchMapTo(() =>{
this.categoriesQuery.selectEntity(this.currentCategoryId)
}
)
)
Because the inner observer is only initialized once (not at every emission from the outer observer) and so the value of this.currentCategoryId
is hard-coded in the first time it's evaluated.
I'm pretty stuck. I'd like to have the effect of switchMapTo
i.e. the outer observer triggers the emission of a new inner stream. But it needs to be a new inner stream and not just the repetition of the original one. Is this possible?
Upvotes: 0
Views: 588
Reputation: 29355
use switchMap
, not switchMapTo
...
this.category$ = this.categoryId$.pipe(
switchMap((newCategoryId) =>
// inner stream is reinitialized using result from outer stream
this.categoriesQuery.selectEntity(newCategoryId)
)
)
switchMapTo
is essentially a shorthand for a switchMap
that switches into a static observable that doesn't care about the outer observable, not a dynamic one that relies on it, that's what switchMap
is for.
Similar logic applies to all operators with a To
variant such as map
and mapTo
... you usually want the plain one, the To
variants are more special case.
Upvotes: 1