All files / src proxy.js

100% Statements 64/64
100% Branches 26/26
100% Functions 2/2
100% Lines 64/64

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 651x 1x 1x 1x 1x 1x 1x 105x 105x 105x 105x 105x 1x 1x 213x 126x 126x 213x 15x 15x 15x 111x 213x 213x 213x 213x 213x 213x 213x 78x 78x 78x 116x 116x 213x 12x 12x 12x 12x 25x 25x 33x 8x 8x 8x 4x 8x 21x 7x 7x 7x 4x 7x 13x 6x 6x 111x 213x 105x 105x 105x 6x 6x 213x  
import { kStaty, isObject, isArray, isSet, isMap } from './constants.js'
import { InternalStaty } from './types/internal.js'
import { ArrayStaty } from './types/array.js'
import { MapStaty } from './types/map.js'
import { SetStaty } from './types/set.js'
import { cloneStructures } from './clone.js'
 
function _createProxy (internal) {
  const state = new Proxy(internal.target, internal)
  internal.setProxy(state)
  return state
}
 
export function createProxy (proxyOptions, x, p, pk, type) {
  if (!x || typeof x !== 'object') return x
 
  const staty = x?.[kStaty]
  if (staty) {
    if (p && !staty.isRef) staty.addParent(pk, p[kStaty], true)
    return x
  }
 
  const str = type || Object.prototype.toString.call(x)
 
  let k
  let tmp
  let proxy
 
  if (str === isObject) {
    tmp = Object.create(Object.getPrototypeOf(x)) // null
    proxy = _createProxy(new InternalStaty(x, tmp, proxyOptions))
    for (k in x) {
      tmp[k] = createProxy(proxyOptions, x[k], proxy, k)
    }
  } else if (str === isArray) {
    k = x.length
    tmp = Array(k)
    proxy = _createProxy(new ArrayStaty(x, tmp, proxyOptions))
    for (; k--;) {
      tmp[k] = createProxy(proxyOptions, x[k], proxy, k)
    }
  } else if (str === isSet) {
    tmp = new Set()
    proxy = _createProxy(new SetStaty(x, tmp, proxyOptions))
    x.forEach(function (val) {
      tmp.add(createProxy(proxyOptions, val, proxy, val))
    })
  } else if (str === isMap) {
    tmp = new Map()
    proxy = _createProxy(new MapStaty(x, tmp, proxyOptions))
    x.forEach(function (val, key) {
      tmp.set(createProxy(proxyOptions, key, proxy, key), createProxy(proxyOptions, val, proxy, key))
    })
  } else {
    tmp = cloneStructures(x, str)
  }
 
  if (proxy) {
    if (p) proxy[kStaty].addParent(pk, p[kStaty], false)
    return proxy
  }
 
  return tmp
}