wip(vapor): optimize event handling

This commit is contained in:
Evan You 2025-02-08 22:40:37 +08:00
parent a2b320bca7
commit 69422d50d8
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
2 changed files with 18 additions and 43 deletions

View File

@ -132,14 +132,6 @@ export function prepend(parent: ParentNode, ...blocks: Block[]): void {
while (i--) insert(blocks[i], parent, 0)
}
/**
* Optimized children removal: record all parents with unmounted children
* during each root remove call, and update their children list by filtering
* unmounted children
*/
// export let parentsWithUnmountedChildren: Set<VaporComponentInstance> | null =
// null
export function remove(block: Block, parent?: ParentNode): void {
if (block instanceof Node) {
parent && parent.removeChild(block)

View File

@ -1,10 +1,4 @@
import {
getCurrentScope,
onEffectCleanup,
onScopeDispose,
} from '@vue/reactivity'
import { queuePostFlushCb } from '@vue/runtime-dom'
import { remove } from '@vue/shared'
import { onEffectCleanup } from '@vue/reactivity'
export function addEventListener(
el: Element,
@ -22,26 +16,13 @@ export function on(
handlerGetter: () => undefined | ((...args: any[]) => any),
options: AddEventListenerOptions & { effect?: boolean } = {},
): void {
const handler: DelegatedHandler = eventHandler(handlerGetter)
let cleanupEvent: (() => void) | undefined
queuePostFlushCb(() => {
cleanupEvent = addEventListener(el, event, handler, options)
})
const handler = eventHandler(handlerGetter)
addEventListener(el, event, handler, options)
if (options.effect) {
onEffectCleanup(cleanup)
} else if (getCurrentScope()) {
onScopeDispose(cleanup)
onEffectCleanup(() => {
el.removeEventListener(event, handler, options)
})
}
function cleanup() {
cleanupEvent && cleanupEvent()
}
}
export type DelegatedHandler = {
(...args: any[]): any
delegate?: boolean
}
export function delegate(
@ -49,20 +30,22 @@ export function delegate(
event: string,
handlerGetter: () => undefined | ((...args: any[]) => any),
): void {
const handler: DelegatedHandler = eventHandler(handlerGetter)
const key = `$evt${event}`
const handler = eventHandler(handlerGetter)
handler.delegate = true
const cacheKey = `$evt${event}`
const handlers: DelegatedHandler[] = el[cacheKey] || (el[cacheKey] = [])
handlers.push(handler)
onScopeDispose(() => remove(handlers, handler))
;(el[key] || (el[key] = [])).push(handler)
}
function eventHandler(getter: () => undefined | ((...args: any[]) => any)) {
return (...args: any[]) => {
let handler = getter()
if (!handler) return
type DelegatedHandler = {
(...args: any[]): any
delegate?: boolean
}
function eventHandler(
getter: () => undefined | ((...args: any[]) => any),
): DelegatedHandler {
return (...args: any[]) => {
const handler = getter()
handler && handler(...args)
}
}