All files / src/types internal.js

100% Statements 344/344
100% Branches 130/130
100% Functions 29/29
100% Lines 344/344

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 3451x 1x 1x 1x 1x 126x 126x 126x 126x 126x 5x 126x 1x 1x 149x 79x 79x 149x 73x 54x 54x 6x 6x 149x 1x 1x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 1x 1x 8x 16x 16x 8x 1x 1x 105x 105x 1x 1x 2x 2x 2x 2x 2x 1x 1x 23x 23x 23x 19x 23x 1x 1x 105x 68x 68x 68x 314x 314x 314x 20x 20x 224x 68x 68x 3x 3x 3x 68x 68x 105x 1x 1x 130x 130x 1x 1x 48x 48x 48x 48x 121x 121x 48x 48x 1x 1x 269x 269x 1x 1x 178x 178x 1x 1x 22x 22x 22x 6x 6x 6x 6x 6x 6x 6x 16x 16x 10x 10x 10x 10x 16x 22x 1x 1x 72x 72x 2x 72x 70x 70x 70x 72x 72x 12x 12x 66x 66x 72x 1x 1x 18x 18x 18x 18x 1x 1x 236x 236x 236x 236x 236x 236x 90x 90x 90x 236x 236x 236x 236x 236x 2x 2x 236x 236x 236x 236x 226x 38x 19x 24x 24x 24x 19x 19x 38x 226x 236x 236x 136x 133x 133x 236x 236x 236x 94x 94x 95x 94x 236x 236x 236x 236x 9x 9x 9x 236x 1x 1x 994x 994x 1x 1x 98x 98x 1x 1x 13x 13x 1x 1x 92x 92x 1x 1x 107x 107x 1x 1x 363x 363x 1x 1x 8x 8x 1x 1x 994x 307x 307x 994x 299x 299x 994x 994x 5x 5x 294x 294x 994x 1x 1x 130x 130x 130x 130x 130x 130x 130x 130x 6x 5x 5x 5x 1x 1x 5x 5x 130x 4x 4x 2x 2x 1x 4x 4x 4x 4x 120x 120x 130x 103x 103x 103x 130x 93x 93x 10x 10x 10x 93x 83x 83x 83x 18x 18x 18x 6x 6x 6x 12x 12x 83x 83x 83x 83x 93x 20x 20x 20x 20x 20x 14x 14x 14x 14x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 2x 3x 3x 14x 14x 14x 130x 1x 1x 38x 38x 35x 35x 38x 38x 38x 35x 7x 7x 7x 35x 35x 33x 38x 1x  
import { kEmpty, kInternalAction, kStaty, isValidForStaty } from '../constants.js'
import { actions } from '../action.js'
import { cloneStructures } from '../clone.js'
import { createProxy } from '../proxy.js'
 
function toString (value) {
  const type = typeof value
  if (type === 'string') return value
  if (type === 'object') return '?'
  if (type === 'symbol') return value.toString()
  return String(value)
}
 
export const rawValue = (value) => {
  if (!value || typeof value !== 'object') return value
 
  const staty = value?.[kStaty]
  if (staty) {
    if (staty.isRef) return value
    return staty.getSnapshot()
  }
 
  return cloneStructures(value, Object.prototype.toString.call(value))
}
export class InternalStaty {
  constructor (source, target, { onReadOnly }) {
    this.source = source
    this.target = target
    this.onReadOnly = onReadOnly
    this.subscriptions = {
      default: new Set(),
      props: new Map()
    }
    this.propsBinded = new Map()
    this.isRef = false
    this.onGetSnapshot = this.onGetSnapshot.bind(this)
    this._snapshot = kEmpty
    /** @type {Proxy<typeof target>} */
    this.proxy = null
  }
 
  forEach (callback) {
    for (const prop in this.target) {
      callback(this.target[prop], prop)
    }
  }
 
  setProxy (proxy) {
    this.proxy = proxy
  }
 
