Skip to content

Instantly share code, notes, and snippets.

@AriPal
Last active May 27, 2020 22:16
Show Gist options
  • Save AriPal/4e41521c6eac411ae40b214a0915cc5f to your computer and use it in GitHub Desktop.
Save AriPal/4e41521c6eac411ae40b214a0915cc5f to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map, publishReplay, refCount } from 'rxjs/operators';
export interface Config {
componentType: string,
show: Boolean
}
@Injectable({
providedIn: 'root'
})
export class ConfigService {
configs: Observable<Config[]>;
constructor(private httpClient: HttpClient) { }
// Get configs from server | HTTP GET
getConfigs(): Observable<Config[]> {
// Cache it once if configs value is false
if (!this.configs) {
this.configs = this.httpClient.get(`${api_url}/configs`).pipe(
map(data => data['configs']),
publishReplay(1), // this tells Rx to cache the latest emitted
refCount() // and this tells Rx to keep the Observable alive as long as there are any Subscribers
);
}
return this.configs;
}
// Clear configs
clearCache() {
this.configs = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment