Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /**
  2. * Gets the value from cache if the key is provided.
  3. * If no value exists in cache, then check if the same call exists
  4. * in flight, if so return the subject. If not create a new
  5. * Subject inFlightObservable and return the source observable.
  6. */
  7. get(key: string, fallback?: Observable<any>, maxAge?: number): Observable<any> | Subject<any> {
  8.  
  9. if (this.hasValidCachedValue(key)) {
  10. console.log(`%cGetting from cache ${key}`, 'color: green');
  11. return Observable.of(this.cache.get(key).value);
  12. }
  13.  
  14. if (!maxAge) {
  15. maxAge = this.DEFAULT_MAX_AGE;
  16. }
  17.  
  18. if (this.inFlightObservables.has(key)) {
  19. return this.inFlightObservables.get(key);
  20. } else if (fallback && fallback instanceof Observable) {
  21. this.inFlightObservables.set(key, new Subject());
  22. console.log(`%c Calling api for ${key}`, 'color: purple');
  23. return fallback.do((value) => { this.set(key, value, maxAge); });
  24. } else {
  25. return Observable.throw('Requested key is not available in Cache');
  26. }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement