Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {ajax} from "rxjs/ajax";
- import {map, shareReplay} from "rxjs/operators";
- class MyRestService {
- #cacheables = new Map();
- generateAjax(urlOrConfig, enableCache, clear) {
- if(enableCache) {
- const cacheKey = urlOrConfig.url || urlOrConfig;
- if(clear) {
- this.#cacheables.delete(cacheKey);
- }
- this.#cacheables.has(cacheKey) || this.#cacheables.set(cacheKey, this.#generateAjaxBase(urlOrConfig).pipe(shareReplay(1)));
- return this.#cacheables.get(cacheKey);
- }
- return this.#generateAjaxBase(urlOrConfig);
- }
- #generateAjaxBase(urlOrConfig) {
- return ajax(urlOrConfig).pipe(map(r => r.response));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment