-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue2): perf timeline, closes #1744
- Loading branch information
Showing
4 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { DevtoolsApi } from '@vue-devtools/app-backend-api' | ||
import { HookEvents, SharedData } from '@vue-devtools/shared-utils' | ||
import { instanceMap } from './tree' | ||
|
||
const COMPONENT_HOOKS = { | ||
beforeCreate: { start: 'create' }, | ||
created: { end: 'create' }, | ||
beforeMount: { start: 'mount' }, | ||
mounted: { end: 'mount' }, | ||
beforeUpdate: { start: 'update' }, | ||
updated: { end: 'update' }, | ||
beforeDestroyed: { start: 'destroy' }, | ||
destroyed: { end: 'destroy' }, | ||
} | ||
|
||
export function initPerf (api: DevtoolsApi, app, Vue) { | ||
// Global mixin | ||
Vue.mixin({ | ||
beforeCreate () { | ||
applyPerfHooks(api, this, app) | ||
}, | ||
}) | ||
|
||
// Apply to existing components | ||
instanceMap?.forEach(vm => applyPerfHooks(api, vm, app)) | ||
} | ||
|
||
export function applyPerfHooks (api: DevtoolsApi, vm, app) { | ||
if (vm.$options.$_devtoolsPerfHooks) return | ||
vm.$options.$_devtoolsPerfHooks = true | ||
|
||
for (const hook in COMPONENT_HOOKS) { | ||
const { start, end } = COMPONENT_HOOKS[hook] | ||
const handler = function (this: any) { | ||
if (SharedData.performanceMonitoringEnabled) { | ||
api.ctx.hook.emit( | ||
start ? HookEvents.PERFORMANCE_START : HookEvents.PERFORMANCE_END, | ||
app, | ||
this._uid, | ||
this, | ||
start ?? end, | ||
api.now(), | ||
) | ||
} | ||
} | ||
const currentValue = vm.$options[hook] | ||
if (Array.isArray(currentValue)) { | ||
vm.$options[hook] = [handler, ...currentValue] | ||
} else if (typeof currentValue === 'function') { | ||
vm.$options[hook] = [handler, currentValue] | ||
} else { | ||
vm.$options[hook] = [handler] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters