Skip to content

Instantly share code, notes, and snippets.

@akatakritos
Last active January 28, 2021 16:32
Show Gist options
  • Save akatakritos/67c996ea1211741b4cd778ae6f46fbff to your computer and use it in GitHub Desktop.
Save akatakritos/67c996ea1211741b4cd778ae6f46fbff to your computer and use it in GitHub Desktop.
RxJs Data Cache
/**
* Provides a cache of look up data
*
* consumers can subscribe or use async-pipe to get the list of lookup values in each category
*
* shareReplay(1) will make it so that each subscriber gets the last emitted value rather than making
* another API call
* take(1) is just a convenience for the consumers in case they forget to unsubscribe it will
* only emit once and then complete, to avoid a memory leak
*/
export class LookupDataStore {
constructor(private readonly api: ApiService) { }
public readonly countries$ = this.api.getLookupData('countries').pipe(shareReplay(1), take(1));
public readonly userRoles$ = this.api.getLookupData('userRoles').pipe(shareReplay(1), take(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment