import { StorageStrategy } from '../core/interfaces/storageStrategy' ; import { Observable , of , Subject } from 'rxjs' ; import { StrategyCacheService } from '../core/strategyCache' ; import { CompatHelper } from '../helpers/compat' ; import { WebStorage } from '../core/interfaces/webStorage' ; export abstract class BaseSyncStorageStrategy implements StorageStrategy < any > { readonly keyChanges : Subject < string > = new Subject ( ) ; abstract readonly name : string ; constructor ( protected storage , protected cache ) { } protected _isAvailable : boolean ; get isAvailable ( ) : boolean { if ( this . _isAvailable === undefined ) this . _isAvailable = CompatHelper . isStorageAvailable ( this . storage ) ; return this . _isAvailable ; } get ( key ) { let data = this . cache . get ( this . name , key ) ; if ( data !== undefined ) return of ( data ) ; try { const item = this . storage . getItem ( key ) ; if ( item !== null ) { data = JSON . parse ( item ) ; this . cache . set ( this . name , key , data ) ; } } catch ( err ) { console . warn ( err ) ; } return of ( data ) ; } set ( key , value ) { const data = JSON . stringify ( value ) ; this . storage . setItem ( key , data ) ; this . cache . set ( this . name , key , value ) ; this . keyChanges . next ( key ) ; return of ( value ) ; } del ( key ) { this . storage . removeItem ( key ) ; this . cache . del ( this . name , key ) ; this . keyChanges . next ( key ) ; return of ( null ) ; } clear ( ) { this . storage . clear ( ) ; this . cache . clear ( this . name ) ; this . keyChanges . next ( null ) ; return of ( null ) ; } }