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) 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 { export function remove(block: Block, parent?: ParentNode): void {
if (block instanceof Node) { if (block instanceof Node) {
parent && parent.removeChild(block) parent && parent.removeChild(block)

View File

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