feat(reactivity): add isShallow api
This commit is contained in:
parent
0c06c748a5
commit
9fda9411ec
@ -10,6 +10,7 @@ import {
|
|||||||
} from '../src/index'
|
} from '../src/index'
|
||||||
import { computed } from '@vue/runtime-dom'
|
import { computed } from '@vue/runtime-dom'
|
||||||
import { shallowRef, unref, customRef, triggerRef } from '../src/ref'
|
import { shallowRef, unref, customRef, triggerRef } from '../src/ref'
|
||||||
|
import { isShallow } from '../src/reactive'
|
||||||
|
|
||||||
describe('reactivity/ref', () => {
|
describe('reactivity/ref', () => {
|
||||||
it('should hold a value', () => {
|
it('should hold a value', () => {
|
||||||
@ -227,6 +228,10 @@ describe('reactivity/ref', () => {
|
|||||||
expect(dummy).toBe(2)
|
expect(dummy).toBe(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('shallowRef isShallow', () => {
|
||||||
|
expect(isShallow(shallowRef({ a: 1 }))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
test('isRef', () => {
|
test('isRef', () => {
|
||||||
expect(isRef(ref(1))).toBe(true)
|
expect(isRef(ref(1))).toBe(true)
|
||||||
expect(isRef(computed(() => 1))).toBe(true)
|
expect(isRef(computed(() => 1))).toBe(true)
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import { isReactive, reactive, shallowReactive } from '../src/reactive'
|
import {
|
||||||
|
isReactive,
|
||||||
|
isShallow,
|
||||||
|
reactive,
|
||||||
|
shallowReactive,
|
||||||
|
shallowReadonly
|
||||||
|
} from '../src/reactive'
|
||||||
|
|
||||||
import { effect } from '../src/effect'
|
import { effect } from '../src/effect'
|
||||||
|
|
||||||
@ -24,6 +30,11 @@ describe('shallowReactive', () => {
|
|||||||
expect(isReactive(reactiveProxy.foo)).toBe(true)
|
expect(isReactive(reactiveProxy.foo)).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('isShallow', () => {
|
||||||
|
expect(isShallow(shallowReactive({}))).toBe(true)
|
||||||
|
expect(isShallow(shallowReadonly({}))).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
describe('collections', () => {
|
describe('collections', () => {
|
||||||
test('should be reactive', () => {
|
test('should be reactive', () => {
|
||||||
const shallowSet = shallowReactive(new Set())
|
const shallowSet = shallowReactive(new Set())
|
||||||
|
@ -84,6 +84,8 @@ function createGetter(isReadonly = false, shallow = false) {
|
|||||||
return !isReadonly
|
return !isReadonly
|
||||||
} else if (key === ReactiveFlags.IS_READONLY) {
|
} else if (key === ReactiveFlags.IS_READONLY) {
|
||||||
return isReadonly
|
return isReadonly
|
||||||
|
} else if (key === ReactiveFlags.IS_SHALLOW) {
|
||||||
|
return shallow
|
||||||
} else if (
|
} else if (
|
||||||
key === ReactiveFlags.RAW &&
|
key === ReactiveFlags.RAW &&
|
||||||
receiver ===
|
receiver ===
|
||||||
|
@ -22,6 +22,7 @@ export {
|
|||||||
readonly,
|
readonly,
|
||||||
isReactive,
|
isReactive,
|
||||||
isReadonly,
|
isReadonly,
|
||||||
|
isShallow,
|
||||||
isProxy,
|
isProxy,
|
||||||
shallowReactive,
|
shallowReactive,
|
||||||
shallowReadonly,
|
shallowReadonly,
|
||||||
|
@ -17,6 +17,7 @@ export const enum ReactiveFlags {
|
|||||||
SKIP = '__v_skip',
|
SKIP = '__v_skip',
|
||||||
IS_REACTIVE = '__v_isReactive',
|
IS_REACTIVE = '__v_isReactive',
|
||||||
IS_READONLY = '__v_isReadonly',
|
IS_READONLY = '__v_isReadonly',
|
||||||
|
IS_SHALLOW = '__v_isShallow',
|
||||||
RAW = '__v_raw'
|
RAW = '__v_raw'
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +25,7 @@ export interface Target {
|
|||||||
[ReactiveFlags.SKIP]?: boolean
|
[ReactiveFlags.SKIP]?: boolean
|
||||||
[ReactiveFlags.IS_REACTIVE]?: boolean
|
[ReactiveFlags.IS_REACTIVE]?: boolean
|
||||||
[ReactiveFlags.IS_READONLY]?: boolean
|
[ReactiveFlags.IS_READONLY]?: boolean
|
||||||
|
[ReactiveFlags.IS_SHALLOW]?: boolean
|
||||||
[ReactiveFlags.RAW]?: any
|
[ReactiveFlags.RAW]?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +89,7 @@ export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>
|
|||||||
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
|
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
|
||||||
export function reactive(target: object) {
|
export function reactive(target: object) {
|
||||||
// if trying to observe a readonly proxy, return the readonly version.
|
// 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 target
|
||||||
}
|
}
|
||||||
return createReactiveObject(
|
return createReactiveObject(
|
||||||
@ -226,6 +228,10 @@ export function isReadonly(value: unknown): boolean {
|
|||||||
return !!(value && (value as Target)[ReactiveFlags.IS_READONLY])
|
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 {
|
export function isProxy(value: unknown): boolean {
|
||||||
return isReactive(value) || isReadonly(value)
|
return isReactive(value) || isReadonly(value)
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,6 @@ export interface Ref<T = any> {
|
|||||||
* autocomplete, so we use a private Symbol instead.
|
* autocomplete, so we use a private Symbol instead.
|
||||||
*/
|
*/
|
||||||
[RefSymbol]: true
|
[RefSymbol]: true
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
_shallow?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RefBase<T> = {
|
type RefBase<T> = {
|
||||||
@ -102,9 +98,9 @@ class RefImpl<T> {
|
|||||||
public dep?: Dep = undefined
|
public dep?: Dep = undefined
|
||||||
public readonly __v_isRef = true
|
public readonly __v_isRef = true
|
||||||
|
|
||||||
constructor(value: T, public readonly _shallow: boolean) {
|
constructor(value: T, public readonly __v_isShallow: boolean) {
|
||||||
this._rawValue = _shallow ? value : toRaw(value)
|
this._rawValue = __v_isShallow ? value : toRaw(value)
|
||||||
this._value = _shallow ? value : toReactive(value)
|
this._value = __v_isShallow ? value : toReactive(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
get value() {
|
get value() {
|
||||||
@ -113,10 +109,10 @@ class RefImpl<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
set value(newVal) {
|
set value(newVal) {
|
||||||
newVal = this._shallow ? newVal : toRaw(newVal)
|
newVal = this.__v_isShallow ? newVal : toRaw(newVal)
|
||||||
if (hasChanged(newVal, this._rawValue)) {
|
if (hasChanged(newVal, this._rawValue)) {
|
||||||
this._rawValue = newVal
|
this._rawValue = newVal
|
||||||
this._value = this._shallow ? newVal : toReactive(newVal)
|
this._value = this.__v_isShallow ? newVal : toReactive(newVal)
|
||||||
triggerRefValue(this, newVal)
|
triggerRefValue(this, newVal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
isRef,
|
isRef,
|
||||||
|
isShallow,
|
||||||
Ref,
|
Ref,
|
||||||
ComputedRef,
|
ComputedRef,
|
||||||
ReactiveEffect,
|
ReactiveEffect,
|
||||||
@ -205,7 +206,7 @@ function doWatch(
|
|||||||
|
|
||||||
if (isRef(source)) {
|
if (isRef(source)) {
|
||||||
getter = () => source.value
|
getter = () => source.value
|
||||||
forceTrigger = !!source._shallow
|
forceTrigger = isShallow(source)
|
||||||
} else if (isReactive(source)) {
|
} else if (isReactive(source)) {
|
||||||
getter = () => source
|
getter = () => source
|
||||||
deep = true
|
deep = true
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { isReactive, isReadonly, isRef, Ref, toRaw } from '@vue/reactivity'
|
import { isReactive, isReadonly, isRef, Ref, toRaw } from '@vue/reactivity'
|
||||||
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
|
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
|
||||||
|
import { isShallow } from '../../reactivity/src/reactive'
|
||||||
import { ComponentInternalInstance, ComponentOptions } from './component'
|
import { ComponentInternalInstance, ComponentOptions } from './component'
|
||||||
import { ComponentPublicInstance } from './componentPublicInstance'
|
import { ComponentPublicInstance } from './componentPublicInstance'
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ export function initCustomFormatter() {
|
|||||||
return [
|
return [
|
||||||
'div',
|
'div',
|
||||||
{},
|
{},
|
||||||
['span', vueStyle, 'Reactive'],
|
['span', vueStyle, isShallow(obj) ? 'ShallowReactive' : 'Reactive'],
|
||||||
'<',
|
'<',
|
||||||
formatValue(obj),
|
formatValue(obj),
|
||||||
`>${isReadonly(obj) ? ` (readonly)` : ``}`
|
`>${isReadonly(obj) ? ` (readonly)` : ``}`
|
||||||
@ -47,7 +48,7 @@ export function initCustomFormatter() {
|
|||||||
return [
|
return [
|
||||||
'div',
|
'div',
|
||||||
{},
|
{},
|
||||||
['span', vueStyle, 'Readonly'],
|
['span', vueStyle, isShallow(obj) ? 'ShallowReadonly' : 'Readonly'],
|
||||||
'<',
|
'<',
|
||||||
formatValue(obj),
|
formatValue(obj),
|
||||||
'>'
|
'>'
|
||||||
@ -181,7 +182,7 @@ export function initCustomFormatter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function genRefFlag(v: Ref) {
|
function genRefFlag(v: Ref) {
|
||||||
if (v._shallow) {
|
if (isShallow(v)) {
|
||||||
return `ShallowRef`
|
return `ShallowRef`
|
||||||
}
|
}
|
||||||
if ((v as any).effect) {
|
if ((v as any).effect) {
|
||||||
|
@ -15,6 +15,7 @@ export {
|
|||||||
isProxy,
|
isProxy,
|
||||||
isReactive,
|
isReactive,
|
||||||
isReadonly,
|
isReadonly,
|
||||||
|
isShallow,
|
||||||
// advanced
|
// advanced
|
||||||
customRef,
|
customRef,
|
||||||
triggerRef,
|
triggerRef,
|
||||||
|
Loading…
Reference in New Issue
Block a user