Skip to content

Commit 0873123

Browse files
committed
refactor: reduce overhead
1 parent accff03 commit 0873123

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

packages/runtime-core/src/component.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ export interface GenericComponentInstance {
379379
*/
380380
propsDefaults: Data | null
381381
/**
382-
* used for getting the keys of a component's raw props
382+
* used for getting the keys of a component's raw props, vapor only
383383
* @internal
384384
*/
385-
getKeysFromRawProps: () => string[] | undefined
385+
rawKeys?: () => string[]
386386

387387
// exposed properties via expose()
388388
exposed: Record<string, any> | null
@@ -736,7 +736,6 @@ export function createComponentInstance(
736736

737737
// props default value
738738
propsDefaults: null,
739-
getKeysFromRawProps: null!, // to be set immediately
740739

741740
// inheritAttrs
742741
inheritAttrs: type.inheritAttrs,
@@ -784,10 +783,6 @@ export function createComponentInstance(
784783
}
785784
instance.root = parent ? parent.root : instance
786785
instance.emit = emit.bind(null, instance)
787-
instance.getKeysFromRawProps = () => {
788-
const { props } = instance.vnode
789-
if (props) return Object.keys(props)
790-
}
791786

792787
// apply custom element special handling
793788
if (vnode.ce) {

packages/runtime-core/src/helpers/useModel.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { type Ref, customRef, ref } from '@vue/reactivity'
22
import { EMPTY_OBJ, camelize, hasChanged, hyphenate } from '@vue/shared'
33
import type { DefineModelOptions, ModelRef } from '../apiSetupHelpers'
4-
import { getCurrentGenericInstance } from '../component'
4+
import {
5+
type ComponentInternalInstance,
6+
getCurrentGenericInstance,
7+
} from '../component'
58
import { warn } from '../warning'
69
import type { NormalizedProps } from '../componentProps'
710
import { watchSyncEffect } from '../apiWatch'
@@ -65,19 +68,38 @@ export function useModel(
6568
) {
6669
return
6770
}
68-
const rawPropKeys = i.getKeysFromRawProps()
69-
if (
70-
!(
71-
rawPropKeys &&
72-
// check if parent has passed v-model
73-
(rawPropKeys.includes(name) ||
74-
rawPropKeys.includes(camelizedName) ||
75-
rawPropKeys.includes(hyphenatedName)) &&
76-
(rawPropKeys.includes(`onUpdate:${name}`) ||
77-
rawPropKeys.includes(`onUpdate:${camelizedName}`) ||
78-
rawPropKeys.includes(`onUpdate:${hyphenatedName}`))
79-
)
80-
) {
71+
72+
let rawPropKeys
73+
let parentPassedModelValue = false
74+
let parentPassedModelUpdater = false
75+
76+
if (i.rawKeys) {
77+
// vapor instance
78+
rawPropKeys = i.rawKeys()
79+
} else {
80+
const rawProps = (i as ComponentInternalInstance).vnode!.props
81+
rawPropKeys = rawProps && Object.keys(rawProps)
82+
}
83+
84+
if (rawPropKeys) {
85+
for (const key of rawPropKeys) {
86+
if (
87+
key === name ||
88+
key === camelizedName ||
89+
key === hyphenatedName
90+
) {
91+
parentPassedModelValue = true
92+
} else if (
93+
key === `onUpdate:${name}` ||
94+
key === `onUpdate:${camelizedName}` ||
95+
key === `onUpdate:${hyphenatedName}`
96+
) {
97+
parentPassedModelUpdater = true
98+
}
99+
}
100+
}
101+
102+
if (!parentPassedModelValue || !parentPassedModelUpdater) {
81103
// no v-model, local update
82104
localValue = value
83105
trigger()

packages/runtime-vapor/src/component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ export class VaporComponentInstance implements GenericComponentInstance {
285285
props: Record<string, any>
286286
attrs: Record<string, any>
287287
propsDefaults: Record<string, any> | null
288-
getKeysFromRawProps: () => string[] | undefined
289288

290289
slots: StaticSlots
291290

@@ -395,7 +394,6 @@ export class VaporComponentInstance implements GenericComponentInstance {
395394
} else {
396395
this.props = this.attrs = EMPTY_OBJ
397396
}
398-
this.getKeysFromRawProps = () => getKeysFromRawProps(this.rawProps)
399397

400398
// init slots
401399
this.rawSlots = rawSlots || EMPTY_OBJ
@@ -413,6 +411,14 @@ export class VaporComponentInstance implements GenericComponentInstance {
413411
this.emitsOptions = normalizeEmitsOptions(comp)
414412
}
415413
}
414+
415+
/**
416+
* Expose `getKeysFromRawProps` on the instance so it can be used in code
417+
* paths where it's needed, e.g. `useModel`
418+
*/
419+
rawKeys(): string[] {
420+
return getKeysFromRawProps(this.rawProps)
421+
}
416422
}
417423

418424
export function isVaporComponent(

0 commit comments

Comments
 (0)