perf: avoid using WeakSet for isRef check
This commit is contained in:
parent
7f06981f7c
commit
46bd9dbab0
@ -1,7 +1,7 @@
|
|||||||
import { ref, effect, reactive, isRef, toRefs } from '../src/index'
|
import { ref, effect, reactive, isRef, toRefs } from '../src/index'
|
||||||
import { computed } from '@vue/runtime-dom'
|
import { computed } from '@vue/runtime-dom'
|
||||||
|
|
||||||
describe('reactivity/value', () => {
|
describe('reactivity/ref', () => {
|
||||||
it('should hold a value', () => {
|
it('should hold a value', () => {
|
||||||
const a = ref(1)
|
const a = ref(1)
|
||||||
expect(a.value).toBe(1)
|
expect(a.value).toBe(1)
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
|
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
|
||||||
import { UnwrapNestedRefs, isRefSymbol, knownRefs } from './ref'
|
import { UnwrapNestedRefs } from './ref'
|
||||||
import { isFunction } from '@vue/shared'
|
import { isFunction } from '@vue/shared'
|
||||||
|
|
||||||
export interface ComputedRef<T> {
|
export interface ComputedRef<T> {
|
||||||
[isRefSymbol]: true
|
_isRef: true
|
||||||
readonly value: UnwrapNestedRefs<T>
|
readonly value: UnwrapNestedRefs<T>
|
||||||
readonly effect: ReactiveEffect
|
readonly effect: ReactiveEffect
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WritableComputedRef<T> {
|
export interface WritableComputedRef<T> {
|
||||||
[isRefSymbol]: true
|
_isRef: true
|
||||||
value: UnwrapNestedRefs<T>
|
value: UnwrapNestedRefs<T>
|
||||||
readonly effect: ReactiveEffect
|
readonly effect: ReactiveEffect
|
||||||
}
|
}
|
||||||
@ -45,7 +45,8 @@ export function computed<T>(
|
|||||||
dirty = true
|
dirty = true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const computedRef = {
|
return {
|
||||||
|
_isRef: true,
|
||||||
// expose effect so computed can be stopped
|
// expose effect so computed can be stopped
|
||||||
effect: runner,
|
effect: runner,
|
||||||
get value() {
|
get value() {
|
||||||
@ -67,8 +68,6 @@ export function computed<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
knownRefs.add(computedRef)
|
|
||||||
return computedRef
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function trackChildRun(childRunner: ReactiveEffect) {
|
function trackChildRun(childRunner: ReactiveEffect) {
|
||||||
|
@ -3,14 +3,8 @@ import { OperationTypes } from './operations'
|
|||||||
import { isObject } from '@vue/shared'
|
import { isObject } from '@vue/shared'
|
||||||
import { reactive } from './reactive'
|
import { reactive } from './reactive'
|
||||||
|
|
||||||
export const knownRefs = new WeakSet()
|
|
||||||
|
|
||||||
export const isRefSymbol = Symbol()
|
|
||||||
|
|
||||||
export interface Ref<T> {
|
export interface Ref<T> {
|
||||||
// this is a type-only field to avoid objects with 'value' property being
|
_isRef: true
|
||||||
// treated as a ref by TypeScript
|
|
||||||
[isRefSymbol]: true
|
|
||||||
value: UnwrapNestedRefs<T>
|
value: UnwrapNestedRefs<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,6 +15,7 @@ const convert = (val: any): any => (isObject(val) ? reactive(val) : val)
|
|||||||
export function ref<T>(raw: T): Ref<T> {
|
export function ref<T>(raw: T): Ref<T> {
|
||||||
raw = convert(raw)
|
raw = convert(raw)
|
||||||
const v = {
|
const v = {
|
||||||
|
_isRef: true,
|
||||||
get value() {
|
get value() {
|
||||||
track(v, OperationTypes.GET, '')
|
track(v, OperationTypes.GET, '')
|
||||||
return raw
|
return raw
|
||||||
@ -30,12 +25,11 @@ export function ref<T>(raw: T): Ref<T> {
|
|||||||
trigger(v, OperationTypes.SET, '')
|
trigger(v, OperationTypes.SET, '')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
knownRefs.add(v)
|
|
||||||
return v as Ref<T>
|
return v as Ref<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isRef(v: any): v is Ref<any> {
|
export function isRef(v: any): v is Ref<any> {
|
||||||
return knownRefs.has(v)
|
return v ? v._isRef === true : false
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toRefs<T extends object>(
|
export function toRefs<T extends object>(
|
||||||
@ -53,6 +47,7 @@ function toProxyRef<T extends object, K extends keyof T>(
|
|||||||
key: K
|
key: K
|
||||||
): Ref<T[K]> {
|
): Ref<T[K]> {
|
||||||
const v = {
|
const v = {
|
||||||
|
_isRef: true,
|
||||||
get value() {
|
get value() {
|
||||||
return object[key]
|
return object[key]
|
||||||
},
|
},
|
||||||
@ -60,7 +55,6 @@ function toProxyRef<T extends object, K extends keyof T>(
|
|||||||
object[key] = newVal
|
object[key] = newVal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
knownRefs.add(v)
|
|
||||||
return v as Ref<T[K]>
|
return v as Ref<T[K]>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ export function createBlock(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isVNode(value: any): boolean {
|
export function isVNode(value: any): boolean {
|
||||||
return value && value._isVNode
|
return value ? value._isVNode === true : false
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createVNode(
|
export function createVNode(
|
||||||
|
Loading…
Reference in New Issue
Block a user