refactor: events tweak

This commit is contained in:
Evan You 2018-12-19 18:14:41 -05:00
parent 85bcc2b87c
commit 9449dfb352

View File

@ -17,7 +17,7 @@ export function patchEvent(
} }
const eventCounts: Record<string, number> = {} const eventCounts: Record<string, number> = {}
const attachedGlobalHandlers: Record<string, Function> = {} const attachedGlobalHandlers: Record<string, Function | null> = {}
export function handleDelegatedEvent( export function handleDelegatedEvent(
el: any, el: any,
@ -38,18 +38,16 @@ export function handleDelegatedEvent(
} }
store[name] = value store[name] = value
} else if (store && store[name]) { } else if (store && store[name]) {
eventCounts[name]-- if (--eventCounts[name] === 0) {
store[name] = null
if (count === 1) {
removeGlobalHandler(name) removeGlobalHandler(name)
} }
store[name] = null
} }
} }
function attachGlobalHandler(name: string) { function attachGlobalHandler(name: string) {
const handler = (attachedGlobalHandlers[name] = (e: Event) => { const handler = (attachedGlobalHandlers[name] = (e: Event) => {
const { type } = e const isClick = e.type === 'click' || e.type === 'dblclick'
const isClick = type === 'click' || type === 'dblclick'
if (isClick && (e as MouseEvent).button !== 0) { if (isClick && (e as MouseEvent).button !== 0) {
e.stopPropagation() e.stopPropagation()
return false return false
@ -114,7 +112,7 @@ function invokeEvents(e: Event, value: EventValue) {
function removeGlobalHandler(name: string) { function removeGlobalHandler(name: string) {
document.removeEventListener(name, attachedGlobalHandlers[name] as any) document.removeEventListener(name, attachedGlobalHandlers[name] as any)
eventCounts[name] = 0 attachedGlobalHandlers[name] = null
} }
function handleNormalEvent(el: Element, name: string, prev: any, next: any) { function handleNormalEvent(el: Element, name: string, prev: any, next: any) {