perf: avoid using WeakSet for isRef check

This commit is contained in:
Evan You 2019-09-30 10:52:50 -04:00
parent 7f06981f7c
commit 46bd9dbab0
4 changed files with 11 additions and 18 deletions

View File

@ -1,7 +1,7 @@
import { ref, effect, reactive, isRef, toRefs } from '../src/index'
import { computed } from '@vue/runtime-dom'
describe('reactivity/value', () => {
describe('reactivity/ref', () => {
it('should hold a value', () => {
const a = ref(1)
expect(a.value).toBe(1)

View File

@ -1,15 +1,15 @@
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
import { UnwrapNestedRefs, isRefSymbol, knownRefs } from './ref'
import { UnwrapNestedRefs } from './ref'
import { isFunction } from '@vue/shared'
export interface ComputedRef<T> {
[isRefSymbol]: true
_isRef: true
readonly value: UnwrapNestedRefs<T>
readonly effect: ReactiveEffect
}
export interface WritableComputedRef<T> {
[isRefSymbol]: true
_isRef: true
value: UnwrapNestedRefs<T>
readonly effect: ReactiveEffect
}
@ -45,7 +45,8 @@ export function computed<T>(
dirty = true
}
})
const computedRef = {
return {
_isRef: true,
// expose effect so computed can be stopped
effect: runner,
get value() {
@ -67,8 +68,6 @@ export function computed<T>(
}
}
}
knownRefs.add(computedRef)
return computedRef
}
function trackChildRun(childRunner: ReactiveEffect) {

View File

@ -3,14 +3,8 @@ import { OperationTypes } from './operations'
import { isObject } from '@vue/shared'
import { reactive } from './reactive'
export const knownRefs = new WeakSet()
export const isRefSymbol = Symbol()
export interface Ref<T> {
// this is a type-only field to avoid objects with 'value' property being
// treated as a ref by TypeScript
[isRefSymbol]: true
_isRef: true
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> {
raw = convert(raw)
const v = {
_isRef: true,
get value() {
track(v, OperationTypes.GET, '')
return raw
@ -30,12 +25,11 @@ export function ref<T>(raw: T): Ref<T> {
trigger(v, OperationTypes.SET, '')
}
}
knownRefs.add(v)
return v as Ref<T>
}
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>(
@ -53,6 +47,7 @@ function toProxyRef<T extends object, K extends keyof T>(
key: K
): Ref<T[K]> {
const v = {
_isRef: true,
get value() {
return object[key]
},
@ -60,7 +55,6 @@ function toProxyRef<T extends object, K extends keyof T>(
object[key] = newVal
}
}
knownRefs.add(v)
return v as Ref<T[K]>
}

View File

@ -126,7 +126,7 @@ export function createBlock(
}
export function isVNode(value: any): boolean {
return value && value._isVNode
return value ? value._isVNode === true : false
}
export function createVNode(