types: avoid plain objects with value being mistaken as refs

This commit is contained in:
Evan You 2019-09-05 18:32:19 -04:00
parent 9b90e673e8
commit 6c7cbb0dc9
4 changed files with 35 additions and 19 deletions

View File

@ -16,7 +16,7 @@ function get(target: any, key: any, wrap: (t: any) => any): any {
return wrap(res) return wrap(res)
} }
function has(key: any): boolean { function has(this: any, key: any): boolean {
const target = toRaw(this) const target = toRaw(this)
key = toRaw(key) key = toRaw(key)
const proto: any = Reflect.getPrototypeOf(target) const proto: any = Reflect.getPrototypeOf(target)
@ -31,7 +31,7 @@ function size(target: any) {
return Reflect.get(proto, 'size', target) return Reflect.get(proto, 'size', target)
} }
function add(value: any) { function add(this: any, value: any) {
value = toRaw(value) value = toRaw(value)
const target = toRaw(this) const target = toRaw(this)
const proto: any = Reflect.getPrototypeOf(this) const proto: any = Reflect.getPrototypeOf(this)
@ -48,7 +48,7 @@ function add(value: any) {
return result return result
} }
function set(key: any, value: any) { function set(this: any, key: any, value: any) {
value = toRaw(value) value = toRaw(value)
const target = toRaw(this) const target = toRaw(this)
const proto: any = Reflect.getPrototypeOf(this) const proto: any = Reflect.getPrototypeOf(this)
@ -75,7 +75,7 @@ function set(key: any, value: any) {
return result return result
} }
function deleteEntry(key: any) { function deleteEntry(this: any, key: any) {
const target = toRaw(this) const target = toRaw(this)
const proto: any = Reflect.getPrototypeOf(this) const proto: any = Reflect.getPrototypeOf(this)
const hadKey = proto.has.call(target, key) const hadKey = proto.has.call(target, key)
@ -93,7 +93,7 @@ function deleteEntry(key: any) {
return result return result
} }
function clear() { function clear(this: any) {
const target = toRaw(this) const target = toRaw(this)
const proto: any = Reflect.getPrototypeOf(this) const proto: any = Reflect.getPrototypeOf(this)
const hadItems = target.size !== 0 const hadItems = target.size !== 0
@ -112,7 +112,7 @@ function clear() {
} }
function createForEach(isReadonly: boolean) { function createForEach(isReadonly: boolean) {
return function forEach(callback: Function, thisArg?: any) { return function forEach(this: any, callback: Function, thisArg?: any) {
const observed = this const observed = this
const target = toRaw(observed) const target = toRaw(observed)
const proto: any = Reflect.getPrototypeOf(target) const proto: any = Reflect.getPrototypeOf(target)
@ -129,7 +129,7 @@ function createForEach(isReadonly: boolean) {
} }
function createIterableMethod(method: string | symbol, isReadonly: boolean) { function createIterableMethod(method: string | symbol, isReadonly: boolean) {
return function(...args: any[]) { return function(this: any, ...args: any[]) {
const target = toRaw(this) const target = toRaw(this)
const proto: any = Reflect.getPrototypeOf(target) const proto: any = Reflect.getPrototypeOf(target)
const isPair = const isPair =
@ -163,7 +163,7 @@ function createReadonlyMethod(
method: Function, method: Function,
type: OperationTypes type: OperationTypes
): Function { ): Function {
return function(...args: any[]) { return function(this: any, ...args: any[]) {
if (LOCKED) { if (LOCKED) {
if (__DEV__) { if (__DEV__) {
const key = args[0] ? `on key "${args[0]}" ` : `` const key = args[0] ? `on key "${args[0]}" ` : ``

View File

@ -1,27 +1,38 @@
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect' import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
import { UnwrapNestedRefs, knownRefs, Ref } from './ref' import { UnwrapNestedRefs, isRefSymbol, knownRefs } from './ref'
import { isFunction } from '@vue/shared' import { isFunction } from '@vue/shared'
export interface ComputedRef<T> { export interface ComputedRef<T> {
[isRefSymbol]: true
readonly value: UnwrapNestedRefs<T> readonly value: UnwrapNestedRefs<T>
readonly effect: ReactiveEffect readonly effect: ReactiveEffect
} }
export interface ComputedOptions<T> { export interface WritableComputedRef<T> {
[isRefSymbol]: true
value: UnwrapNestedRefs<T>
readonly effect: ReactiveEffect
}
export interface WritableComputedOptions<T> {
get: () => T get: () => T
set: (v: T) => void set: (v: T) => void
} }
export function computed<T>(getter: () => T): ComputedRef<T> export function computed<T>(getter: () => T): ComputedRef<T>
export function computed<T>(options: ComputedOptions<T>): Ref<T>
export function computed<T>( export function computed<T>(
getterOrOptions: (() => T) | ComputedOptions<T> options: WritableComputedOptions<T>
): Ref<T> { ): WritableComputedRef<T>
export function computed<T>(
getterOrOptions: (() => T) | WritableComputedOptions<T>
): any {
const isReadonly = isFunction(getterOrOptions) const isReadonly = isFunction(getterOrOptions)
const getter = isReadonly const getter = isReadonly
? (getterOrOptions as (() => T)) ? (getterOrOptions as (() => T))
: (getterOrOptions as ComputedOptions<T>).get : (getterOrOptions as WritableComputedOptions<T>).get
const setter = isReadonly ? null : (getterOrOptions as ComputedOptions<T>).set const setter = isReadonly
? null
: (getterOrOptions as WritableComputedOptions<T>).set
let dirty: boolean = true let dirty: boolean = true
let value: any = undefined let value: any = undefined
@ -34,7 +45,7 @@ export function computed<T>(
dirty = true dirty = true
} }
}) })
const computedValue = { const computedRef = {
// expose effect so computed can be stopped // expose effect so computed can be stopped
effect: runner, effect: runner,
get value() { get value() {
@ -56,8 +67,8 @@ export function computed<T>(
} }
} }
} }
knownRefs.add(computedValue) knownRefs.add(computedRef)
return computedValue return computedRef
} }
function trackChildRun(childRunner: ReactiveEffect) { function trackChildRun(childRunner: ReactiveEffect) {

View File

@ -8,7 +8,7 @@ export {
markReadonly, markReadonly,
markNonReactive markNonReactive
} from './reactive' } from './reactive'
export { computed, ComputedRef, ComputedOptions } from './computed' export { computed, ComputedRef, WritableComputedOptions } from './computed'
export { export {
effect, effect,
stop, stop,

View File

@ -5,7 +5,12 @@ import { reactive } from './reactive'
export const knownRefs = new WeakSet() 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
// treated as a ref by TypeScript
[isRefSymbol]: true
value: UnwrapNestedRefs<T> value: UnwrapNestedRefs<T>
} }