-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9885469
commit 8acab1d
Showing
10 changed files
with
283 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import type { DevToolsV6PluginAPI } from '../../api/v6' | ||
import { isBrowser } from '@vue/devtools-shared' | ||
import { getAppRecord, getInstanceName } from '../../core/component/utils' | ||
import { hook } from '../../hook' | ||
import { PERFORMANCE_EVENT_LAYER_ID, performanceMarkEnd, performanceMarkStart } from './perf' | ||
|
||
const COMPONENT_EVENT_LAYER_ID = 'component-event' | ||
|
||
export function setupBuiltinTimelineLayers(api: DevToolsV6PluginAPI) { | ||
if (!isBrowser) | ||
return | ||
|
||
// Mouse events timeline layer | ||
|
||
api.addTimelineLayer({ | ||
id: 'mouse', | ||
label: 'Mouse', | ||
color: 0xA451AF, | ||
}) | ||
|
||
;(['mousedown', 'mouseup', 'click', 'dblclick'] as const).forEach((eventType) => { | ||
window.addEventListener(eventType, async (event: MouseEvent) => { | ||
await api.addTimelineEvent({ | ||
layerId: 'mouse', | ||
event: { | ||
time: Date.now(), | ||
data: { | ||
type: eventType, | ||
x: event.clientX, | ||
y: event.clientY, | ||
}, | ||
title: eventType, | ||
}, | ||
}) | ||
}, { | ||
capture: true, | ||
passive: true, | ||
}) | ||
}) | ||
|
||
// Keyboard events timeline layer | ||
|
||
api.addTimelineLayer({ | ||
id: 'keyboard', | ||
label: 'Keyboard', | ||
color: 0x8151AF, | ||
}) | ||
|
||
;(['keyup', 'keydown', 'keypress'] as const).forEach((eventType) => { | ||
window.addEventListener(eventType, async (event: KeyboardEvent) => { | ||
await api.addTimelineEvent({ | ||
layerId: 'keyboard', | ||
event: { | ||
time: Date.now(), | ||
data: { | ||
type: eventType, | ||
key: event.key, | ||
ctrlKey: event.ctrlKey, | ||
shiftKey: event.shiftKey, | ||
altKey: event.altKey, | ||
metaKey: event.metaKey, | ||
}, | ||
title: event.key, | ||
}, | ||
}) | ||
}, { | ||
capture: true, | ||
passive: true, | ||
}) | ||
}) | ||
|
||
// Component events timeline layer | ||
|
||
api.addTimelineLayer({ | ||
id: COMPONENT_EVENT_LAYER_ID, | ||
label: 'Component events', | ||
color: 0x4FC08D, | ||
}) | ||
|
||
hook.on.componentEmit(async (app, instance, event, params) => { | ||
const appRecord = await getAppRecord(app) | ||
|
||
if (!appRecord) | ||
return | ||
|
||
const componentId = `${appRecord.id}:${instance.uid}` | ||
const componentName = getInstanceName(instance) || 'Unknown Component' | ||
|
||
api.addTimelineEvent({ | ||
layerId: COMPONENT_EVENT_LAYER_ID, | ||
event: { | ||
time: Date.now(), | ||
data: { | ||
component: { | ||
_custom: { | ||
type: 'component-definition', | ||
display: componentName, | ||
}, | ||
}, | ||
event, | ||
params, | ||
}, | ||
title: event, | ||
subtitle: `by ${componentName}`, | ||
meta: { | ||
componentId, | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
// Performance timeline layer | ||
|
||
api.addTimelineLayer({ | ||
id: 'performance', | ||
label: PERFORMANCE_EVENT_LAYER_ID, | ||
color: 0x41B86A, | ||
}) | ||
|
||
hook.on.perfStart((app, uid, vm, type, time) => { | ||
performanceMarkStart(api, app, uid, vm, type, time) | ||
}) | ||
|
||
hook.on.perfEnd((app, uid, vm, type, time) => { | ||
performanceMarkEnd(api, app, uid, vm, type, time) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { App } from 'vue' | ||
import type { DevToolsV6PluginAPI } from '../../api/v6' | ||
import type { VueAppInstance } from '../../types' | ||
import { getAppRecord, getInstanceName } from '../../core/component/utils' | ||
import { devtoolsState } from '../../ctx/state' | ||
|
||
type HookAppInstance = App & VueAppInstance | ||
const markEndQueue = new Map<string, { | ||
app: App | ||
uid: number | ||
instance: HookAppInstance | ||
type: string | ||
time: number | ||
}>() | ||
export const PERFORMANCE_EVENT_LAYER_ID = 'performance' | ||
|
||
export async function performanceMarkStart(api: DevToolsV6PluginAPI, app: App, uid: number, vm: HookAppInstance, type: string, time: number) { | ||
const appRecord = await getAppRecord(app) | ||
if (!appRecord) { | ||
return | ||
} | ||
const componentName = getInstanceName(vm) || 'Unknown Component' | ||
const groupId = devtoolsState.perfUniqueGroupId++ | ||
const groupKey = `${uid}-${type}` | ||
appRecord.perfGroupIds.set(groupKey, { groupId, time }) | ||
await api.addTimelineEvent({ | ||
layerId: PERFORMANCE_EVENT_LAYER_ID, | ||
event: { | ||
time: Date.now(), | ||
data: { | ||
component: componentName, | ||
type, | ||
measure: 'start', | ||
}, | ||
title: componentName, | ||
subtitle: type, | ||
groupId, | ||
}, | ||
}) | ||
if (markEndQueue.has(groupKey)) { | ||
const { | ||
app, | ||
uid, | ||
instance, | ||
type, | ||
time, | ||
} = markEndQueue.get(groupKey)! | ||
markEndQueue.delete(groupKey) | ||
await performanceMarkEnd( | ||
api, | ||
app, | ||
uid, | ||
instance, | ||
type, | ||
time, | ||
) | ||
} | ||
} | ||
|
||
export function performanceMarkEnd(api: DevToolsV6PluginAPI, app: App, uid: number, vm: HookAppInstance, type: string, time: number) { | ||
const appRecord = getAppRecord(app) | ||
if (!appRecord) | ||
return | ||
|
||
const componentName = getInstanceName(vm) || 'Unknown Component' | ||
const groupKey = `${uid}-${type}` | ||
const groupInfo = appRecord.perfGroupIds.get(groupKey) | ||
if (groupInfo) { | ||
const groupId = groupInfo.groupId | ||
const startTime = groupInfo.time | ||
const duration = time - startTime | ||
api.addTimelineEvent({ | ||
layerId: PERFORMANCE_EVENT_LAYER_ID, | ||
event: { | ||
time: Date.now(), | ||
data: { | ||
component: componentName, | ||
type, | ||
measure: 'end', | ||
duration: { | ||
_custom: { | ||
type: 'Duration', | ||
value: duration, | ||
display: `${duration} ms`, | ||
}, | ||
}, | ||
}, | ||
title: componentName, | ||
subtitle: type, | ||
groupId, | ||
}, | ||
}) | ||
} | ||
else { | ||
markEndQueue.set(groupKey, { app, uid, instance: vm, type, time }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.