All files / src/types ref.js

100% Statements 33/33
100% Branches 12/12
100% Functions 4/4
100% Lines 33/33

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 341x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 1x 1x 30x 30x 30x 1x 1x 24x 24x 22x 24x 24x 1x 1x 52x 52x 52x 27x 27x 52x 1x  
const kValue = Symbol('valueRef')
 
const kMapSnapshot = Symbol('kMapSnapshot')
const kSnapshot = Symbol('kSnapshot')
export class RefStaty {
  constructor (value, mapSnapshot, cache = false) {
    this.isRef = true
    this.cache = cache
    this[kMapSnapshot] = mapSnapshot
    this[kSnapshot] = null
    this.setValue(value)
  }
 
  setValue (value) {
    this.value = value
    this.updateSnapshot()
  }
 
  getSnapshot () {
    const snapshot = this[kSnapshot]
    if (this.cache) return snapshot === kValue ? this.value : snapshot
    this.updateSnapshot()
    return snapshot === kValue ? this.value : snapshot
  }
 
  updateSnapshot () {
    this[kSnapshot] = kValue
    const mapSnapshot = this[kMapSnapshot]
    if (mapSnapshot) {
      this[kSnapshot] = mapSnapshot(this.value)
    }
  }
}