Skip to content

Commit fb6a2ac

Browse files
committed
feat(vue2): update tracking
1 parent 8b7c4af commit fb6a2ac

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

packages/app-backend-core/src/component.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,8 @@ export async function refreshComponentTreeSearch (ctx: BackendContext) {
131131
await sendComponentTreeData(ctx.currentAppRecord, '_root', ctx.currentAppRecord.componentFilter, null, ctx)
132132
}
133133

134-
export async function sendComponentUpdateTracking (appRecord: AppRecord, instanceId: string, ctx: BackendContext) {
134+
export async function sendComponentUpdateTracking (instanceId: string, ctx: BackendContext) {
135135
if (!instanceId) return
136-
const instance = getComponentInstance(appRecord, instanceId, ctx)
137-
if (!instance) return
138136
const payload = {
139137
instanceId,
140138
time: Date.now(), // Use normal date

packages/app-backend-core/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async function connect () {
134134
}
135135

136136
if (SharedData.trackUpdates) {
137-
sendComponentUpdateTracking(appRecord, id, ctx)
137+
sendComponentUpdateTracking(id, ctx)
138138
}
139139
} catch (e) {
140140
if (SharedData.debugInfo) {
@@ -169,7 +169,7 @@ async function connect () {
169169
}
170170

171171
if (SharedData.trackUpdates) {
172-
sendComponentUpdateTracking(appRecord, parentId, ctx)
172+
sendComponentUpdateTracking(parentId, ctx)
173173
}
174174
}
175175
}
@@ -180,7 +180,7 @@ async function connect () {
180180
}
181181

182182
if (SharedData.trackUpdates) {
183-
sendComponentUpdateTracking(appRecord, id, ctx)
183+
sendComponentUpdateTracking(id, ctx)
184184
}
185185

186186
await refreshComponentTreeSearch(ctx)

packages/app-backend-vue2/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
},
1212
"dependencies": {
1313
"@vue-devtools/app-backend-api": "^0.0.0",
14+
"@vue-devtools/app-backend-core": "^0.0.0",
1415
"@vue-devtools/shared-utils": "^0.0.0",
1516
"@vue/devtools-api": "^6.0.0-beta.7",
16-
"clone-deep": "^4.0.1"
17+
"clone-deep": "^4.0.1",
18+
"lodash": "^4.17.21"
1719
},
1820
"devDependencies": {
1921
"@types/node": "^13.9.1",

packages/app-backend-vue2/src/components/tree.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { classify, kebabize } from '@vue-devtools/shared-utils'
33
import { ComponentTreeNode, ComponentInstance } from '@vue/devtools-api'
44
import { getRootElementsFromComponentInstance } from './el'
55
import { applyPerfHooks } from './perf.js'
6+
import { applyTrackingUpdateHook } from './update-tracking.js'
67
import { getInstanceName, getRenderKey, getUniqueId, isBeingDestroyed } from './util'
78

89
export let instanceMap: Map<any, any>
@@ -326,6 +327,7 @@ function mark (instance) {
326327
instanceMap.delete(refId)
327328
})
328329
applyPerfHooks(api, instance, appRecord.options.app)
330+
applyTrackingUpdateHook(api, instance)
329331
}
330332
}
331333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

packages/app-backend-vue2/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { editState, getCustomInstanceDetails, getInstanceDetails } from './compo
55
import { getInstanceOrVnodeRect, findRelatedComponent, getRootElementsFromComponentInstance } from './components/el'
66
import { initPerf } from './components/perf.js'
77
import { getComponentParents, instanceMap, walkTree } from './components/tree'
8+
import { initUpdateTracking } from './components/update-tracking.js'
89
import { getInstanceName } from './components/util'
910
import { wrapVueForEvents } from './events'
1011
import { setupPlugin } from './plugin'
@@ -95,6 +96,8 @@ export const backend = defineBackend({
9596

9697
// Perf
9798
initPerf(api, app, Vue)
99+
// Update tracking
100+
initUpdateTracking(api, Vue)
98101
},
99102
})
100103

0 commit comments

Comments
 (0)