|
| 1 | +import { App } from './apiCreateApp' |
| 2 | +import { Fragment, Text, Comment, Static } from './vnode' |
| 3 | +import { ComponentInternalInstance } from './component' |
| 4 | + |
| 5 | +export interface AppRecord { |
| 6 | + id: number |
| 7 | + app: App |
| 8 | + version: string |
| 9 | + types: { [key: string]: string | Symbol } |
| 10 | +} |
| 11 | + |
| 12 | +enum DevtoolsHooks { |
| 13 | + APP_INIT = 'app:init', |
| 14 | + APP_UNMOUNT = 'app:unmount', |
| 15 | + COMPONENT_UPDATED = 'component:updated', |
| 16 | + COMPONENT_ADDED = 'component:added', |
| 17 | + COMPONENT_REMOVED = 'component:removed' |
| 18 | +} |
| 19 | + |
| 20 | +export interface DevtoolsHook { |
| 21 | + emit: (event: string, ...payload: any[]) => void |
| 22 | + on: (event: string, handler: Function) => void |
| 23 | + once: (event: string, handler: Function) => void |
| 24 | + off: (event: string, handler: Function) => void |
| 25 | + appRecords: AppRecord[] |
| 26 | +} |
| 27 | + |
| 28 | +export let devtools: DevtoolsHook |
| 29 | + |
| 30 | +export function setDevtoolsHook(hook: DevtoolsHook) { |
| 31 | + devtools = hook |
| 32 | +} |
| 33 | + |
| 34 | +export function initApp(app: App, version: string) { |
| 35 | + // TODO queue if devtools is undefined |
| 36 | + if (!devtools) return |
| 37 | + devtools.emit(DevtoolsHooks.APP_INIT, app, version, { |
| 38 | + Fragment: Fragment, |
| 39 | + Text: Text, |
| 40 | + Comment: Comment, |
| 41 | + Static: Static |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +export function appUnmounted(app: App) { |
| 46 | + if (!devtools) return |
| 47 | + devtools.emit(DevtoolsHooks.APP_UNMOUNT, app) |
| 48 | +} |
| 49 | + |
| 50 | +export function componentAdded(component: ComponentInternalInstance) { |
| 51 | + if (!devtools || !component.appContext.__app) return |
| 52 | + devtools.emit( |
| 53 | + DevtoolsHooks.COMPONENT_ADDED, |
| 54 | + component.appContext.__app, |
| 55 | + component.uid, |
| 56 | + component.parent ? component.parent.uid : undefined |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +export function componentUpdated(component: ComponentInternalInstance) { |
| 61 | + if (!devtools || !component.appContext.__app) return |
| 62 | + devtools.emit( |
| 63 | + DevtoolsHooks.COMPONENT_UPDATED, |
| 64 | + component.appContext.__app, |
| 65 | + component.uid, |
| 66 | + component.parent ? component.parent.uid : undefined |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +export function componentRemoved(component: ComponentInternalInstance) { |
| 71 | + if (!devtools || !component.appContext.__app) return |
| 72 | + devtools.emit( |
| 73 | + DevtoolsHooks.COMPONENT_REMOVED, |
| 74 | + component.appContext.__app, |
| 75 | + component.uid, |
| 76 | + component.parent ? component.parent.uid : undefined |
| 77 | + ) |
| 78 | +} |
0 commit comments