Skip to content

refactor(compiler-dom): use faster sets in vOn #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions packages/compiler-dom/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@ import {
} from '@vue/compiler-core'
import { V_ON_MODIFIERS_GUARD, V_ON_KEYS_GUARD } from '../runtimeHelpers'

const EVENT_OPTION_MODIFIERS = { passive: true, once: true, capture: true }
const NOT_KEY_MODIFIERS = {
stop: true,
prevent: true,
self: true,
const EVENT_OPTION_MODIFIERS = new Set(['passive', 'once', 'capture'])
const NOT_KEY_MODIFIERS = new Set([
'stop',
'prevent',
'self',
// system
ctrl: true,
shift: true,
alt: true,
meta: true,
'ctrl',
'shift',
'alt',
'meta',
// mouse
left: true,
middle: true,
right: true,
'left',
'middle',
'right',
// exact
exact: true
}
const KEYBOARD_EVENTS = { onkeyup: true, onkeydown: true, onkeypress: true }
'exact'
])
const KEYBOARD_EVENTS = new Set(['onkeyup', 'onkeydown', 'onkeypress'])

export const transformOn: DirectiveTransform = (dir, node, context) => {
const { modifiers } = dir
const baseResult = baseTransform(dir, node, context)
if (!modifiers.length) return baseResult
const { key, value } = baseResult.props[0]
const runtimeModifiers = modifiers.filter(m => !(m in EVENT_OPTION_MODIFIERS))
const runtimeModifiers = modifiers.filter(m => !EVENT_OPTION_MODIFIERS.has(m))
let handler = createCallExpression(context.helper(V_ON_MODIFIERS_GUARD), [
value,
JSON.stringify(runtimeModifiers.filter(m => m in NOT_KEY_MODIFIERS))
JSON.stringify(runtimeModifiers.filter(m => NOT_KEY_MODIFIERS.has(m)))
])
if (
// if event name is dynamic, always wrap with keys guard
key.type === NodeTypes.COMPOUND_EXPRESSION ||
!(key.isStatic) ||
key.content.toLowerCase() in KEYBOARD_EVENTS
KEYBOARD_EVENTS.has(key.content.toLowerCase())
) {
handler = createCallExpression(context.helper(V_ON_KEYS_GUARD), [
handler,
JSON.stringify(runtimeModifiers.filter(m => !(m in NOT_KEY_MODIFIERS)))
JSON.stringify(runtimeModifiers.filter(m => !NOT_KEY_MODIFIERS.has(m)))
])
}
const properties = [
Expand All @@ -55,9 +55,7 @@ export const transformOn: DirectiveTransform = (dir, node, context) => {
createObjectProperty('persistent', createSimpleExpression('true', false))
]

const eventOptionModifiers = modifiers.filter(
modifier => modifier in EVENT_OPTION_MODIFIERS
)
const eventOptionModifiers = modifiers.filter(m => EVENT_OPTION_MODIFIERS.has(m))
if (eventOptionModifiers.length) {
properties.push(
createObjectProperty(
Expand Down