Skip to content

Commit

Permalink
refactor(runtime-vapor): cache event handlers on element
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Dec 25, 2024
1 parent ef6986f commit c748399
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
25 changes: 0 additions & 25 deletions packages/runtime-vapor/src/componentMetadata.ts

This file was deleted.

16 changes: 8 additions & 8 deletions packages/runtime-vapor/src/dom/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import {
onEffectCleanup,
onScopeDispose,
} from '@vue/reactivity'
import {
MetadataKind,
getMetadata,
recordEventMetadata,
} from '../componentMetadata'
import { queuePostFlushCb } from '@vue/runtime-dom'
import { remove } from '@vue/shared'

export function addEventListener(
el: Element,
Expand Down Expand Up @@ -49,13 +45,17 @@ export type DelegatedHandler = {
}

export function delegate(
el: HTMLElement,
el: any,
event: string,
handlerGetter: () => undefined | ((...args: any[]) => any),
): void {
const handler: DelegatedHandler = eventHandler(handlerGetter)
handler.delegate = true
recordEventMetadata(el, event, handler)

const cacheKey = `$evt${event}`
const handlers: DelegatedHandler[] = el[cacheKey] || (el[cacheKey] = [])
handlers.push(handler)
onScopeDispose(() => remove(handlers, handler))
}

function eventHandler(getter: () => undefined | ((...args: any[]) => any)) {
Expand Down Expand Up @@ -98,7 +98,7 @@ const delegatedEventHandler = (e: Event) => {
},
})
while (node !== null) {
const handlers = getMetadata(node)[MetadataKind.event][e.type]
const handlers = node[`$evt${e.type}`] as DelegatedHandler[]
if (handlers) {
for (const handler of handlers) {
if (handler.delegate && !node.disabled) {
Expand Down

0 comments on commit c748399

Please sign in to comment.