Skip to content

Commit 370fc82

Browse files
committed
refactor(runtime-core): refactor instance public proxy context object
1 parent b2662a6 commit 370fc82

File tree

6 files changed

+131
-154
lines changed

6 files changed

+131
-154
lines changed

packages/runtime-core/__tests__/componentProxy.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('component: proxy', () => {
116116
render(h(Comp), nodeOps.createElement('div'))
117117
instanceProxy.foo = 1
118118
expect(instanceProxy.foo).toBe(1)
119-
expect(instance!.proxyTarget.foo).toBe(1)
119+
expect(instance!.ctx.foo).toBe(1)
120120
})
121121

122122
test('globalProperties', () => {
@@ -141,7 +141,7 @@ describe('component: proxy', () => {
141141
// set should overwrite globalProperties with local
142142
instanceProxy.foo = 2
143143
// expect(instanceProxy.foo).toBe(2)
144-
expect(instance!.proxyTarget.foo).toBe(2)
144+
expect(instance!.ctx.foo).toBe(2)
145145
// should not affect global
146146
expect(app.config.globalProperties.foo).toBe(1)
147147
})
@@ -177,7 +177,7 @@ describe('component: proxy', () => {
177177
expect('msg' in instanceProxy).toBe(true)
178178
// data
179179
expect('foo' in instanceProxy).toBe(true)
180-
// renderContext
180+
// ctx
181181
expect('bar' in instanceProxy).toBe(true)
182182
// public properties
183183
expect('$el' in instanceProxy).toBe(true)

packages/runtime-core/src/component.ts

+23-25
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import {
88
} from '@vue/reactivity'
99
import {
1010
ComponentPublicInstance,
11-
ComponentPublicProxyTarget,
1211
PublicInstanceProxyHandlers,
1312
RuntimeCompiledPublicInstanceProxyHandlers,
14-
createDevProxyTarget,
15-
exposePropsOnDevProxyTarget,
16-
exposeSetupStateOnDevProxyTarget
13+
createRenderContext,
14+
exposePropsOnRenderContext,
15+
exposeSetupStateOnRenderContext
1716
} from './componentProxy'
1817
import { ComponentPropsOptions, initProps } from './componentProps'
1918
import { Slots, initSlots, InternalSlots } from './componentSlots'
@@ -136,30 +135,30 @@ export interface ComponentInternalInstance {
136135
components: Record<string, Component>
137136
directives: Record<string, Directive>
138137

139-
// the rest are only for stateful components
140-
renderContext: Data
138+
// the rest are only for stateful components ---------------------------------
139+
140+
// main proxy that serves as the public instance (`this`)
141+
proxy: ComponentPublicInstance | null
142+
// alternative proxy used only for runtime-compiled render functions using
143+
// `with` block
144+
withProxy: ComponentPublicInstance | null
145+
// This is the target for the public instance proxy. It also holds properties
146+
// injected by user options (computed, methods etc.) and user-attached
147+
// custom properties (via `this.x = ...`)
148+
ctx: Data
149+
150+
// internal state
141151
data: Data
142152
props: Data
143153
attrs: Data
144154
slots: InternalSlots
145-
proxy: ComponentPublicInstance | null
146155
refs: Data
147156
emit: EmitFn
148157

149158
// setup
150159
setupState: Data
151160
setupContext: SetupContext | null
152161

153-
// The target object for the public instance proxy. In dev mode, we also
154-
// define getters for all known instance properties on it so it can be
155-
// properly inspected in the console. These getters are skipped in prod mode
156-
// for performance. In addition, any user attached properties
157-
// (via `this.x = ...`) are also stored on this object.
158-
proxyTarget: ComponentPublicProxyTarget
159-
// alternative proxy used only for runtime-compiled render functions using
160-
// `with` block
161-
withProxy: ComponentPublicInstance | null
162-
163162
// suspense related
164163
suspense: SuspenseBoundary | null
165164
asyncDep: Promise<any> | null
@@ -211,15 +210,14 @@ export function createComponentInstance(
211210
update: null!, // will be set synchronously right after creation
212211
render: null,
213212
proxy: null,
214-
proxyTarget: null!, // to be immediately set
215213
withProxy: null,
216214
effects: null,
217215
provides: parent ? parent.provides : Object.create(appContext.provides),
218216
accessCache: null!,
219217
renderCache: [],
220218

221219
// state
222-
renderContext: EMPTY_OBJ,
220+
ctx: EMPTY_OBJ,
223221
data: EMPTY_OBJ,
224222
props: EMPTY_OBJ,
225223
attrs: EMPTY_OBJ,
@@ -258,9 +256,9 @@ export function createComponentInstance(
258256
emit: null as any // to be set immediately
259257
}
260258
if (__DEV__) {
261-
instance.proxyTarget = createDevProxyTarget(instance)
259+
instance.ctx = createRenderContext(instance)
262260
} else {
263-
instance.proxyTarget = { _: instance }
261+
instance.ctx = { _: instance }
264262
}
265263
instance.root = parent ? parent.root : instance
266264
instance.emit = emit.bind(null, instance)
@@ -335,9 +333,9 @@ function setupStatefulComponent(
335333
// 0. create render proxy property access cache
336334
instance.accessCache = {}
337335
// 1. create public instance / render proxy
338-
instance.proxy = new Proxy(instance.proxyTarget, PublicInstanceProxyHandlers)
336+
instance.proxy = new Proxy(instance.ctx, PublicInstanceProxyHandlers)
339337
if (__DEV__) {
340-
exposePropsOnDevProxyTarget(instance)
338+
exposePropsOnRenderContext(instance)
341339
}
342340
// 2. call setup()
343341
const { setup } = Component
@@ -399,7 +397,7 @@ export function handleSetupResult(
399397
// assuming a render function compiled from template is present.
400398
instance.setupState = reactive(setupResult)
401399
if (__DEV__) {
402-
exposeSetupStateOnDevProxyTarget(instance)
400+
exposeSetupStateOnRenderContext(instance)
403401
}
404402
} else if (__DEV__ && setupResult !== undefined) {
405403
warn(
@@ -469,7 +467,7 @@ function finishComponentSetup(
469467
// also only allows a whitelist of globals to fallthrough.
470468
if (instance.render._rc) {
471469
instance.withProxy = new Proxy(
472-
instance.proxyTarget,
470+
instance.ctx,
473471
RuntimeCompiledPublicInstanceProxyHandlers
474472
)
475473
}

0 commit comments

Comments
 (0)