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

112 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-07-17 06:18:52 +08:00
import { App } from './apiCreateApp'
import { Fragment, Text, Comment, Static } from './vnode'
import { 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
}
const 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
}
interface DevtoolsHook {
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[]
}
export let devtools: DevtoolsHook
export function setDevtoolsHook(hook: DevtoolsHook) {
devtools = hook
}
export function devtoolsInitApp(app: App, version: string) {
2020-07-17 06:18:52 +08:00
// TODO queue if devtools is undefined
if (!devtools) return
devtools.emit(DevtoolsHooks.APP_INIT, app, version, {
Fragment,
Text,
Comment,
Static
2020-07-17 06:18:52 +08:00
})
}
export function devtoolsUnmountApp(app: App) {
2020-07-17 06:18:52 +08:00
if (!devtools) return
devtools.emit(DevtoolsHooks.APP_UNMOUNT, app)
}
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)
2021-07-20 06:24:18 +08:00
export const devtoolsComponentRemoved =
/*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_REMOVED)
2020-07-17 06:18:52 +08:00
function createDevtoolsComponentHook(hook: DevtoolsHooks) {
return (component: ComponentInternalInstance) => {
if (!devtools) return
devtools.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) => {
if (!devtools) return
devtools.emit(
hook,
component.appContext.app,
component.uid,
component,
type,
time
)
}
}
2020-08-24 07:31:32 +08:00
export function devtoolsComponentEmit(
component: ComponentInternalInstance,
event: string,
params: any[]
) {
if (!devtools) return
devtools.emit(
DevtoolsHooks.COMPONENT_EMIT,
component.appContext.app,
component,
2020-08-24 07:31:32 +08:00
event,
params
)
}