Skip to content

Commit

Permalink
chore: improve code style (#2579)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Dec 7, 2021
1 parent 483f79f commit 4a083ba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
44 changes: 22 additions & 22 deletions packages/reactive/src/annotations/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ export interface IComputed {
<T>(compute: { get?: () => T; set?: (value: T) => void }): IValue<T>
}

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 = {}
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion packages/reactive/src/checkers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const toString = Object.prototype.toString
export const isMap = (val: any): val is Map<any, any> =>
val && val instanceof Map
export const isSet = (val: any): val is Set<any> => val && val instanceof Set
Expand All @@ -8,7 +9,7 @@ export const isWeakSet = (val: any): val is WeakSet<any> =>
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 (
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/checkers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const toString = Object.prototype.toString
const isType =
<T>(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>('Object')
Expand Down

0 comments on commit 4a083ba

Please sign in to comment.