Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Gets the value from cache if the key is provided.
- * If no value exists in cache, then check if the same call exists
- * in flight, if so return the subject. If not create a new
- * Subject inFlightObservable and return the source observable.
- */
- get(key: string, fallback?: Observable<any>, maxAge?: number): Observable<any> | Subject<any> {
- if (this.hasValidCachedValue(key)) {
- console.log(`%cGetting from cache ${key}`, 'color: green');
- return Observable.of(this.cache.get(key).value);
- }
- if (!maxAge) {
- maxAge = this.DEFAULT_MAX_AGE;
- }
- if (this.inFlightObservables.has(key)) {
- return this.inFlightObservables.get(key);
- } else if (fallback && fallback instanceof Observable) {
- this.inFlightObservables.set(key, new Subject());
- console.log(`%c Calling api for ${key}`, 'color: purple');
- return fallback.do((value) => { this.set(key, value, maxAge); });
- } else {
- return Observable.throw('Requested key is not available in Cache');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement