import { Injectable , Inject } from '@angular/core' ; import { IStorageSetConfig , IDriverType } from './metadata' ; import { Driver } from './Driver' ; import { DRIVERS , LOCKER_DRIVER_TYPES } from './DriverTypes' ; import { LockerConfig } from './LockerConfig' ; @ Injectable ( ) export class Locker { private driverFallback : DRIVERS | DRIVERS [ ] ; private namespace : string ; private separator : string ; constructor ( @ Inject ( LOCKER_DRIVER_TYPES ) private driverTypes , private lockerConfig ) { this . setNamespace ( ) ; this . setSeparator ( ) ; this . setDriverFallback ( ) ; } public setNamespace ( namespace = this . lockerConfig . driverNamespace ) { this . namespace = namespace ; } public setSeparator ( separator = this . lockerConfig . namespaceSeparator ) { this . separator = separator ; } public setDriverFallback ( driverFallback = this . lockerConfig . driverFallback ) { this . driverFallback = driverFallback ; } public set ( type , key , data , config ? ) { this . _getDriver ( type ) . set ( this . _makeKey ( key ) , data , config ) ; } public get ( type , key ) { return this . _getDriver ( type ) . get ( this . _makeKey ( key ) ) ; } public has ( type , key ) { return this . _getDriver ( type ) . has ( this . _makeKey ( key ) ) ; } public remove ( type , key ) { this . _getDriver ( type ) . remove ( this . _makeKey ( key ) ) ; } public key ( type , index ? ) { return this . _decodeKey ( this . _getDriver ( type ) . key ( index ) ) ; } public clear ( type ) { this . _getDriver ( type ) . clear ( ) ; } private _makeKey ( key ) { return this . namespace ? `${ this . namespace }${ this . separator }${ key }` : key ; } private _decodeKey ( key ) { if ( this . namespace ) return key . slice ( this . namespace . length + this . separator . length ) ; else return key ; } private _getDriver ( type ) { const askedDriver = this . _getDriverType ( type ) ; if ( askedDriver && askedDriver . storage . isSupported ( ) ) return askedDriver . storage ; else return this . _getFallbackDriverType ( ) . storage ; } private _getDriverType ( type ) { return this . driverTypes . find ( ( driverType ) => driverType . type === type ) ; } private _getFallbackDriverType ( ) { if ( Array . isArray ( this . driverFallback ) ) { return this . driverFallback . map ( ( type ) => this . _getDriverType ( type ) ) . find ( ( driverType ) => driverType . storage . isSupported ( ) ) || this . _getDriverType ( DRIVERS . MEMORY ) ; } else if ( this . driverFallback ) { const driverType = this . _getDriverType ( this . driverFallback ) ; return driverType . storage . isSupported ( ) ? driverType : this . _getDriverType ( DRIVERS . MEMORY ) ; } else { return this . _getDriverType ( DRIVERS . MEMORY ) ; } } }