import { Injectable , Optional , SkipSelf } from '@angular/core' ; @ Injectable ( { providedIn : 'root' , } ) export class StorageService { private window : Window ; private localStorage : Storage ; private sessionStorage : Storage ; constructor ( ) { this . window = window ; this . localStorage = window . localStorage ; this . sessionStorage = window . sessionStorage ; } public setLocalObject ( key , obj ) { return this . setObject ( this . localStorage , key , obj ) ; } public setLocalItem ( key , obj ) { this . setItem ( this . localStorage , key , obj ) ; } public setSessionObject ( key , obj ) { return this . setObject ( this . sessionStorage , key , obj ) ; } public setSessionItem ( key , obj ) { this . setItem ( this . sessionStorage , key , obj ) ; } private setObject ( storage , key , obj ) { let jsonString ; try { jsonString = JSON . stringify ( obj ) ; } catch ( ex ) { return false ; } this . setItem ( storage , key , jsonString ) ; return true ; } private setItem ( storage , key , obj ) { storage . setItem ( key , obj ) ; } public setWindowName ( value ) { this . window . name = value ; } public getLocalObject < T > ( key ) { return this . getObject < T > ( this . localStorage , key ) ; } public getLocalObjects < T > ( keys ) { return this . getObjects < T > ( this . localStorage , keys ) ; } public getLocalItem ( key ) { return this . getItem ( this . localStorage , key ) ; } public getSessionObject < T > ( key ) { return this . getObject < T > ( this . sessionStorage , key ) ; } public getSessionObjects < T > ( keys ) { return this . getObjects < T > ( this . sessionStorage , keys ) ; } public getSessionItem ( key ) { return this . getItem ( this . sessionStorage , key ) ; } public getObjects < T > ( storage , keys ) { return keys . map ( key => this . getObject < T > ( storage , key ) ) ; } private getObject < T > ( storage , key ) { const jsonString = this . getItem ( storage , key ) ; if ( jsonString === null ) { return null ; } try { return JSON . parse ( jsonString ) as T ; } catch ( ex ) { return null ; } } private getItem ( storage , key ) { return storage . getItem ( key ) || null ; } public getWindowName ( ) { return this . window . name ; } public removeLocalItem ( key ) { this . removeItem ( this . localStorage , key ) ; } public removeSessionItem ( key ) { this . removeItem ( this . sessionStorage , key ) ; } private removeItem ( storage , key ) { storage . removeItem ( key ) ; } public clearLocalStorage ( ) { this . clearStorage ( this . localStorage ) ; } public clearSessionStorage ( ) { this . clearStorage ( this . sessionStorage ) ; } private clearStorage ( storage ) { storage . clear ( ) ; } public getLocalItemKeys ( ) { return this . getStorageItemKeys ( this . localStorage ) ; } public getSessionItemKeys ( ) { return this . getStorageItemKeys ( this . sessionStorage ) ; } private getStorageItemKeys ( storage ) { const keys = [ ] ; for ( let i = 0 ; i < storage . length ; i ++ ) { keys . push ( storage . key ( i ) ) ; } return keys ; } } export const StorageServiceProviderFactory = ( parentDispatcher ) => { return parentDispatcher || new StorageService ( ) ; } ; export const StorageServiceProvider = { provide : StorageService , deps : [ [ new Optional ( ) , new SkipSelf ( ) , StorageService ] ] , useFactory : StorageServiceProviderFactory , } ;