diff --git a/packages/reactive/src/annotations/computed.ts b/packages/reactive/src/annotations/computed.ts index 10be6fd57fc..d2db718812e 100644 --- a/packages/reactive/src/annotations/computed.ts +++ b/packages/reactive/src/annotations/computed.ts @@ -20,6 +20,26 @@ export interface IComputed { (compute: { get?: () => T; set?: (value: T) => void }): IValue } +function getGetter(target: any, key: PropertyKey, value: any) { + if (!target) { + if (value && value.get) return value.get + return value + } + const descriptor = Object.getOwnPropertyDescriptor(target, key) + if (descriptor && descriptor.get) return descriptor.get + return getGetter(Object.getPrototypeOf(target), key, value) +} + +function getSetter(target: any, key: PropertyKey, value: any) { + if (!target) { + if (value && value.set) return value.set + return + } + const descriptor = Object.getOwnPropertyDescriptor(target, key) + if (descriptor && descriptor.set) return descriptor.set + return getSetter(Object.getPrototypeOf(target), key, value) +} + export const computed: IComputed = createAnnotation( ({ target, key, value }) => { const store: IValue = {} @@ -28,28 +48,8 @@ export const computed: IComputed = createAnnotation( const context = target ? target : store const property = target ? key : 'value' - const getter = getGetter(context) - const setter = getSetter(context) - - function getGetter(target: any) { - if (!target) { - if (value && value.get) return value.get - return value - } - const descriptor = Object.getOwnPropertyDescriptor(target, property) - if (descriptor && descriptor.get) return descriptor.get - return getGetter(Object.getPrototypeOf(target)) - } - - function getSetter(target: any) { - if (!target) { - if (value && value.set) return value.set - return - } - const descriptor = Object.getOwnPropertyDescriptor(target, property) - if (descriptor && descriptor.set) return descriptor.set - return getSetter(Object.getPrototypeOf(target)) - } + const getter = getGetter(context, property, value) + const setter = getSetter(context, property, value) function compute() { store.value = getter?.call?.(context) diff --git a/packages/reactive/src/checkers.ts b/packages/reactive/src/checkers.ts index f50442b57d1..1fd8bb16579 100644 --- a/packages/reactive/src/checkers.ts +++ b/packages/reactive/src/checkers.ts @@ -1,3 +1,4 @@ +const toString = Object.prototype.toString export const isMap = (val: any): val is Map => val && val instanceof Map export const isSet = (val: any): val is Set => val && val instanceof Set @@ -8,7 +9,7 @@ export const isWeakSet = (val: any): val is WeakSet => export const isFn = (val: any): val is Function => typeof val === 'function' export const isArr = Array.isArray export const isPlainObj = (val: any): val is object => - Object.prototype.toString.call(val) === '[object Object]' + toString.call(val) === '[object Object]' export const isValid = (val: any) => val !== null && val !== undefined export const isCollectionType = (target: any) => { return ( diff --git a/packages/shared/src/checkers.ts b/packages/shared/src/checkers.ts index e519e0fdd0a..dfdb446c5e3 100644 --- a/packages/shared/src/checkers.ts +++ b/packages/shared/src/checkers.ts @@ -1,8 +1,9 @@ +const toString = Object.prototype.toString const isType = (type: string | string[]) => (obj: unknown): obj is T => getType(obj) === `[object ${type}]` -export const getType = (obj: any) => Object.prototype.toString.call(obj) +export const getType = (obj: any) => toString.call(obj) export const isFn = (val: any): val is Function => typeof val === 'function' export const isArr = Array.isArray export const isPlainObj = isType('Object')