All files objectStorageCache.js

96.67% Statements 29/30
95.45% Branches 21/22
100% Functions 7/7
96.67% Lines 29/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102        42x 1x         41x 1x         40x 1x         39x 1x         38x 1x         37x           37x 37x       3x           3x       311x 86x         311x       109x 70x           109x       2x 1x     2x       2x 2x       32x   32x 2x                  
import { InStorageCacheError, validStorage, toObject } from './utils'
 
class ObjectStorageCache {
  constructor (data = {}, persistence = {}) {
    if (!persistence.storage) {
      throw new InStorageCacheError(
        'You must provide a persistence.storage to use'
      )
    }
 
    if (!validStorage(persistence.storage)) {
      throw new InStorageCacheError(
        'You must provide a valid persistence.storage to use'
      )
    }
 
    if (typeof persistence.normalize !== 'function') {
      throw new InStorageCacheError(
        'You must provide a persistence.normalize function'
      )
    }
 
    if (typeof persistence.denormalize !== 'function') {
      throw new InStorageCacheError(
        'You must provide a persistence.denormalize function'
      )
    }
 
    if (typeof persistence.shouldPersist !== 'function') {
      throw new InStorageCacheError(
        'You must provide a persistence.shouldPersist function'
      )
    }
 
    Iif (typeof persistence.prefix !== 'string') {
      throw new InStorageCacheError(
        'You must provide a persistence.prefix string'
      )
    }
 
    this.persistence = persistence
    this.data = { ...data }
  }
 
  toObject () {
    const persisted = toObject(
      this.persistence.storage,
      this.persistence.denormalize,
      this.persistence.prefix
    )
 
    return { ...persisted, ...this.data }
  }
 
  get (dataId) {
    if (!this.data[dataId] && this.persistence.shouldPersist('get', dataId)) {
      this.data[dataId] = this.persistence.denormalize(
        this.persistence.storage.getItem(`${this.persistence.prefix}${dataId}`)
      )
    }
 
    return this.data[dataId]
  }
 
  set (dataId, value) {
    if (this.persistence.shouldPersist('set', dataId, value)) {
      this.persistence.storage.setItem(
        `${this.persistence.prefix}${dataId}`,
        this.persistence.normalize(value)
      )
    }
 
    this.data[dataId] = value
  }
 
  delete (dataId) {
    if (this.persistence.shouldPersist('delete', dataId)) {
      this.persistence.storage.removeItem(`${this.persistence.prefix}${dataId}`)
    }
 
    this.data[dataId] = undefined
  }
 
  clear () {
    this.persistence.storage.clear()
    this.data = {}
  }
 
  replace (newData) {
    this.data = {}
 
    for (let dataId in newData) {
      this.set(dataId, {
        ...this.get(dataId),
        ...newData[dataId],
      })
    }
  }
}
 
export { ObjectStorageCache }