refactor(reactivity): adjust APIs
BREAKING CHANGE: Reactivity APIs adjustments: - `readonly` is now non-tracking if called on plain objects. `lock` and `unlock` have been removed. A `readonly` proxy can no longer be directly mutated. However, it can still wrap an already reactive object and track changes to the source reactive object. - `isReactive` now only returns true for proxies created by `reactive`, or a `readonly` proxy that wraps a `reactive` proxy. - A new utility `isProxy` is introduced, which returns true for both reactive or readonly proxies. - `markNonReactive` has been renamed to `markRaw`.
This commit is contained in:
@@ -11,13 +11,14 @@ export {
|
||||
} from './ref'
|
||||
export {
|
||||
reactive,
|
||||
isReactive,
|
||||
shallowReactive,
|
||||
readonly,
|
||||
isReactive,
|
||||
isReadonly,
|
||||
isProxy,
|
||||
shallowReactive,
|
||||
shallowReadonly,
|
||||
toRaw,
|
||||
markNonReactive
|
||||
markRaw,
|
||||
toRaw
|
||||
} from './reactive'
|
||||
export {
|
||||
computed,
|
||||
|
||||
@@ -20,7 +20,7 @@ const readonlyToRaw = new WeakMap<any, any>()
|
||||
|
||||
// WeakSets for values that are marked readonly or non-reactive during
|
||||
// observable creation.
|
||||
const nonReactiveValues = new WeakSet<any>()
|
||||
const rawValues = new WeakSet<any>()
|
||||
|
||||
const collectionTypes = new Set<Function>([Set, Map, WeakMap, WeakSet])
|
||||
const isObservableType = /*#__PURE__*/ makeMap(
|
||||
@@ -32,7 +32,7 @@ const canObserve = (value: any): boolean => {
|
||||
!value._isVue &&
|
||||
!value._isVNode &&
|
||||
isObservableType(toRawType(value)) &&
|
||||
!nonReactiveValues.has(value) &&
|
||||
!rawValues.has(value) &&
|
||||
!Object.isFrozen(value)
|
||||
)
|
||||
}
|
||||
@@ -132,19 +132,24 @@ function createReactiveObject(
|
||||
}
|
||||
|
||||
export function isReactive(value: unknown): boolean {
|
||||
return reactiveToRaw.has(value) || readonlyToRaw.has(value)
|
||||
value = readonlyToRaw.get(value) || value
|
||||
return reactiveToRaw.has(value)
|
||||
}
|
||||
|
||||
export function isReadonly(value: unknown): boolean {
|
||||
return readonlyToRaw.has(value)
|
||||
}
|
||||
|
||||
export function isProxy(value: unknown): boolean {
|
||||
return readonlyToRaw.has(value) || reactiveToRaw.has(value)
|
||||
}
|
||||
|
||||
export function toRaw<T>(observed: T): T {
|
||||
observed = readonlyToRaw.get(observed) || observed
|
||||
return reactiveToRaw.get(observed) || observed
|
||||
}
|
||||
|
||||
export function markNonReactive<T extends object>(value: T): T {
|
||||
nonReactiveValues.add(value)
|
||||
export function markRaw<T extends object>(value: T): T {
|
||||
rawValues.add(value)
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { track, trigger } from './effect'
|
||||
import { TrackOpTypes, TriggerOpTypes } from './operations'
|
||||
import { isObject } from '@vue/shared'
|
||||
import { reactive, isReactive } from './reactive'
|
||||
import { reactive, isProxy } from './reactive'
|
||||
import { ComputedRef } from './computed'
|
||||
import { CollectionTypes } from './collectionHandlers'
|
||||
|
||||
@@ -98,7 +98,7 @@ export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
|
||||
export function toRefs<T extends object>(
|
||||
object: T
|
||||
): { [K in keyof T]: Ref<T[K]> } {
|
||||
if (__DEV__ && !isReactive(object)) {
|
||||
if (__DEV__ && !isProxy(object)) {
|
||||
console.warn(`toRefs() expects a reactive object but received a plain one.`)
|
||||
}
|
||||
const ret: any = {}
|
||||
|
||||
Reference in New Issue
Block a user