@@ -52,21 +52,24 @@ const modifierGuards: Record<
5252export const withModifiers = <
5353 T extends ( event : Event , ...args : unknown [ ] ) => any ,
5454> (
55- fn : T & { _withMods ?: { [ key : string ] : T } } ,
55+ fn : T & { _withMods ?: Map < string , T > } ,
5656 modifiers : VOnModifiers [ ] ,
5757) : T => {
58- const cache = fn . _withMods || ( fn . _withMods = { } )
58+ const cache = fn . _withMods || ( fn . _withMods = new Map ( ) )
5959 const cacheKey = modifiers . join ( '.' )
60- return (
61- cache [ cacheKey ] ||
62- ( cache [ cacheKey ] = ( ( event , ...args ) => {
63- for ( let i = 0 ; i < modifiers . length ; i ++ ) {
64- const guard = modifierGuards [ modifiers [ i ] as ModifierGuards ]
65- if ( guard && guard ( event , modifiers ) ) return
66- }
67- return fn ( event , ...args )
68- } ) as T )
69- )
60+ const cached = cache . get ( cacheKey )
61+ if ( cached ) {
62+ return cached
63+ }
64+ const modifier = ( ( event , ...args ) => {
65+ for ( let i = 0 ; i < modifiers . length ; i ++ ) {
66+ const guard = modifierGuards [ modifiers [ i ] as ModifierGuards ]
67+ if ( guard && guard ( event , modifiers ) ) return
68+ }
69+ return fn ( event , ...args )
70+ } ) as T
71+ cache . set ( cacheKey , modifier )
72+ return modifier
7073}
7174
7275// Kept for 2.x compat.
@@ -88,7 +91,7 @@ const keyNames: Record<
8891 * @private
8992 */
9093export const withKeys = < T extends ( event : KeyboardEvent ) => any > (
91- fn : T & { _withKeys ?: { [ k : string ] : T } } ,
94+ fn : T & { _withKeys ?: Map < string , T > } ,
9295 modifiers : string [ ] ,
9396) : T => {
9497 let globalKeyCodes : LegacyConfig [ 'keyCodes' ]
@@ -110,54 +113,60 @@ export const withKeys = <T extends (event: KeyboardEvent) => any>(
110113 }
111114 }
112115
113- const cache : { [ k : string ] : T } = fn . _withKeys || ( fn . _withKeys = { } )
116+ const cache : Map < string , T > = fn . _withKeys || ( fn . _withKeys = new Map ( ) )
114117 const cacheKey = modifiers . join ( '.' )
115118
116- return (
117- cache [ cacheKey ] ||
118- ( cache [ cacheKey ] = ( event => {
119- if ( ! ( 'key' in event ) ) {
120- return
121- }
119+ const cached = cache . get ( cacheKey )
120+ if ( cached ) {
121+ return cached
122+ }
122123
123- const eventKey = hyphenate ( event . key )
124+ const withKey = ( event => {
125+ if ( ! ( 'key' in event ) ) {
126+ return
127+ }
128+
129+ const eventKey = hyphenate ( event . key )
130+ if (
131+ modifiers . some (
132+ k =>
133+ k === eventKey ||
134+ keyNames [ k as unknown as CompatModifiers ] === eventKey ,
135+ )
136+ ) {
137+ return fn ( event )
138+ }
139+
140+ if ( __COMPAT__ ) {
141+ const keyCode = String ( event . keyCode )
124142 if (
125- modifiers . some (
126- k =>
127- k === eventKey ||
128- keyNames [ k as unknown as CompatModifiers ] === eventKey ,
129- )
143+ compatUtils . isCompatEnabled (
144+ DeprecationTypes . V_ON_KEYCODE_MODIFIER ,
145+ instance ,
146+ ) &&
147+ modifiers . some ( mod => mod == keyCode )
130148 ) {
131149 return fn ( event )
132150 }
133-
134- if ( __COMPAT__ ) {
135- const keyCode = String ( event . keyCode )
136- if (
137- compatUtils . isCompatEnabled (
138- DeprecationTypes . V_ON_KEYCODE_MODIFIER ,
139- instance ,
140- ) &&
141- modifiers . some ( mod => mod == keyCode )
142- ) {
143- return fn ( event )
144- }
145- if ( globalKeyCodes ) {
146- for ( const mod of modifiers ) {
147- const codes = globalKeyCodes [ mod ]
148- if ( codes ) {
149- const matches = isArray ( codes )
150- ? codes . some ( code => String ( code ) === keyCode )
151- : String ( codes ) === keyCode
152- if ( matches ) {
153- return fn ( event )
154- }
151+ if ( globalKeyCodes ) {
152+ for ( const mod of modifiers ) {
153+ const codes = globalKeyCodes [ mod ]
154+ if ( codes ) {
155+ const matches = isArray ( codes )
156+ ? codes . some ( code => String ( code ) === keyCode )
157+ : String ( codes ) === keyCode
158+ if ( matches ) {
159+ return fn ( event )
155160 }
156161 }
157162 }
158163 }
159- } ) as T )
160- )
164+ }
165+ } ) as T
166+
167+ cache . set ( cacheKey , withKey )
168+
169+ return withKey
161170}
162171
163172export type VOnDirective = Directive < any , any , VOnModifiers >
0 commit comments