refactor(reactivity): make readonly non-tracking
This commit is contained in:
@@ -3,7 +3,8 @@ import {
|
||||
render,
|
||||
getCurrentInstance,
|
||||
nodeOps,
|
||||
createApp
|
||||
createApp,
|
||||
shallowReadonly
|
||||
} from '@vue/runtime-test'
|
||||
import { mockWarn } from '@vue/shared'
|
||||
import { ComponentInternalInstance } from '../src/component'
|
||||
@@ -85,10 +86,10 @@ describe('component: proxy', () => {
|
||||
}
|
||||
render(h(Comp), nodeOps.createElement('div'))
|
||||
expect(instanceProxy.$data).toBe(instance!.data)
|
||||
expect(instanceProxy.$props).toBe(instance!.props)
|
||||
expect(instanceProxy.$attrs).toBe(instance!.attrs)
|
||||
expect(instanceProxy.$slots).toBe(instance!.slots)
|
||||
expect(instanceProxy.$refs).toBe(instance!.refs)
|
||||
expect(instanceProxy.$props).toBe(shallowReadonly(instance!.props))
|
||||
expect(instanceProxy.$attrs).toBe(shallowReadonly(instance!.attrs))
|
||||
expect(instanceProxy.$slots).toBe(shallowReadonly(instance!.slots))
|
||||
expect(instanceProxy.$refs).toBe(shallowReadonly(instance!.refs))
|
||||
expect(instanceProxy.$parent).toBe(
|
||||
instance!.parent && instance!.parent.proxy
|
||||
)
|
||||
|
||||
@@ -3,7 +3,8 @@ import {
|
||||
reactive,
|
||||
ReactiveEffect,
|
||||
pauseTracking,
|
||||
resetTracking
|
||||
resetTracking,
|
||||
shallowReadonly
|
||||
} from '@vue/reactivity'
|
||||
import {
|
||||
ComponentPublicInstance,
|
||||
@@ -347,7 +348,7 @@ function setupStatefulComponent(
|
||||
setup,
|
||||
instance,
|
||||
ErrorCodes.SETUP_FUNCTION,
|
||||
[instance.props, setupContext]
|
||||
[__DEV__ ? shallowReadonly(instance.props) : instance.props, setupContext]
|
||||
)
|
||||
resetTracking()
|
||||
currentInstance = null
|
||||
@@ -479,17 +480,6 @@ function finishComponentSetup(
|
||||
}
|
||||
}
|
||||
|
||||
const slotsHandlers: ProxyHandler<InternalSlots> = {
|
||||
set: () => {
|
||||
warn(`setupContext.slots is readonly.`)
|
||||
return false
|
||||
},
|
||||
deleteProperty: () => {
|
||||
warn(`setupContext.slots is readonly.`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const attrHandlers: ProxyHandler<Data> = {
|
||||
get: (target, key: string) => {
|
||||
markAttrsAccessed()
|
||||
@@ -514,7 +504,7 @@ function createSetupContext(instance: ComponentInternalInstance): SetupContext {
|
||||
return new Proxy(instance.attrs, attrHandlers)
|
||||
},
|
||||
get slots() {
|
||||
return new Proxy(instance.slots, slotsHandlers)
|
||||
return shallowReadonly(instance.slots)
|
||||
},
|
||||
get emit() {
|
||||
return (event: string, ...args: any[]) => instance.emit(event, ...args)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { toRaw, lock, unlock, shallowReadonly } from '@vue/reactivity'
|
||||
import { toRaw, shallowReactive } from '@vue/reactivity'
|
||||
import {
|
||||
EMPTY_OBJ,
|
||||
camelize,
|
||||
@@ -114,7 +114,7 @@ export function initProps(
|
||||
|
||||
if (isStateful) {
|
||||
// stateful
|
||||
instance.props = isSSR ? props : shallowReadonly(props)
|
||||
instance.props = isSSR ? props : shallowReactive(props)
|
||||
} else {
|
||||
if (!options) {
|
||||
// functional w/ optional props, props === attrs
|
||||
@@ -132,9 +132,6 @@ export function updateProps(
|
||||
rawProps: Data | null,
|
||||
optimized: boolean
|
||||
) {
|
||||
// allow mutation of propsProxy (which is readonly by default)
|
||||
unlock()
|
||||
|
||||
const {
|
||||
props,
|
||||
attrs,
|
||||
@@ -205,9 +202,6 @@ export function updateProps(
|
||||
}
|
||||
}
|
||||
|
||||
// lock readonly
|
||||
lock()
|
||||
|
||||
if (__DEV__ && rawOptions && rawProps) {
|
||||
validateProps(props, rawOptions)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,12 @@ import { ComponentInternalInstance, Data } from './component'
|
||||
import { nextTick, queueJob } from './scheduler'
|
||||
import { instanceWatch } from './apiWatch'
|
||||
import { EMPTY_OBJ, hasOwn, isGloballyWhitelisted, NOOP } from '@vue/shared'
|
||||
import { ReactiveEffect, UnwrapRef, toRaw } from '@vue/reactivity'
|
||||
import {
|
||||
ReactiveEffect,
|
||||
UnwrapRef,
|
||||
toRaw,
|
||||
shallowReadonly
|
||||
} from '@vue/reactivity'
|
||||
import {
|
||||
ExtractComputedReturns,
|
||||
ComponentOptionsBase,
|
||||
@@ -57,10 +62,10 @@ const publicPropertiesMap: Record<
|
||||
$: i => i,
|
||||
$el: i => i.vnode.el,
|
||||
$data: i => i.data,
|
||||
$props: i => i.props,
|
||||
$attrs: i => i.attrs,
|
||||
$slots: i => i.slots,
|
||||
$refs: i => i.refs,
|
||||
$props: i => (__DEV__ ? shallowReadonly(i.props) : i.props),
|
||||
$attrs: i => (__DEV__ ? shallowReadonly(i.attrs) : i.attrs),
|
||||
$slots: i => (__DEV__ ? shallowReadonly(i.slots) : i.slots),
|
||||
$refs: i => (__DEV__ ? shallowReadonly(i.refs) : i.refs),
|
||||
$parent: i => i.parent && i.parent.proxy,
|
||||
$root: i => i.root && i.root.proxy,
|
||||
$emit: i => i.emit,
|
||||
|
||||
@@ -14,6 +14,7 @@ export {
|
||||
readonly,
|
||||
isReadonly,
|
||||
shallowReactive,
|
||||
shallowReadonly,
|
||||
markNonReactive,
|
||||
toRaw
|
||||
} from '@vue/reactivity'
|
||||
|
||||
Reference in New Issue
Block a user