userradu
userradu

Reputation: 119

NgRx Selector - cold or hot observable

I know that the ngrx store is a hot observable based on this answer: Is @ngrx/store a hot or cold observable?

My question is if a selector is a hot or cold observable?

Thanks!

Upvotes: 0

Views: 1458

Answers (2)

frido
frido

Reputation: 14129

Selectors are pure functions not observables.

You're probably asking about the select operator or function.

this.store.pipe(select(...)) // or this.store.select(...)

That's basically just

this.store.pipe(
  map(...),
  distinctUntilChanged()
)

map and distinctUntilChanged don't really make an observable hot or cold. They only subscribe to their source if they are subscribed to, so data will only flow through them if you subscribe. But if the source (e.g. store) receives items from producers before you subscribe (i.e. is hot) you won't receive them.

So subscribing to this.store.pipe(select(...)) won't tell the store to start producing values, the store will emit values before that as it's actively listening to its producers (reducers & actions), but select won't listen to the store and no data will flow through it unless you subscribe.

Upvotes: 3

ggradnig
ggradnig

Reputation: 14189

It's cold because it won't subscribe to the source until someone subscribes to it.

Most operators except for the share and publish operators create cold observables.

Upvotes: 3

Related Questions