All files / src/operations get_item.js

100% Statements 42/42
91.67% Branches 11/12
100% Functions 5/5
100% Lines 42/42

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        5x 5x   5x     3x 3x 3x 9x 9x     3x       14x 14x 14x 14x 7x 7x 7x 7x 1x 1x       13x 13x   13x 3x   3x   2x 2x 2x 2x 2x 1x 1x     1x 1x   1x 1x 1x 1x 1x     1x 1x 1x       10x      
export default {
  // NOTE: SimpleCache will use waterfall checks to find values
  // Session storage is more fragile so it will be checked first
  single: function ({ key, local, session, log }) {
    log(`Get Single`)
    log(`Key ${key}`)
 
    return this._waterfallLogic({ key, local, session, log })
  },
  multi: function ({ keys, local, session, log }) {
    log(`Get Multi`)
    const promises = []
    keys.forEach(key =>
      promises.push(new Promise(resolve => {
        resolve(this._waterfallLogic({ key, local, session, log }))
      }))
    )
    return Promise.all(promises)
  },
  _waterfallLogic: function ({ key, local, session, log }) {
    // TODO: Come back and clean this function up
    log(`Checking session...`)
    let foundIn = 'session'
    let item = session.get(key)
    if (!item) {
      log(`Checking local...`)
      foundIn = 'local'
      item = local.get(key)
      if (!item) {
        log(`Nothing Found`)
        return null
      }
    }
 
    log(`Item Found`)
    item = JSON.parse(item)
 
    if (Date.now() >= item.ttl) {
      log(`Item Expired`)
 
      if (foundIn === 'session') {
        // Retry in local
        log(`Removing session cache`)
        session.remove(key)
        log(`Checking local...`)
        item = local.get(key)
        if (!item) {
          log(`Nothing Found`)
          return null
        }
 
        log(`Item Found`)
        item = JSON.parse(item)
 
        Eif (Date.now() >= item.ttl) {
          log(`Item Expired`)
          log(`Removing local cache`)
          local.remove(key)
          return null
        }
      } else {
        log(`Removing local cache`)
        local.remove(key)
        return null
      }
    }
 
    return item.value
  }
}