Skip to content

Commit f8eba75

Browse files
authored
chore(reactivity): change literal flag properties to enum flag properties (#10367)
1 parent 189573d commit f8eba75

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

Diff for: packages/reactivity/src/computed.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export class ComputedRefImpl<T = any> implements Subscriber {
4747
/**
4848
* @internal
4949
*/
50-
readonly dep = new Dep(this)
50+
readonly dep = new Dep(this);
5151
/**
5252
* @internal
5353
*/
54-
readonly __v_isRef = true;
54+
readonly [ReactiveFlags.IS_REF] = true;
5555
/**
5656
* @internal
5757
*/
@@ -96,7 +96,7 @@ export class ComputedRefImpl<T = any> implements Subscriber {
9696
private readonly setter: ComputedSetter<T> | undefined,
9797
isSSR: boolean,
9898
) {
99-
this.__v_isReadonly = !setter
99+
this[ReactiveFlags.IS_READONLY] = !setter
100100
this.isSSR = isSSR
101101
}
102102

Diff for: packages/reactivity/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export enum ReactiveFlags {
2020
IS_READONLY = '__v_isReadonly',
2121
IS_SHALLOW = '__v_isShallow',
2222
RAW = '__v_raw',
23+
IS_REF = '__v_isRef',
2324
}
2425

2526
export enum DirtyLevels {

Diff for: packages/reactivity/src/ref.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
toReactive,
1717
} from './reactive'
1818
import type { ComputedRef } from './computed'
19-
import { TrackOpTypes, TriggerOpTypes } from './constants'
19+
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
2020
import { warn } from './warning'
2121

2222
declare const RefSymbol: unique symbol
@@ -40,7 +40,7 @@ export interface Ref<T = any> {
4040
*/
4141
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
4242
export function isRef(r: any): r is Ref {
43-
return r ? r.__v_isRef === true : false
43+
return r ? r[ReactiveFlags.IS_REF] === true : false
4444
}
4545

4646
/**
@@ -105,14 +105,13 @@ class RefImpl<T = any> {
105105

106106
dep: Dep = new Dep()
107107

108-
public readonly __v_isRef = true
108+
public readonly [ReactiveFlags.IS_REF] = true
109+
public readonly [ReactiveFlags.IS_SHALLOW]: boolean = false
109110

110-
constructor(
111-
value: T,
112-
public readonly __v_isShallow: boolean,
113-
) {
114-
this._rawValue = __v_isShallow ? value : toRaw(value)
115-
this._value = __v_isShallow ? value : toReactive(value)
111+
constructor(value: T, isShallow: boolean) {
112+
this._rawValue = isShallow ? value : toRaw(value)
113+
this._value = isShallow ? value : toReactive(value)
114+
this[ReactiveFlags.IS_SHALLOW] = isShallow
116115
}
117116

118117
get value() {
@@ -131,7 +130,9 @@ class RefImpl<T = any> {
131130
set value(newValue) {
132131
const oldValue = this._rawValue
133132
const useDirectValue =
134-
this.__v_isShallow || isShallow(newValue) || isReadonly(newValue)
133+
this[ReactiveFlags.IS_SHALLOW] ||
134+
isShallow(newValue) ||
135+
isReadonly(newValue)
135136
newValue = useDirectValue ? newValue : toRaw(newValue)
136137
if (hasChanged(newValue, oldValue)) {
137138
this._rawValue = newValue
@@ -277,7 +278,7 @@ class CustomRefImpl<T> {
277278
private readonly _get: ReturnType<CustomRefFactory<T>>['get']
278279
private readonly _set: ReturnType<CustomRefFactory<T>>['set']
279280

280-
public readonly __v_isRef = true
281+
public readonly [ReactiveFlags.IS_REF] = true
281282

282283
constructor(factory: CustomRefFactory<T>) {
283284
const dep = (this.dep = new Dep())
@@ -330,7 +331,7 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
330331
}
331332

332333
class ObjectRefImpl<T extends object, K extends keyof T> {
333-
public readonly __v_isRef = true
334+
public readonly [ReactiveFlags.IS_REF] = true
334335

335336
constructor(
336337
private readonly _object: T,
@@ -353,8 +354,8 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
353354
}
354355

355356
class GetterRefImpl<T> {
356-
public readonly __v_isRef = true
357-
public readonly __v_isReadonly = true
357+
public readonly [ReactiveFlags.IS_REF] = true
358+
public readonly [ReactiveFlags.IS_READONLY] = true
358359
constructor(private readonly _getter: () => T) {}
359360
get value() {
360361
return this._getter()

Diff for: packages/shared/src/toDisplayString.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ReactiveFlags } from '@vue/reactivity'
12
import {
23
isArray,
34
isFunction,
@@ -28,7 +29,7 @@ export const toDisplayString = (val: unknown): string => {
2829

2930
const replacer = (_key: string, val: any): any => {
3031
// can't use isRef here since @vue/shared has no deps
31-
if (val && val.__v_isRef) {
32+
if (val && val[ReactiveFlags.IS_REF]) {
3233
return replacer(_key, val.value)
3334
} else if (isMap(val)) {
3435
return {

0 commit comments

Comments
 (0)