All files / src/types set.js

100% Statements 95/95
100% Branches 45/45
100% Functions 13/13
100% Lines 95/95

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 961x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 26x 1x 1x 1x 26x 16x 26x 1x 1x 5x 5x 5x 4x 5x 5x 5x 1x 1x 38x 38x 38x 38x 38x 10x 38x 1x 1x 16x 15x 15x 16x 1x 1x 31x 29x 31x 1x 1x 68x 68x 14x 68x 1x 1x 15x 14x 14x 14x 15x 1x 1x 16x 16x 1x 1x 13x 13x 1x 1x 1x 1x 1x 1x 2x 1x 3x 1x 1x 1x 1x 3x 1x 1x 1x 2x 1x  
import { InternalStaty, rawValue } from './internal.js'
import { actions, action } from '../action.js'
 
export class SetStaty extends InternalStaty {
  constructor (...args) {
    super(...args)
 
    this._reverse = new Map(this.source.entries())
    this._addHandler = this._addHandler.bind(this)
    this._deleteHandler = this._deleteHandler.bind(this)
    this._clearHandler = this._clearHandler.bind(this)
    this._hasHandler = this._hasHandler.bind(this)
  }
 
  forEach (callback) {
    this.target.forEach(callback)
  }
 
  onGetSnapshot (target, prop, value) {
    if (prop === 'add' || prop === 'delete' || prop === 'clear') {
      this.onReadOnly(target, prop, value)
      return () => {}
    }
    if (typeof value === 'function') return value.bind(target)
    return value
  }
 
  clone () {
    const x = this.target
    const tmp = new Set()
    x.forEach(function (val) {
      tmp.add(rawValue(val))
    })
    return tmp
  }
 
  handler (value, prop) {
    if (prop === 'add') return this._addHandler
    if (prop === 'delete') return this._deleteHandler
    if (prop === 'clear') return this._clearHandler
    if (prop === 'has') return this._hasHandler
    if (typeof value === 'function') return value.bind(this.target)
    return value
  }
 
  reflectSet (target, prop, value, useBaseReflect) {
    if (useBaseReflect) return Reflect.set(target, prop, value)
    this._reverse.set(prop, value)
    return !!target.add(value)
  }
 
  reflectHas (target, prop, useBaseReflect) {
    if (useBaseReflect) return Reflect.has(target, prop)
    return this._reverse.has(prop)
  }
 
  reflectGet (target, prop, useBaseReflect) {
    if (useBaseReflect) return Reflect.get(target, prop)
    if (this._reverse.has(prop)) return prop
    return undefined
  }
 
  reflectDeleteProperty (target, prop, useBaseReflect) {
    if (useBaseReflect) return Reflect.deleteProperty(target, prop)
    const val = this._reverse.get(prop)
    this._reverse.delete(prop)
    return target.delete(val)
  }
 
  _addHandler (val) {
    return this._set(this.target, val, val)
  }
 
  _deleteHandler (val) {
    return this._deleteProperty(this.target, val)
  }
 
  _hasHandler (val) {
    return this._reverse.has(val)
  }
 
  _clearHandler () {
    if (actions.current) {
      this.target.forEach(value => {
        this._deleteHandler(value)
      })
    } else {
      action(() => {
        this.target.forEach(value => {
          this._deleteHandler(value)
        })
      })
    }
  }
}