Skip to content

refactor(reactivity): extract isRef to shared #11202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
import { DirtyLevels, TrackOpTypes, TriggerOpTypes } from './constants'
import {
type IfAny,
type Ref,
hasChanged,
isArray,
isFunction,
isObject,
isRef,
} from '@vue/shared'
import {
isProxy,
Expand All @@ -27,19 +29,9 @@ import { ComputedRefImpl } from './computed'
import { getDepFromReactive } from './reactiveEffect'
import { warn } from './warning'

declare const RefSymbol: unique symbol
export type { Ref } from '@vue/shared'
export declare const RawSymbol: unique symbol

export interface Ref<T = any> {
value: T
/**
* Type differentiator only.
* We need this to be in public d.ts but don't want it to show up in IDE
* autocomplete, so we use a private Symbol instead.
*/
[RefSymbol]: true
}

type RefBase<T> = {
dep?: Dep
value: T
Expand Down Expand Up @@ -90,16 +82,7 @@ export function triggerRefValue(
}
}

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

/**
* Takes an inner value and returns a reactive and mutable ref object, which
Expand Down
15 changes: 15 additions & 0 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { makeMap } from './makeMap'
import type { Ref } from './typeUtils'

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

// @vue/shared can't have dependencies,
// but this is needed here for #7306

/**
* Checks if a value is a ref object.
*
* @param r - The value to inspect.
* @see {@link https://vuejs.org/api/reactivity-utilities.html#isref}
*/
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
export function isRef(r: any): r is Ref {
return !!(r && r.__v_isRef === true)
}

export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
return (
(isObject(val) || isFunction(val)) &&
Expand Down
6 changes: 1 addition & 5 deletions packages/shared/src/toDisplayString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import {
isMap,
isObject,
isPlainObject,
isRef,
isSet,
isString,
isSymbol,
objectToString,
} from './general'

// can't use isRef here since @vue/shared has no deps
const isRef = (val: any): val is { value: unknown } => {
return !!(val && val.__v_isRef === true)
}

/**
* For converting {{ interpolation }} values to displayed strings.
* @private
Expand Down
12 changes: 12 additions & 0 deletions packages/shared/src/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ export type Awaited<T> = T extends null | undefined
? Awaited<V> // recursively unwrap the value
: never // the argument to `then` was not callable
: T // non-object or non-thenable

declare const RefSymbol: unique symbol

export interface Ref<T = any> {
value: T
/**
* Type differentiator only.
* We need this to be in public d.ts but don't want it to show up in IDE
* autocomplete, so we use a private Symbol instead.
*/
[RefSymbol]: true
}