DEV Community

Discussion on: Simple yet powerful state management in Angular with RxJS

Collapse
 
spierala profile image
Florian Spier

What is the use-case? Normally you want the Observable returned by select to only emit when it has a new Object reference. It's the same in Akita or NgRx. That behaviour encourages also updating state in an immutable manner.

Collapse
 
killbond profile image
killbond • Edited

Sorry for necroposting, but let's say, we have two different properties in our state. Both are known to be complex, not trivial. To give a better idea, here's an example:

city: { 
  id: number, 
  name: string
},
user: {
  id: number,
  name: string,
}
Enter fullscreen mode Exit fullscreen mode

and you want to know when only the value of the city property will change. You wouldn't be able to achieve that, because distinctUntilChanged uses a strict comparsion by default, so the state subject will emit the same value of the city property every time the user changes. Sometimes this can lead to unwanted behaviour, such as requesting data.

Thread Thread
 
spierala profile image
Florian Spier

If you are interested in city changes, then you can write a selector specific for the city:

city$ = this.select(state => state.city);

The distinctUntilChanges operator inside the select method will make sure that there is only an emission if the city object changes.

However, this selector would emit when city or user change:

userAndCityState$ = this.select(state => state);