import { startWith , tap , shareReplay } from 'rxjs/operators' ; import { Observable } from 'rxjs' ; param cacheKey export const shareAndCache = ( cacheKey ) => < T > ( source ) => { let cachedValue = null ; try { cachedValue = JSON . parse ( localStorage [ cacheKey ] ) ; } catch ( parseException ) { } let result = source . pipe ( tap ( next => ( localStorage [ cacheKey ] = JSON . stringify ( next ) ) ) ) ; if ( cachedValue ) { result = result . pipe ( startWith ( < T > cachedValue ) ) ; } return result . pipe ( shareReplay ( 1 ) ) ; } ;