import config from '../config' ; import isEmpty from 'lodash.isempty' ; import Errors from '../errors' ; class LocalStoreManager { private LOCAL_STORAGE_KEYS_ID = '_ascey_local_ids' ; private _keys : any = { } ; constructor ( ) { if ( config . isNextJSServer ( ) ) { return ; } if ( ( config . isReactNative ( ) || config . isNodeJS ( ) ) && ! this . engine ( ) ) throw Errors . unsetLocalStore ( ) ; this . _fetchKeys ( ) ; } public engine = ( ) => config . getStoreEngine ( ) ; public getKeys = ( ) => this . _keys ; public toString = ( ) => JSON . stringify ( this . _keys ) ; public toJSON = ( keys ) => this . _keys = JSON . parse ( keys ) ; public isEnabled = ( ) : boolean => ! ! this . engine ( ) ; public expirationSystemDisabled = ( ) : boolean => config . isNodeJS ( ) ; public addElement = ( key , data , expires = 7 ) => { if ( ! this . engine ( ) ) return ; if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; this . engine ( ) . setItem ( key , data ) ; this . addKey ( key , expires ) ; } ; public getElement = async ( key ) => { if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; return await this . engine ( ) . getItem ( key ) || undefined ; } ; public removeElement = ( key ) => { if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; this . engine ( ) . removeItem ( key ) ; this . removeKey ( key ) ; } ; public addKey = ( key , expires = 7 ) => { if ( this . expirationSystemDisabled ( ) ) return ; if ( expires < 0 ) throw new Error ( "expire value can't be negative." ) ; if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; const d = new Date ( ) ; d . setSeconds ( d . getSeconds ( ) + ( expires * 86400 ) ) ; this . getKeys ( ) [ key ] = d . toString ( ) ; this . engine ( ) . setItem ( this . LOCAL_STORAGE_KEYS_ID , this . toString ( ) ) ; } ; public removeKey = ( key ) => { if ( this . expirationSystemDisabled ( ) ) return ; if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; if ( this . getKeys ( ) [ key ] ) { delete this . getKeys ( ) [ key ] ; this . engine ( ) . setItem ( this . LOCAL_STORAGE_KEYS_ID , this . toString ( ) ) ; } } ; public getKeyExpiration = ( key ) => { if ( this . expirationSystemDisabled ( ) ) return ; if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; const dateString = this . getKeys ( ) [ key ] ; return dateString ? new Date ( dateString ) : undefined ; } ; public prune = ( ) => { if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; for ( let key in this . getKeys ( ) ) { this . engine ( ) . removeItem ( key ) ; delete this . getKeys ( ) [ key ] ; } } ; private _fetchKeys = async ( ) => { if ( this . expirationSystemDisabled ( ) ) return ; if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; const keys = await this . engine ( ) . getItem ( this . LOCAL_STORAGE_KEYS_ID ) ; ! isEmpty ( keys ) && this . toJSON ( keys ) ; this . _analyzeExpired ( ) ; } ; private _analyzeExpired = ( ) => { if ( this . expirationSystemDisabled ( ) ) return ; if ( ! this . isEnabled ( ) ) throw Errors . localStoreDisabled ( ) ; const now = new Date ( ) ; for ( let key in this . getKeys ( ) ) { const date = this . getKeyExpiration ( key ) ; date && date < now && this . removeKey ( key ) ; } } ; } export default LocalStoreManager ;