perf(ssr): optimize setup context creation for ssr in v8

This commit is contained in:
Evan You 2024-04-12 16:02:52 +08:00
parent 6af733d68e
commit ca84316bfb
2 changed files with 29 additions and 35 deletions

View File

@ -1004,16 +1004,11 @@ export function finishComponentSetup(
} }
} }
function getAttrsProxy(instance: ComponentInternalInstance): Data { const attrsProxyHandlers = __DEV__
return (
instance.attrsProxy ||
(instance.attrsProxy = new Proxy(
instance.attrs,
__DEV__
? { ? {
get(target, key: string) { get(target: Data, key: string) {
markAttrsAccessed() markAttrsAccessed()
track(instance, TrackOpTypes.GET, '$attrs') track(target, TrackOpTypes.GET, '')
return target[key] return target[key]
}, },
set() { set() {
@ -1026,14 +1021,11 @@ function getAttrsProxy(instance: ComponentInternalInstance): Data {
}, },
} }
: { : {
get(target, key: string) { get(target: Data, key: string) {
track(instance, TrackOpTypes.GET, '$attrs') track(target, TrackOpTypes.GET, '')
return target[key] return target[key]
}, },
}, }
))
)
}
/** /**
* Dev-only * Dev-only
@ -1080,9 +1072,13 @@ export function createSetupContext(
if (__DEV__) { if (__DEV__) {
// We use getters in dev in case libs like test-utils overwrite instance // We use getters in dev in case libs like test-utils overwrite instance
// properties (overwrites should not be done in prod) // properties (overwrites should not be done in prod)
let attrsProxy: Data
return Object.freeze({ return Object.freeze({
get attrs() { get attrs() {
return getAttrsProxy(instance) return (
attrsProxy ||
(attrsProxy = new Proxy(instance.attrs, attrsProxyHandlers))
)
}, },
get slots() { get slots() {
return getSlotsProxy(instance) return getSlotsProxy(instance)
@ -1094,9 +1090,7 @@ export function createSetupContext(
}) })
} else { } else {
return { return {
get attrs() { attrs: new Proxy(instance.attrs, attrsProxyHandlers),
return getAttrsProxy(instance)
},
slots: instance.slots, slots: instance.slots,
emit: instance.emit, emit: instance.emit,
expose, expose,

View File

@ -365,7 +365,7 @@ export function updateProps(
// trigger updates for $attrs in case it's used in component slots // trigger updates for $attrs in case it's used in component slots
if (hasAttrsChanged) { if (hasAttrsChanged) {
trigger(instance, TriggerOpTypes.SET, '$attrs') trigger(instance.attrs, TriggerOpTypes.SET, '')
} }
if (__DEV__) { if (__DEV__) {