# All files / src/instance expose.ts

22.22% Statements 8/36
12.5% Branches 3/24
5.88% Functions 1/17
22.22% Lines 8/36

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
    1x
    1x

    20x
    20x

    1x

    1x
    1x

    1x

     export function initExposed(vm) {
      const expose = vm.$options.expose || {}
      if (Array.isArray(expose)) {
        if (expose.length) {
          const exposed = vm._exposed || (vm._exposed = {})
        I  expose.forEach((key) => {
            Object.defineProperty(exposed, key, {
              get: () => vm[key],
              set: (val) => (vm[key] = val)
            })
          })
          return exposed
        } else if (!vm._exposed) {
          vm._exposed = {}
        }
      }
    }

    export function getPublicInstance(vm) {
      if (!vm) return null
      return getExposeProxy(vm) || vm
    }

    export type PublicPropertiesMap = Record<
      string,
      (i) => any
    >

    export const publicPropertiesMap: PublicPropertiesMap = {
      $data: (vm) => vm.$data,
      $props: (vm) => vm.$props,
      $refs: (vm) => vm.$refs,
      $parent: (vm) => vm.$parent,
      $root: (vm) => vm.$root,
      $children: (vm) => vm.$children,
      $options: (vm) => vm.$options,
      $emit: (vm) => vm.$emit,
      $nextTick: (vm) => vm.$nextTick,
    }

    export function getExposeProxy(vm) {
      if (vm._exposed) {
        return (
          vm._exposeProxy ||
          (vm._exposeProxy = new Proxy(vm._exposed, {
            get(target, key: string) {
              if (key in target) {
                return target[key]
              } else if (key in publicPropertiesMap) {
                return publicPropertiesMap[key](vm)
              }
            },
            has(target, key: string) {
              return key in target || key in publicPropertiesMap
            },
          }))
        )
      }
    }