Skip to content

Commit

Permalink
perf: avoid renderer updating for out viewport component
Browse files Browse the repository at this point in the history
  • Loading branch information
zcf0508 committed Dec 21, 2024
1 parent 610f523 commit d8618d5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/core/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import {
updateHighlight as _updateHighlight,
type ComponentBoundingRect,
type ComponentHighLighterOptions,
createHighlight,
type VueAppInstance,
} from '@vue/devtools-kit'
import { throttle } from 'lodash-es'
import { getComponentBoundingRect, getInstanceName } from './utils'

export function isInViewport(bounds: ComponentBoundingRect): boolean {
const viewportWidth = window.innerWidth
const viewportHeight = window.innerHeight
const scrollTop = window.scrollY
const scrollLeft = window.scrollX

return !(
bounds.right < scrollLeft
|| bounds.bottom < scrollTop
|| bounds.left > scrollLeft + viewportWidth
|| bounds.top > scrollTop + viewportHeight
)
}

type UpdateHighlightFn = (
options: ComponentHighLighterOptions & { elementId?: string, style?: Partial<CSSStyleDeclaration> },
flashCount: number,
Expand All @@ -20,6 +35,10 @@ export function createUpdateHighlight(): UpdateHighlightFn {
flashCount: number,
hideCompnentName?: boolean,
) => {
if (!isInViewport(options.bounds)) {
return
}

_updateHighlight(options)

if (!options.elementId) {
Expand Down Expand Up @@ -75,6 +94,10 @@ export function highlight(
const bounds = getComponentBoundingRect(instance)
if (!bounds.width && !bounds.height)
return
if (!isInViewport(bounds)) {
return
}

const name = `${getInstanceName(instance)} x ${flashCount}`

const continerEl = document.getElementById(uuid)
Expand Down

0 comments on commit d8618d5

Please sign in to comment.