import { isPlatformBrowser } from '@angular/common' ; import { Inject , Injectable , PLATFORM_ID } from '@angular/core' ; import { LocalStorage } from '../interfaces/local-storage.interface' ; import { MemoryStorage } from '../interfaces/memory-storage.interface' ; import { storageAvailable } from '../utils/storage.util' ; @ Injectable ( ) export class BaseLocalStorage implements LocalStorage { /**
     * Storage
     */ private readonly storage : Storage ; constructor ( private memoryStorage , @ Inject ( PLATFORM_ID ) private platformId ) { if ( isPlatformBrowser ( this . platformId ) && storageAvailable ( 'localStorage' ) ) { this . storage = window . localStorage ; } else { this . storage = this . memoryStorage ; } } get length ( ) : number { return this . storage . length ; } clear ( ) { this . storage . clear ( ) ; } getItem ( key ) { return this . storage . getItem ( key ) ; } key ( index ) { return this . storage . key ( index ) ; } removeItem ( key ) { this . storage . removeItem ( key ) ; } setItem ( key , value ) { this . storage . setItem ( key , value ) ; } }