import { App } from './apiCreateApp' ; import { Fragment , Text , Comment , Static } from './vnode' ; import { ComponentInternalInstance } from './component' ; interface AppRecord { id : number ; app : App ; version : string ; types : Record < string , string | Symbol > ; } const enum DevtoolsHooks { APP_INIT = 'app:init' , APP_UNMOUNT = 'app:unmount' , COMPONENT_UPDATED = 'component:updated' , COMPONENT_ADDED = 'component:added' , COMPONENT_REMOVED = 'component:removed' , COMPONENT_EMIT = 'component:emit' , PERFORMANCE_START = 'perf:start' , PERFORMANCE_END = 'perf:end' } interface DevtoolsHook { 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 ; export function setDevtoolsHook ( hook ) { devtools = hook ; } export function devtoolsInitApp ( app , version ) { if ( ! devtools ) return ; devtools . emit ( DevtoolsHooks . APP_INIT , app , version , { Fragment , Text , Comment , Static } ) ; } export function devtoolsUnmountApp ( app ) { if ( ! devtools ) return ; devtools . emit ( DevtoolsHooks . APP_UNMOUNT , app ) ; } export const devtoolsComponentAdded = createDevtoolsComponentHook ( DevtoolsHooks . COMPONENT_ADDED ) ; export const devtoolsComponentUpdated = createDevtoolsComponentHook ( DevtoolsHooks . COMPONENT_UPDATED ) ; export const devtoolsComponentRemoved = createDevtoolsComponentHook ( DevtoolsHooks . COMPONENT_REMOVED ) ; function createDevtoolsComponentHook ( hook ) { return ( component ) => { if ( ! devtools ) return ; devtools . emit ( hook , component . appContext . app , component . uid , component . parent ? component . parent . uid : undefined , component ) ; } ; } export const devtoolsPerfStart = createDevtoolsPerformanceHook ( DevtoolsHooks . PERFORMANCE_START ) ; export const devtoolsPerfEnd = createDevtoolsPerformanceHook ( DevtoolsHooks . PERFORMANCE_END ) ; function createDevtoolsPerformanceHook ( hook ) { return ( component , type , time ) => { if ( ! devtools ) return ; devtools . emit ( hook , component . appContext . app , component . uid , component , type , time ) ; } ; } export function devtoolsComponentEmit ( component , event , params ) { if ( ! devtools ) return ; devtools . emit ( DevtoolsHooks . COMPONENT_EMIT , component . appContext . app , component , event , params ) ; }