  setOnAction (onAction) {
    this.onAction = {
      run: actionName => onAction(this.proxy, actionName),
      before: true
    }
  }
 
  getValueByProp (prop) {
    const value = this.reflectGet(this.target, prop)
    const staty = value?.[kStaty]
    if (staty && staty.isRef) return staty.getSnapshot()
    return value
  }
 
  getSnapshot () {
    if (this._snapshot !== kEmpty) return this._snapshot
    const internal = this
    const snapshot = this._snapshot = new Proxy(this.clone(), {
      get (target, prop) {
        const value = Reflect.get(target, prop)
        if (prop === 'constructor') return value
        if (value?.[kStaty]) {
          return value[kStaty].getSnapshot()
        }
        return internal.onGetSnapshot(target, prop, value)
      },
      set (target, prop, value) {
        internal.onReadOnly(target, prop, value)
        return true
      }
    })
    return snapshot
  }
 
  onGetSnapshot (target, prop, value) {
    return value
  }
 
  clone () {
    let k
    const x = this.target
    const tmp = Object.create(Object.getPrototypeOf(x))
    for (k in x) {
      tmp[k] = rawValue(x[k])
    }
    return tmp
  }
 
  clearSnapshot () {
    this._snapshot = kEmpty
  }
 
  handler (value, prop) {
    return value
  }
 
  checkCircularReference (prop, value) {
    prop = toString(prop)
 
    if (this === value) {
      const err = new Error('circular reference detected')
      // @ts-ignore
      err.location = `^${prop}`
      // @ts-ignore
      err.value = value.getSnapshot()
      throw err
    }
 
    this.propsBinded.forEach((parents, propBinded) => {
      propBinded = toString(propBinded)
      parents.forEach(parent => {
        parent.checkCircularReference(`${propBinded}.${prop}`, value)
      })
    })
  }
 
  addParent (prop, parent, checkCircularReference) {
    let parents
    if (this.propsBinded.has(prop)) {
      parents = this.propsBinded.get(prop)
    } else {
      parents = new Set()
      this.propsBinded.set(prop, parents)
    }
 
    if (checkCircularReference) {
      parent.checkCircularReference(prop, this)
    }
 
    parents.add(parent)
  }
 
  delParent (prop, parent) {
    const parents = this.propsBinded.get(prop)
    parents.delete(parent)
    if (parents.size === 0) this.propsBinded.delete(prop)
  }
 
  run (prop, rollback) {
    let action = actions.current
 
    this.clearSnapshot()
 
    let actionInitiator = false
    if (rollback && (!action || action.name === kInternalAction)) {
      action = actions.create(kInternalAction)
      actionInitiator = true
    }
 
    try {
      if (rollback) action.pushHistory(rollback)
 
      if (this.onAction) {
        action.add(this.onAction)
      }
 
      const subscriptions = this.subscriptions
 
      if (prop) {
        for (const [key, handlers] of subscriptions.props.entries()) {
          if (prop.startsWith(`${key}.`) || prop === key) {
            Array.from(handlers.values()).forEach(handler => {
              if (action.valid(handler)) {
                action.add(handler)
              }
            })
          }
        }
      }
 
      Array.from(subscriptions.default.values()).forEach(handler => {
        if (action.valid(handler)) {
          action.add(handler)
        }
      })
 
      this.propsBinded.forEach((parents, propBinded) => {
        propBinded = toString(propBinded)
        parents.forEach(parent => {
          parent.run(prop ? `${propBinded}.${prop}` : propBinded)
        })
      })
 
      actionInitiator && action.done()
    } catch (err) {
      actionInitiator && action.cancel()
      throw err
    }
  }
 
  get (target, prop) {
    return this._get(target, prop, true)
  }
 
  set (target, prop, value) {
    return this._set(target, prop, value, true)
  }
 
  deleteProperty (target, prop) {
    return this._deleteProperty(target, prop, true)
  }
 
  reflectSet (target, prop, value, useBaseReflect) {
    return Reflect.set(target, prop, value)
  }
 
