Last active
January 28, 2021 16:32
-
-
Save akatakritos/67c996ea1211741b4cd778ae6f46fbff to your computer and use it in GitHub Desktop.
RxJs Data Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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