File

src/Locker.ts

Index

Properties
Methods

Constructor

constructor(driverTypes: IDriverType[], lockerConfig: LockerConfig)
Defined in src/Locker.ts:12
Parameters :
Name Type Optional Description
driverTypes IDriverType[]
lockerConfig LockerConfig

Methods

Private _decodeKey
_decodeKey(key: string)
Defined in src/Locker.ts:60
Parameters :
Name Type Optional Description
key string
Returns : string
Private _getDriver
_getDriver(type: DRIVERS)
Defined in src/Locker.ts:67
Parameters :
Name Type Optional Description
type DRIVERS
Returns : Driver
Private _getDriverType
_getDriverType(type: DRIVERS)
Defined in src/Locker.ts:76
Parameters :
Name Type Optional Description
type DRIVERS
Returns : IDriverType
Private _getFallbackDriverType
_getFallbackDriverType()
Defined in src/Locker.ts:80
Returns : IDriverType
Private _makeKey
_makeKey(key: string)
Defined in src/Locker.ts:56
Parameters :
Name Type Optional Description
key string
Returns : string
Public clear
clear(type: DRIVERS)
Defined in src/Locker.ts:52
Parameters :
Name Type Optional Description
type DRIVERS
Returns : void
Public get
get(type: DRIVERS, key: string)
Defined in src/Locker.ts:36
Parameters :
Name Type Optional Description
type DRIVERS
key string
Returns : any
Public has
has(type: DRIVERS, key: string)
Defined in src/Locker.ts:40
Parameters :
Name Type Optional Description
type DRIVERS
key string
Returns : any
Public key
key(type: DRIVERS, index?: number)
Defined in src/Locker.ts:48
Parameters :
Name Type Optional Description
type DRIVERS
index number true
Returns : string
Public remove
remove(type: DRIVERS, key: string)
Defined in src/Locker.ts:44
Parameters :
Name Type Optional Description
type DRIVERS
key string
Returns : void
Public set
set(type: DRIVERS, key: string, data: any, config?: IStorageSetConfig)
Defined in src/Locker.ts:32
Parameters :
Name Type Optional Description
type DRIVERS
key string
data any
config IStorageSetConfig true
Returns : void
Public setDriverFallback
setDriverFallback(driverFallback: DRIVERS | [])
Defined in src/Locker.ts:28
Parameters :
Name Type Optional Description
driverFallback DRIVERS | []
Returns : void
Public setNamespace
setNamespace(namespace: string)
Defined in src/Locker.ts:20
Parameters :
Name Type Optional Description
namespace string
Returns : void
Public setSeparator
setSeparator(separator: string)
Defined in src/Locker.ts:24
Parameters :
Name Type Optional Description
separator string
Returns : void

Properties

Private driverFallback
driverFallback: DRIVERS | []
Type : DRIVERS | []
Defined in src/Locker.ts:10
Private namespace
namespace: string
Type : string
Defined in src/Locker.ts:11
Private separator
separator: string
Type : string
Defined in src/Locker.ts:12
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: IDriverType[], private lockerConfig: LockerConfig) {
    this.setNamespace()
    this.setSeparator()
    this.setDriverFallback()
  }

  public setNamespace(namespace: string = this.lockerConfig.driverNamespace) {
    this.namespace = namespace
  }

  public setSeparator(separator: string = this.lockerConfig.namespaceSeparator) {
    this.separator = separator
  }

  public setDriverFallback(driverFallback: DRIVERS | DRIVERS[] = this.lockerConfig.driverFallback) {
    this.driverFallback = driverFallback
  }

  public set(type: DRIVERS, key: string, data: any, config?: IStorageSetConfig) {
    this._getDriver(type).set(this._makeKey(key), data, config)
  }

  public get(type: DRIVERS, key: string) {
    return this._getDriver(type).get(this._makeKey(key))
  }

  public has(type: DRIVERS, key: string) {
    return this._getDriver(type).has(this._makeKey(key))
  }

  public remove(type: DRIVERS, key: string) {
    this._getDriver(type).remove(this._makeKey(key))
  }

  public key(type: DRIVERS, index?: number) {
    return this._decodeKey(this._getDriver(type).key(index))
  }

  public clear(type: DRIVERS) {
    this._getDriver(type).clear()
  }

  private _makeKey(key: string): string {
    return this.namespace ? `${this.namespace}${this.separator}${key}` : key
  }

  private _decodeKey(key: string): string {
    if (this.namespace)
      return key.slice(this.namespace.length + this.separator.length)
    else
      return key
  }

  private _getDriver(type: DRIVERS): Driver {
    const askedDriver = this._getDriverType(type)

    if (askedDriver && askedDriver.storage.isSupported())
      return askedDriver.storage
    else
      return this._getFallbackDriverType().storage
  }

  private _getDriverType(type: DRIVERS): IDriverType {
    return this.driverTypes.find((driverType) => driverType.type === type)
  }

  private _getFallbackDriverType(): IDriverType {
    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)
    }
  }
}

results matching ""

    No results matching ""