File size: 1,447 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { DevtoolsApi } from '@vue-devtools/app-backend-api'
import { HookEvents, SharedData } from '@vue-devtools/shared-utils'
import throttle from 'lodash/throttle'
import { getUniqueId } from './util.js'

export function initUpdateTracking(api: DevtoolsApi, Vue) {
  // Global mixin
  Vue.mixin({
    beforeCreate() {
      applyTrackingUpdateHook(api, this)
    },
  })
}

const COMPONENT_HOOKS = [
  'created',
  'updated',
]

export function applyTrackingUpdateHook(api: DevtoolsApi, vm) {
  if (vm.$options.$_devtoolsUpdateTrackingHooks) {
    return
  }
  vm.$options.$_devtoolsUpdateTrackingHooks = true

  const handler = throttle(async function (this: any) {
    if (SharedData.trackUpdates) {
      api.ctx.hook.emit(HookEvents.TRACK_UPDATE, getUniqueId(this), api.ctx)

      const parents = await api.walkComponentParents(this)
      for (const parent of parents) {
        api.ctx.hook.emit(HookEvents.TRACK_UPDATE, getUniqueId(parent), api.ctx)
      }
    }

    if (SharedData.flashUpdates) {
      api.ctx.hook.emit(HookEvents.FLASH_UPDATE, this, api.backend)
    }
  }, 100)
  for (const hook of COMPONENT_HOOKS) {
    const currentValue = vm.$options[hook]
    if (Array.isArray(currentValue)) {
      vm.$options[hook] = [handler, ...currentValue]
    }
    else if (typeof currentValue === 'function') {
      vm.$options[hook] = [handler, currentValue]
    }
    else {
      vm.$options[hook] = [handler]
    }
  }
}