} from '../src/index'
import { computed } from '@vue/runtime-dom'
import { shallowRef, unref, customRef, triggerRef } from '../src/ref'
+import { isShallow } from '../src/reactive'
describe('reactivity/ref', () => {
it('should hold a value', () => {
expect(dummy).toBe(2)
})
+ test('shallowRef isShallow', () => {
+ expect(isShallow(shallowRef({ a: 1 }))).toBe(true)
+ })
+
test('isRef', () => {
expect(isRef(ref(1))).toBe(true)
expect(isRef(computed(() => 1))).toBe(true)
-import { isReactive, reactive, shallowReactive } from '../src/reactive'
+import {
+ isReactive,
+ isShallow,
+ reactive,
+ shallowReactive,
+ shallowReadonly
+} from '../src/reactive'
import { effect } from '../src/effect'
expect(isReactive(reactiveProxy.foo)).toBe(true)
})
+ test('isShallow', () => {
+ expect(isShallow(shallowReactive({}))).toBe(true)
+ expect(isShallow(shallowReadonly({}))).toBe(true)
+ })
+
describe('collections', () => {
test('should be reactive', () => {
const shallowSet = shallowReactive(new Set())
return !isReadonly
} else if (key === ReactiveFlags.IS_READONLY) {
return isReadonly
+ } else if (key === ReactiveFlags.IS_SHALLOW) {
+ return shallow
} else if (
key === ReactiveFlags.RAW &&
receiver ===
readonly,
isReactive,
isReadonly,
+ isShallow,
isProxy,
shallowReactive,
shallowReadonly,
SKIP = '__v_skip',
IS_REACTIVE = '__v_isReactive',
IS_READONLY = '__v_isReadonly',
+ IS_SHALLOW = '__v_isShallow',
RAW = '__v_raw'
}
[ReactiveFlags.SKIP]?: boolean
[ReactiveFlags.IS_REACTIVE]?: boolean
[ReactiveFlags.IS_READONLY]?: boolean
+ [ReactiveFlags.IS_SHALLOW]?: boolean
[ReactiveFlags.RAW]?: any
}
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
export function reactive(target: object) {
// if trying to observe a readonly proxy, return the readonly version.
- if (target && (target as Target)[ReactiveFlags.IS_READONLY]) {
+ if (isReadonly(target)) {
return target
}
return createReactiveObject(
return !!(value && (value as Target)[ReactiveFlags.IS_READONLY])
}
+export function isShallow(value: unknown): boolean {
+ return !!(value && (value as Target)[ReactiveFlags.IS_SHALLOW])
+}
+
export function isProxy(value: unknown): boolean {
return isReactive(value) || isReadonly(value)
}
* autocomplete, so we use a private Symbol instead.
*/
[RefSymbol]: true
- /**
- * @internal
- */
- _shallow?: boolean
}
type RefBase<T> = {
public dep?: Dep = undefined
public readonly __v_isRef = true
- constructor(value: T, public readonly _shallow: boolean) {
- this._rawValue = _shallow ? value : toRaw(value)
- this._value = _shallow ? value : toReactive(value)
+ constructor(value: T, public readonly __v_isShallow: boolean) {
+ this._rawValue = __v_isShallow ? value : toRaw(value)
+ this._value = __v_isShallow ? value : toReactive(value)
}
get value() {
}
set value(newVal) {
- newVal = this._shallow ? newVal : toRaw(newVal)
+ newVal = this.__v_isShallow ? newVal : toRaw(newVal)
if (hasChanged(newVal, this._rawValue)) {
this._rawValue = newVal
- this._value = this._shallow ? newVal : toReactive(newVal)
+ this._value = this.__v_isShallow ? newVal : toReactive(newVal)
triggerRefValue(this, newVal)
}
}
import {
isRef,
+ isShallow,
Ref,
ComputedRef,
ReactiveEffect,
if (isRef(source)) {
getter = () => source.value
- forceTrigger = !!source._shallow
+ forceTrigger = isShallow(source)
} else if (isReactive(source)) {
getter = () => source
deep = true
import { isReactive, isReadonly, isRef, Ref, toRaw } from '@vue/reactivity'
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
+import { isShallow } from '../../reactivity/src/reactive'
import { ComponentInternalInstance, ComponentOptions } from './component'
import { ComponentPublicInstance } from './componentPublicInstance'
return [
'div',
{},
- ['span', vueStyle, 'Reactive'],
+ ['span', vueStyle, isShallow(obj) ? 'ShallowReactive' : 'Reactive'],
'<',
formatValue(obj),
`>${isReadonly(obj) ? ` (readonly)` : ``}`
return [
'div',
{},
- ['span', vueStyle, 'Readonly'],
+ ['span', vueStyle, isShallow(obj) ? 'ShallowReadonly' : 'Readonly'],
'<',
formatValue(obj),
'>'
}
function genRefFlag(v: Ref) {
- if (v._shallow) {
+ if (isShallow(v)) {
return `ShallowRef`
}
if ((v as any).effect) {
isProxy,
isReactive,
isReadonly,
+ isShallow,
// advanced
customRef,
triggerRef,