|
| 1 | +/* eslint-disable no-restricted-globals */ |
| 2 | +import type { App } from './apiCreateVaporApp' |
| 3 | +import type { ComponentInternalInstance } from './component' |
| 4 | + |
| 5 | +interface AppRecord { |
| 6 | + id: number |
| 7 | + app: App |
| 8 | + version: string |
| 9 | + types: Record<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 | + COMPONENT_EMIT = 'component:emit', |
| 19 | + PERFORMANCE_START = 'perf:start', |
| 20 | + PERFORMANCE_END = 'perf:end', |
| 21 | +} |
| 22 | + |
| 23 | +export interface DevtoolsHook { |
| 24 | + enabled?: boolean |
| 25 | + emit: (event: string, ...payload: any[]) => void |
| 26 | + on: (event: string, handler: Function) => void |
| 27 | + once: (event: string, handler: Function) => void |
| 28 | + off: (event: string, handler: Function) => void |
| 29 | + appRecords: AppRecord[] |
| 30 | + /** |
| 31 | + * Added at https://github.com/vuejs/devtools/commit/f2ad51eea789006ab66942e5a27c0f0986a257f9 |
| 32 | + * Returns whether the arg was buffered or not |
| 33 | + */ |
| 34 | + cleanupBuffer?: (matchArg: unknown) => boolean |
| 35 | +} |
| 36 | + |
| 37 | +export let devtools: DevtoolsHook |
| 38 | + |
| 39 | +let buffer: { event: string; args: any[] }[] = [] |
| 40 | + |
| 41 | +let devtoolsNotInstalled = false |
| 42 | + |
| 43 | +function emit(event: string, ...args: any[]) { |
| 44 | + if (devtools) { |
| 45 | + devtools.emit(event, ...args) |
| 46 | + } else if (!devtoolsNotInstalled) { |
| 47 | + buffer.push({ event, args }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function setDevtoolsHook(hook: DevtoolsHook, target: any) { |
| 52 | + devtools = hook |
| 53 | + if (devtools) { |
| 54 | + devtools.enabled = true |
| 55 | + buffer.forEach(({ event, args }) => devtools.emit(event, ...args)) |
| 56 | + buffer = [] |
| 57 | + } else if ( |
| 58 | + // handle late devtools injection - only do this if we are in an actual |
| 59 | + // browser environment to avoid the timer handle stalling test runner exit |
| 60 | + // (#4815) |
| 61 | + typeof window !== 'undefined' && |
| 62 | + // some envs mock window but not fully |
| 63 | + window.HTMLElement && |
| 64 | + // also exclude jsdom |
| 65 | + // eslint-disable-next-line no-restricted-syntax |
| 66 | + !window.navigator?.userAgent?.includes('jsdom') |
| 67 | + ) { |
| 68 | + const replay = (target.__VUE_DEVTOOLS_HOOK_REPLAY__ = |
| 69 | + target.__VUE_DEVTOOLS_HOOK_REPLAY__ || []) |
| 70 | + replay.push((newHook: DevtoolsHook) => { |
| 71 | + setDevtoolsHook(newHook, target) |
| 72 | + }) |
| 73 | + // clear buffer after 3s - the user probably doesn't have devtools installed |
| 74 | + // at all, and keeping the buffer will cause memory leaks (#4738) |
| 75 | + setTimeout(() => { |
| 76 | + if (!devtools) { |
| 77 | + target.__VUE_DEVTOOLS_HOOK_REPLAY__ = null |
| 78 | + devtoolsNotInstalled = true |
| 79 | + buffer = [] |
| 80 | + } |
| 81 | + }, 3000) |
| 82 | + } else { |
| 83 | + // non-browser env, assume not installed |
| 84 | + devtoolsNotInstalled = true |
| 85 | + buffer = [] |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export function devtoolsInitApp(app: App, version: string) { |
| 90 | + emit(DevtoolsHooks.APP_INIT, app, version, {}) |
| 91 | +} |
| 92 | + |
| 93 | +export function devtoolsUnmountApp(app: App) { |
| 94 | + emit(DevtoolsHooks.APP_UNMOUNT, app) |
| 95 | +} |
| 96 | + |
| 97 | +export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook( |
| 98 | + DevtoolsHooks.COMPONENT_ADDED, |
| 99 | +) |
| 100 | + |
| 101 | +export const devtoolsComponentUpdated = |
| 102 | + /*#__PURE__*/ createDevtoolsComponentHook(DevtoolsHooks.COMPONENT_UPDATED) |
| 103 | + |
| 104 | +const _devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsComponentHook( |
| 105 | + DevtoolsHooks.COMPONENT_REMOVED, |
| 106 | +) |
| 107 | + |
| 108 | +export const devtoolsComponentRemoved = ( |
| 109 | + component: ComponentInternalInstance, |
| 110 | +) => { |
| 111 | + if ( |
| 112 | + devtools && |
| 113 | + typeof devtools.cleanupBuffer === 'function' && |
| 114 | + // remove the component if it wasn't buffered |
| 115 | + !devtools.cleanupBuffer(component) |
| 116 | + ) { |
| 117 | + _devtoolsComponentRemoved(component) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/*! #__NO_SIDE_EFFECTS__ */ |
| 122 | +function createDevtoolsComponentHook(hook: DevtoolsHooks) { |
| 123 | + return (component: ComponentInternalInstance) => { |
| 124 | + emit( |
| 125 | + hook, |
| 126 | + component.appContext.app, |
| 127 | + component.uid, |
| 128 | + component.parent ? component.parent.uid : undefined, |
| 129 | + component, |
| 130 | + ) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export const devtoolsPerfStart = /*#__PURE__*/ createDevtoolsPerformanceHook( |
| 135 | + DevtoolsHooks.PERFORMANCE_START, |
| 136 | +) |
| 137 | + |
| 138 | +export const devtoolsPerfEnd = /*#__PURE__*/ createDevtoolsPerformanceHook( |
| 139 | + DevtoolsHooks.PERFORMANCE_END, |
| 140 | +) |
| 141 | + |
| 142 | +function createDevtoolsPerformanceHook(hook: DevtoolsHooks) { |
| 143 | + return (component: ComponentInternalInstance, type: string, time: number) => { |
| 144 | + emit(hook, component.appContext.app, component.uid, component, type, time) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +export function devtoolsComponentEmit( |
| 149 | + component: ComponentInternalInstance, |
| 150 | + event: string, |
| 151 | + params: any[], |
| 152 | +) { |
| 153 | + emit( |
| 154 | + DevtoolsHooks.COMPONENT_EMIT, |
| 155 | + component.appContext.app, |
| 156 | + component, |
| 157 | + event, |
| 158 | + params, |
| 159 | + ) |
| 160 | +} |
0 commit comments