  reflectHas (target, prop, useBaseReflect) {
    return Reflect.has(target, prop)
  }
 
  reflectGet (target, prop, useBaseReflect) {
    return Reflect.get(target, prop)
  }
 
  reflectDeleteProperty (target, prop, useBaseReflect) {
    return Reflect.deleteProperty(target, prop)
  }
 
  _get (target, prop, useBaseReflect) {
    if (prop === kStaty) return this
 
    const value = this.reflectGet(target, prop, useBaseReflect)
    if (value === null || value === undefined) return value
 
    const valueStaty = value?.[kStaty]
 
    if (valueStaty?.isRef) {
      return valueStaty.value
    }
 
    return this.handler(value, prop)
  }
 
  _set (target, prop, value, useBaseReflect) {
    const internal = this
    const newProp = !internal.reflectHas(target, prop, useBaseReflect)
    const oldValue = internal.reflectGet(target, prop, useBaseReflect)
    const oldValueStaty = oldValue?.[kStaty]
    let valueStaty = value?.[kStaty]
 
    // start ref support
    if (oldValueStaty?.isRef && !valueStaty?.isRef) {
      if (oldValue === value || oldValueStaty.value === value) return true
      const oldRawValue = oldValueStaty.value
      oldValueStaty.setValue(value)
      internal.run(prop, () => {
        internal.clearSnapshot()
        oldValueStaty.setValue(oldRawValue)
      })
      return true
    } else if (valueStaty?.isRef) {
      if (internal.reflectSet(target, prop, value, useBaseReflect)) {
        internal.run(prop, () => {
          internal.clearSnapshot()
          if (newProp) return internal.reflectDeleteProperty(target, prop, useBaseReflect)
          internal.reflectSet(target, prop, oldValue, useBaseReflect)
        })
      }
      return true
    }
    // end ref support
 
    if (oldValue === value || (oldValueStaty && oldValueStaty.source === value)) return true
 
    let checkCircularReference = true
    let type
    if (!valueStaty) {
      type = Object.prototype.toString.call(value)
      if (isValidForStaty(type)) {
        value = createProxy({ onReadOnly: internal.onReadOnly }, value, null, null, type)
        valueStaty = value[kStaty]
        checkCircularReference = false
      } else {
        internal.reflectSet(target, prop, value, useBaseReflect)
 
        internal.run(prop, () => {
          internal.clearSnapshot()
 
          if (newProp) {
            internal.reflectDeleteProperty(target, prop, useBaseReflect)
            return
          }
 
          internal.reflectSet(target, prop, oldValue, useBaseReflect)
        })
 
        return true
      }
    }
 
    if (oldValueStaty !== valueStaty) {
      valueStaty?.addParent(prop, internal, checkCircularReference)
      oldValueStaty?.delParent(prop, internal)
    }
 
    internal.reflectSet(target, prop, value, useBaseReflect)
 
    internal.run(prop, () => {
      internal.clearSnapshot()
 
      if (oldValueStaty !== valueStaty) {
        oldValueStaty?.addParent(prop, internal)
        valueStaty?.delParent(prop, internal)
      }
 
      if (newProp) {
        internal.reflectDeleteProperty(target, prop, useBaseReflect)
        return
      }
 
      internal.reflectSet(target, prop, oldValue, useBaseReflect)
    })
 
    return true
  }
 
  _deleteProperty (target, prop, useBaseReflect) {
    const internal = this
    if (!internal.reflectHas(target, prop, useBaseReflect)) return true
 
    const oldValue = internal.reflectGet(target, prop, useBaseReflect)
    oldValue?.[kStaty]?.delParent?.(prop, internal)
 
    if (internal.reflectDeleteProperty(target, prop, useBaseReflect)) {
      internal.run(prop, () => {
        internal.clearSnapshot()
        oldValue?.[kStaty]?.addParent?.(prop, internal)
        internal.reflectSet(target, prop, oldValue, useBaseReflect)
      })
    }
    return true
  }
}