|
| 1 | +import { of } from 'rxjs'; |
| 2 | +import { Observable } from 'rxjs/internal/Observable'; |
| 3 | +import { isObservable } from 'rxjs/internal/util/isObservable'; |
| 4 | +import { shareReplay, tap } from 'rxjs/operators'; |
| 5 | +import { DefaultConfigCacheModel, __simpleCacheStore } from './simple-cache-store'; |
| 6 | + |
| 7 | +const getRxjsObservableCacheValue = (key: string, payloadToHash?: any): Observable<any> => { |
| 8 | + const cache = __simpleCacheStore.getCacheValue(key, payloadToHash); |
| 9 | + |
| 10 | + if (cache == null) return cache; |
| 11 | + |
| 12 | + if (isObservable(cache)) return cache; |
| 13 | + |
| 14 | + return of(cache); |
| 15 | +}; |
| 16 | + |
| 17 | +const setRxjsObservableCacheValue = ( |
| 18 | + stream: Observable<any>, |
| 19 | + key: string, |
| 20 | + payloadToHash?: any, |
| 21 | + config?: DefaultConfigCacheModel |
| 22 | +): Observable<any> => { |
| 23 | + if (stream == null || key == null) return of(null); |
| 24 | + |
| 25 | + const setCache = ($data: any) => { |
| 26 | + if ($data != null) __simpleCacheStore.setCacheValue(key, $data, payloadToHash, config); |
| 27 | + }; |
| 28 | + |
| 29 | + setCache(stream.pipe(shareReplay())); |
| 30 | + |
| 31 | + return stream.pipe( |
| 32 | + tap((_data: any) => setCache(_data)), |
| 33 | + shareReplay() |
| 34 | + ); |
| 35 | +}; |
| 36 | + |
| 37 | +const __rxCacheStore = { |
| 38 | + getRxjsObservableCacheValue, |
| 39 | + setRxjsObservableCacheValue, |
| 40 | + resetCache: __simpleCacheStore.resetCache, |
| 41 | + resetAllCaches: __simpleCacheStore.resetAllCaches, |
| 42 | +}; |
| 43 | + |
| 44 | +export { __rxCacheStore as __rxjsCacheStore }; |
0 commit comments