Skip to content

Commit

Permalink
perf(ssr): optimize setup context creation for ssr in v8
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 12, 2024
1 parent 6af733d commit ca84316
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
62 changes: 28 additions & 34 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,36 +1004,28 @@ export function finishComponentSetup(
}
}

function getAttrsProxy(instance: ComponentInternalInstance): Data {
return (
instance.attrsProxy ||
(instance.attrsProxy = new Proxy(
instance.attrs,
__DEV__
? {
get(target, key: string) {
markAttrsAccessed()
track(instance, TrackOpTypes.GET, '$attrs')
return target[key]
},
set() {
warn(`setupContext.attrs is readonly.`)
return false
},
deleteProperty() {
warn(`setupContext.attrs is readonly.`)
return false
},
}
: {
get(target, key: string) {
track(instance, TrackOpTypes.GET, '$attrs')
return target[key]
},
},
))
)
}
const attrsProxyHandlers = __DEV__
? {
get(target: Data, key: string) {
markAttrsAccessed()
track(target, TrackOpTypes.GET, '')
return target[key]
},
set() {
warn(`setupContext.attrs is readonly.`)
return false
},
deleteProperty() {
warn(`setupContext.attrs is readonly.`)
return false
},
}
: {
get(target: Data, key: string) {
track(target, TrackOpTypes.GET, '')
return target[key]
},
}

/**
* Dev-only
Expand Down Expand Up @@ -1080,9 +1072,13 @@ export function createSetupContext(
if (__DEV__) {
// We use getters in dev in case libs like test-utils overwrite instance
// properties (overwrites should not be done in prod)
let attrsProxy: Data
return Object.freeze({
get attrs() {
return getAttrsProxy(instance)
return (
attrsProxy ||
(attrsProxy = new Proxy(instance.attrs, attrsProxyHandlers))
)
},
get slots() {
return getSlotsProxy(instance)
Expand All @@ -1094,9 +1090,7 @@ export function createSetupContext(
})
} else {
return {
get attrs() {
return getAttrsProxy(instance)
},
attrs: new Proxy(instance.attrs, attrsProxyHandlers),
slots: instance.slots,
emit: instance.emit,
expose,
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export function updateProps(

// trigger updates for $attrs in case it's used in component slots
if (hasAttrsChanged) {
trigger(instance, TriggerOpTypes.SET, '$attrs')
trigger(instance.attrs, TriggerOpTypes.SET, '')
}

if (__DEV__) {
Expand Down

0 comments on commit ca84316

Please sign in to comment.