import { StorageStrategy } from '../interfaces/storageStrategy' ; import { Observable } from 'rxjs' ; import { StorageService } from '../interfaces/storageService' ; import { StorageKeyManager } from '../../helpers/storageKeyManager' ; import { distinctUntilChanged , filter , map , shareReplay , switchMap } from 'rxjs/operators' ; export class AsyncStorage implements StorageService { constructor ( protected strategy ) { } retrieve ( key ) { return this . strategy . get ( StorageKeyManager . normalize ( key ) ) . pipe ( map ( ( value ) => typeof value === 'undefined' ? null : value ) ) ; } store ( key , value ) { return this . strategy . set ( StorageKeyManager . normalize ( key ) , value ) ; } clear ( key ? ) { return key !== undefined ? this . strategy . del ( StorageKeyManager . normalize ( key ) ) : this . strategy . clear ( ) ; } getStrategyName ( ) { return this . strategy . name ; } observe ( key ) { key = StorageKeyManager . normalize ( key ) ; return this . strategy . keyChanges . pipe ( filter ( ( changed ) => changed === null || changed === key ) , switchMap ( ( ) => this . strategy . get ( key ) ) , distinctUntilChanged ( ) , shareReplay ( ) ) ; } }