vue3-core/packages/runtime-core/src/devtools.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-05-10 10:43:35 +08:00
/* eslint-disable no-restricted-globals */
2020-07-17 06:18:52 +08:00
import type { App } from './apiCreateApp'
import { Comment, Fragment, Static, Text } from './vnode'
import type { ComponentInternalInstance } from './component'
interface AppRecord {
2020-07-17 06:18:52 +08:00
id: number
app: App
version: string
types: Record<string, string | Symbol>
2020-07-17 06:18:52 +08:00
}
enum DevtoolsHooks {
2020-07-17 06:18:52 +08:00
APP_INIT = 'app:init',
APP_UNMOUNT = 'app:unmount',
COMPONENT_UPDATED = 'component:updated',
COMPONENT_ADDED = 'component:added',
2020-08-24 07:31:32 +08:00
COMPONENT_REMOVED = 'component:removed',
2021-05-20 02:48:35 +08:00
COMPONENT_EMIT = 'component:emit',
PERFORMANCE_START = 'perf:start',
PERFORMANCE_END = 'perf:end',
2020-07-17 06:18:52 +08:00
}
export interface DevtoolsHook {
enabled?: boolean
2020-07-17 06:18:52 +08:00
emit: (event: string, ...payload: any[]) => void
on: (event: string, handler: Function) => void
once: (event: string, handler: Function) => void
off: (event: string, handler: Function) => void
appRecords: AppRecord[]
/**
* Added at https://github.com/vuejs/devtools/commit/f2ad51eea789006ab66942e5a27c0f0986a257f9
2023-07-09 13:03:30 +08:00
* Returns whether the arg was buffered or not
*/
cleanupBuffer?: (matchArg: unknown) => boolean
2020-07-17 06:18:52 +08:00
}
export let devtools: DevtoolsHook
let buffer: { event: string; args: any[] }[] = []
let devtoolsNotInstalled = false
function emit(event: string, ...args: any[]) {
if (devtools) {
devtools.emit(event, ...args)
} else if (!devtoolsNotInstalled) {
buffer.push({ event, args })
}
}
export function setDevtoolsHook(hook: DevtoolsHook, target: any) {
2020-07-17 06:18:52 +08:00
devtools = hook
if (devtools) {
devtools.enabled = true
buffer.forEach(({ event, args }) => devtools.emit(event, ...args))
buffer = []
} else if (
// handle late devtools injection - only do this if we are in an actual
// browser environment to avoid the timer handle stalling test runner exit
// (#4815)
typeof window !== 'undefined' &&
// some envs mock window but not fully
window.HTMLElement &&
// also exclude jsdom
// eslint-disable-next-line no-restricted-syntax
!window.navigator?.userAgent?.includes('jsdom')
) {
const replay = (target.__VUE_DEVTOOLS_HOOK_REPLAY__ =
target.__VUE_DEVTOOLS_HOOK_REPLAY__ || [])
replay.push((newHook: DevtoolsHook) => {
setDevtoolsHook(newHook, target)
})
// clear buffer after 3s - the user probably doesn't have devtools installed
// at all, and keeping the buffer will cause memory leaks (#4738)
setTimeout(() => {
if (!devtools) {
target.__VUE_DEVTOOLS_HOOK_REPLAY__ = null
devtoolsNotInstalled = true
buffer = []
}
}, 3000)
} else {
// non-browser env, assume not installed
devtoolsNotInstalled = true
buffer = []
}
2020-07-17 06:18:52 +08:00
}
export function devtoolsInitApp(app: App, version: string) {
emit(DevtoolsHooks.APP_INIT, app, version, {
Fragment,
Text,
Comment,
Static,
2020-07-17 06:18:52 +08:00
})
}
export function devtoolsUnmountApp(app: App) {
emit(DevtoolsHooks.APP_UNMOUNT, app)
2020-07-17 06:18:52 +08:00
}
export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook(
DevtoolsHooks.COMPONENT_ADDED,
)
2020-07-17 06:18:52 +08:00
2021-07-20 06:24:18 +08:00
export const devtoolsComponentUpdated =
/*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_UPDATED)
const _devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsComponentHook(
DevtoolsHooks.COMPONENT_REMOVED,
)
export const devtoolsComponentRemoved = (
component: ComponentInternalInstance,
) => {
if (
devtools &&
typeof devtools.cleanupBuffer === 'function' &&
// remove the component if it wasn't buffered
!devtools.cleanupBuffer(component)
) {
_devtoolsComponentRemoved(component)
}
}
2020-07-17 06:18:52 +08:00
/*! #__NO_SIDE_EFFECTS__ */
function createDevtoolsComponentHook(hook: DevtoolsHooks) {
return (component: ComponentInternalInstance) => {
emit(
hook,
component.appContext.app,
component.uid,
2020-12-19 01:24:01 +08:00
component.parent ? component.parent.uid : undefined,
component,
)
}
2020-07-17 06:18:52 +08:00
}
2020-08-24 07:31:32 +08:00
2021-05-20 02:48:35 +08:00
export const devtoolsPerfStart = /*#__PURE__*/ createDevtoolsPerformanceHook(
DevtoolsHooks.PERFORMANCE_START,
)
export const devtoolsPerfEnd = /*#__PURE__*/ createDevtoolsPerformanceHook(
DevtoolsHooks.PERFORMANCE_END,
)
function createDevtoolsPerformanceHook(hook: DevtoolsHooks) {
return (component: ComponentInternalInstance, type: string, time: number) => {
emit(hook, component.appContext.app, component.uid, component, type, time)
2021-05-20 02:48:35 +08:00
}
}
2020-08-24 07:31:32 +08:00
export function devtoolsComponentEmit(
component: ComponentInternalInstance,
event: string,
params: any[],
) {
emit(
2020-08-24 07:31:32 +08:00
DevtoolsHooks.COMPONENT_EMIT,
component.appContext.app,
component,
2020-08-24 07:31:32 +08:00
event,
params,
)
}