refactor(reactivity): make readonly non-tracking

This commit is contained in:
Evan You
2020-04-14 23:49:46 -04:00
parent 09b44e07cb
commit 3178504273
11 changed files with 110 additions and 250 deletions

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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,

View File

@@ -14,6 +14,7 @@ export {
readonly,
isReadonly,
shallowReactive,
shallowReadonly,
markNonReactive,
toRaw
} from '@vue/reactivity'