Skip to content

Commit ad8f7ca

Browse files
authored
refactor(reactivity): extract isRef to shared
1 parent 423b462 commit ad8f7ca

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

packages/reactivity/src/ref.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import {
88
import { DirtyLevels, TrackOpTypes, TriggerOpTypes } from './constants'
99
import {
1010
type IfAny,
11+
type Ref,
1112
hasChanged,
1213
isArray,
1314
isFunction,
1415
isObject,
16+
isRef,
1517
} from '@vue/shared'
1618
import {
1719
isProxy,
@@ -27,19 +29,9 @@ import { ComputedRefImpl } from './computed'
2729
import { getDepFromReactive } from './reactiveEffect'
2830
import { warn } from './warning'
2931

30-
declare const RefSymbol: unique symbol
32+
export type { Ref } from '@vue/shared'
3133
export declare const RawSymbol: unique symbol
3234

33-
export interface Ref<T = any> {
34-
value: T
35-
/**
36-
* Type differentiator only.
37-
* We need this to be in public d.ts but don't want it to show up in IDE
38-
* autocomplete, so we use a private Symbol instead.
39-
*/
40-
[RefSymbol]: true
41-
}
42-
4335
type RefBase<T> = {
4436
dep?: Dep
4537
value: T
@@ -90,16 +82,7 @@ export function triggerRefValue(
9082
}
9183
}
9284

93-
/**
94-
* Checks if a value is a ref object.
95-
*
96-
* @param r - The value to inspect.
97-
* @see {@link https://vuejs.org/api/reactivity-utilities.html#isref}
98-
*/
99-
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
100-
export function isRef(r: any): r is Ref {
101-
return !!(r && r.__v_isRef === true)
102-
}
85+
export { isRef } from '@vue/shared'
10386

10487
/**
10588
* Takes an inner value and returns a reactive and mutable ref object, which

packages/shared/src/general.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { makeMap } from './makeMap'
2+
import type { Ref } from './typeUtils'
23

34
export const EMPTY_OBJ: { readonly [key: string]: any } = __DEV__
45
? Object.freeze({})
@@ -52,6 +53,20 @@ export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
5253
export const isObject = (val: unknown): val is Record<any, any> =>
5354
val !== null && typeof val === 'object'
5455

56+
// @vue/shared can't have dependencies,
57+
// but this is needed here for #7306
58+
59+
/**
60+
* Checks if a value is a ref object.
61+
*
62+
* @param r - The value to inspect.
63+
* @see {@link https://vuejs.org/api/reactivity-utilities.html#isref}
64+
*/
65+
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
66+
export function isRef(r: any): r is Ref {
67+
return !!(r && r.__v_isRef === true)
68+
}
69+
5570
export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
5671
return (
5772
(isObject(val) || isFunction(val)) &&

packages/shared/src/toDisplayString.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ import {
44
isMap,
55
isObject,
66
isPlainObject,
7+
isRef,
78
isSet,
89
isString,
910
isSymbol,
1011
objectToString,
1112
} from './general'
1213

13-
// can't use isRef here since @vue/shared has no deps
14-
const isRef = (val: any): val is { value: unknown } => {
15-
return !!(val && val.__v_isRef === true)
16-
}
17-
1814
/**
1915
* For converting {{ interpolation }} values to displayed strings.
2016
* @private

packages/shared/src/typeUtils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ export type Awaited<T> = T extends null | undefined
2121
? Awaited<V> // recursively unwrap the value
2222
: never // the argument to `then` was not callable
2323
: T // non-object or non-thenable
24+
25+
declare const RefSymbol: unique symbol
26+
27+
export interface Ref<T = any> {
28+
value: T
29+
/**
30+
* Type differentiator only.
31+
* We need this to be in public d.ts but don't want it to show up in IDE
32+
* autocomplete, so we use a private Symbol instead.
33+
*/
34+
[RefSymbol]: true
35+
}

0 commit comments

Comments
 (0)