|
| 1 | +import { DevtoolsApi } from '@vue-devtools/app-backend-api' |
| 2 | +import { SharedData } from '@vue-devtools/shared-utils' |
| 3 | +import { sendComponentUpdateTracking } from '@vue-devtools/app-backend-core/lib/component' |
| 4 | +import throttle from 'lodash/throttle' |
| 5 | +import { getUniqueId } from './util.js' |
| 6 | + |
| 7 | +export function initUpdateTracking (api: DevtoolsApi, Vue) { |
| 8 | + // Global mixin |
| 9 | + Vue.mixin({ |
| 10 | + beforeCreate () { |
| 11 | + applyTrackingUpdateHook(api, this) |
| 12 | + }, |
| 13 | + }) |
| 14 | +} |
| 15 | + |
| 16 | +const COMPONENT_HOOKS = [ |
| 17 | + 'created', |
| 18 | + 'updated', |
| 19 | +] |
| 20 | + |
| 21 | +export function applyTrackingUpdateHook (api: DevtoolsApi, vm) { |
| 22 | + if (vm.$options.$_devtoolsUpdateTrackingHooks) return |
| 23 | + vm.$options.$_devtoolsUpdateTrackingHooks = true |
| 24 | + |
| 25 | + const handler = throttle(async function (this: any) { |
| 26 | + if (SharedData.trackUpdates) { |
| 27 | + sendComponentUpdateTracking(getUniqueId(this), api.ctx) |
| 28 | + |
| 29 | + const parents = await api.walkComponentParents(this) |
| 30 | + for (const parent of parents) { |
| 31 | + sendComponentUpdateTracking(getUniqueId(parent), api.ctx) |
| 32 | + } |
| 33 | + } |
| 34 | + }, 100) |
| 35 | + for (const hook of COMPONENT_HOOKS) { |
| 36 | + const currentValue = vm.$options[hook] |
| 37 | + if (Array.isArray(currentValue)) { |
| 38 | + vm.$options[hook] = [handler, ...currentValue] |
| 39 | + } else if (typeof currentValue === 'function') { |
| 40 | + vm.$options[hook] = [handler, currentValue] |
| 41 | + } else { |
| 42 | + vm.$options[hook] = [handler] |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments