Skip to content

Commit

Permalink
fix(runtime-dom): functions returned should not support reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoErii committed Dec 16, 2023
1 parent 4e7967f commit 9951051
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions packages/runtime-dom/src/directives/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,16 @@ const modifierGuards: Record<
export const withModifiers = <
T extends (event: Event, ...args: unknown[]) => any
>(
fn: T & { _withMods?: T },
fn: T,
modifiers: string[]
) => {
return (
fn._withMods ||
(fn._withMods = ((event, ...args) => {
for (let i = 0; i < modifiers.length; i++) {
const guard = modifierGuards[modifiers[i]]
if (guard && guard(event, modifiers)) return
}
return fn(event, ...args)
}) as T)
)
return ((event, ...args) => {
for (let i = 0; i < modifiers.length; i++) {
const guard = modifierGuards[modifiers[i]]
if (guard && guard(event, modifiers)) return
}
return fn(event, ...args)
}) as T
}

// Kept for 2.x compat.
Expand All @@ -66,7 +63,7 @@ const keyNames: Record<string, string | string[]> = {
* @private
*/
export const withKeys = <T extends (event: KeyboardEvent) => any>(
fn: T & { _withKeys?: T },
fn: T,
modifiers: string[]
) => {
let globalKeyCodes: LegacyConfig['keyCodes']
Expand All @@ -88,43 +85,40 @@ export const withKeys = <T extends (event: KeyboardEvent) => any>(
}
}

return (
fn._withKeys ||
(fn._withKeys = (event => {
if (!('key' in event)) {
return
}
return (event => {
if (!('key' in event)) {
return
}

const eventKey = hyphenate(event.key)
if (modifiers.some(k => k === eventKey || keyNames[k] === eventKey)) {
const eventKey = hyphenate(event.key)
if (modifiers.some(k => k === eventKey || keyNames[k] === eventKey)) {
return fn(event)
}

if (__COMPAT__) {
const keyCode = String(event.keyCode)
if (
compatUtils.isCompatEnabled(
DeprecationTypes.V_ON_KEYCODE_MODIFIER,
instance
) &&
modifiers.some(mod => mod == keyCode)
) {
return fn(event)
}

if (__COMPAT__) {
const keyCode = String(event.keyCode)
if (
compatUtils.isCompatEnabled(
DeprecationTypes.V_ON_KEYCODE_MODIFIER,
instance
) &&
modifiers.some(mod => mod == keyCode)
) {
return fn(event)
}
if (globalKeyCodes) {
for (const mod of modifiers) {
const codes = globalKeyCodes[mod]
if (codes) {
const matches = isArray(codes)
? codes.some(code => String(code) === keyCode)
: String(codes) === keyCode
if (matches) {
return fn(event)
}
if (globalKeyCodes) {
for (const mod of modifiers) {
const codes = globalKeyCodes[mod]
if (codes) {
const matches = isArray(codes)
? codes.some(code => String(code) === keyCode)
: String(codes) === keyCode
if (matches) {
return fn(event)
}
}
}
}
}) as T)
)
}
}) as T
}

0 comments on commit 9951051

Please sign in to comment.