All files / src/types map.js

100% Statements 84/84
100% Branches 41/41
100% Functions 12/12
100% Lines 84/84

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 851x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 26x 1x 1x 1x 26x 16x 26x 1x 1x 5x 5x 5x 4x 5x 5x 5x 1x 1x 16x 15x 16x 1x 1x 30x 28x 30x 1x 1x 64x 28x 64x 1x 1x 13x 12x 13x 1x 1x 34x 34x 34x 34x 8x 34x 1x 1x 16x 16x 1x 1x 12x 12x 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 MapStaty extends InternalStaty {
  constructor (...args) {
    super(...args)
 
    this._setHandler = this._setHandler.bind(this)
    this._deleteHandler = this._deleteHandler.bind(this)
    this._clearHandler = this._clearHandler.bind(this)
  }
 
  forEach (callback) {
    this.target.forEach(callback)
  }
 
  onGetSnapshot (target, prop, value) {
    if (prop === 'set' || 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 Map()
    x.forEach(function (val, key) {
      tmp.set(key, rawValue(val))
    })
    return tmp
  }
 
  reflectSet (target, prop, value, useBaseReflect) {
    if (useBaseReflect) return Reflect.set(target, prop, value)
    return !!target.set(prop, value)
  }
 
  reflectHas (target, prop, useBaseReflect) {
    if (useBaseReflect) return Reflect.has(target, prop)
    return target.has(prop)
  }
 
  reflectGet (target, prop, useBaseReflect) {
    if (useBaseReflect) return Reflect.get(target, prop)
    return target.get(prop)
  }
 
  reflectDeleteProperty (target, prop, useBaseReflect) {
    if (useBaseReflect) return Reflect.deleteProperty(target, prop)
    return target.delete(prop)
  }
 
  handler (value, prop) {
    if (prop === 'set') return this._setHandler
    if (prop === 'delete') return this._deleteHandler
    if (prop === 'clear') return this._clearHandler
    if (typeof value === 'function') return value.bind(this.target)
    return value
  }
 
  _setHandler (key, val) {
    return this._set(this.target, key, val)
  }
 
  _deleteHandler (key) {
    return this._deleteProperty(this.target, key)
  }
 
  _clearHandler () {
    if (actions.current) {
      this.target.forEach((_, key) => {
        this._deleteHandler(key)
      })
    } else {
      action(() => {
        this.target.forEach((_, key) => {
          this._deleteHandler(key)
        })
      })
    }
  }
}