I am working on an angular stack that has n amount of services that hold data in stores of this type:

fooStore$ = BehaviorSubject<FooStoreType>({})

accessing properties in these have generally been done like this:

export wantedPropSelector$ = fooStore$.pipe(
  map(store => store.wantedProp)

Now I'm working on improving this by adding memoization and selector singletons for each service. I've ended up with a pattern that allows this syntax in the service-side:

export wantedPropSelector$ = select(fooStore$, store => store.wantedProp);

and the select function looks like this:

type MappingFunction<T, R> = (mappable: T) => R;
type MemoizationFunction<R> = (previousResult: R, currentResult: R) => boolean;

export function select<T, R>(
  inStream: Observable<T>,
  mappingFunction: MappingFunction<T, R>,
  memoizationFunction?: MemoizationFunction<R>
): Observable<R> {
  return inStream.pipe(

My assumption is that distinctUntilChanged will give me memoization, and it's possible to send in custom memoization logic for Objects, and shareReplay(1) will allow me to turn the selectors into singletons that also return the last value.

I'd greatly appreciate CR on this from anyone sufficiently experienced with RxJs to point out any possible flaws in this code.

  • 1
    \$\begingroup\$ Welcome to Code Review! While I don't have any experience with RxJs myself, this looks like a good question to me. If you are interested in more about Code Review, read the tour or How to Ask. \$\endgroup\$ – Gloweye Oct 30 '19 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.