DEV Community

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

Collapse
lallenfrancisl profile image
Allen Francis • Edited on

One addition I think would really be helpful would be the option to specify compare function for the select for using with distinctUntilChanged. Which means the function will be

protected select<K>(
    mapFn: (state: T) => K,
    changeFn?: (a: K, key: K) => boolean
  ): Observable<K> {
    return this.state$.asObservable().pipe(
      map((state: T) => mapFn(state)),
      distinctUntilChanged(changeFn)
    );
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
spierala profile image
Florian Spier Author

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.