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

90 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-07-16 22:18:52 +00:00
import { App } from './apiCreateApp'
import { Fragment, Text, Comment, Static } from './vnode'
import { ComponentInternalInstance } from './component'
interface AppRecord {
2020-07-16 22:18:52 +00:00
id: number
app: App
version: string
types: Record<string, string | Symbol>
2020-07-16 22:18:52 +00:00
}
const enum DevtoolsHooks {
2020-07-16 22:18:52 +00:00
APP_INIT = 'app:init',
APP_UNMOUNT = 'app:unmount',
COMPONENT_UPDATED = 'component:updated',
COMPONENT_ADDED = 'component:added',
2020-08-23 23:31:32 +00:00
COMPONENT_REMOVED = 'component:removed',
COMPONENT_EMIT = 'component:emit'
2020-07-16 22:18:52 +00:00
}
interface DevtoolsHook {
2020-07-16 22:18:52 +00: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-16 22:18:52 +00:00
// TODO queue if devtools is undefined
if (!devtools) return
devtools.emit(DevtoolsHooks.APP_INIT, app, version, {
Fragment,
Text,
Comment,
Static
2020-07-16 22:18:52 +00:00
})
}
export function devtoolsUnmountApp(app: App) {
2020-07-16 22:18:52 +00:00
if (!devtools) return
devtools.emit(DevtoolsHooks.APP_UNMOUNT, app)
}
export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook(
DevtoolsHooks.COMPONENT_ADDED
)
2020-07-16 22:18:52 +00:00
export const devtoolsComponentUpdated = /*#__PURE__*/ createDevtoolsComponentHook(
DevtoolsHooks.COMPONENT_UPDATED
)
export const devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsComponentHook(
DevtoolsHooks.COMPONENT_REMOVED
)
2020-07-16 22:18:52 +00:00
function createDevtoolsComponentHook(hook: DevtoolsHooks) {
return (component: ComponentInternalInstance) => {
if (!devtools) return
devtools.emit(
hook,
component.appContext.app,
component.uid,
2020-12-18 17:24:01 +00:00
component.parent ? component.parent.uid : undefined,
component
)
}
2020-07-16 22:18:52 +00:00
}
2020-08-23 23:31:32 +00:00
export function devtoolsComponentEmit(
component: ComponentInternalInstance,
event: string,
params: any[]
) {
if (!devtools) return
devtools.emit(
DevtoolsHooks.COMPONENT_EMIT,
component.appContext.app,
component,
2020-08-23 23:31:32 +00:00
event,
params
)
}