Skip to content

Commit

Permalink
chore: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed May 29, 2022
1 parent a21182d commit 9164a28
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/models/Field.ts
Expand Up @@ -159,10 +159,10 @@ export class Field<
decoratorType: observable.ref,
componentType: observable.ref,
content: observable.ref,
feedbacks: observable.ref,
decoratorProps: observable,
componentProps: observable,
validator: observable.shallow,
feedbacks: observable.shallow,
data: observable.shallow,
component: observable.computed,
decorator: observable.computed,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/main.scss
Expand Up @@ -13,4 +13,5 @@
@import './form/main.scss';
@import './preview-text/main.scss';
@import './select-table/main.scss';
@import './space/main.scss';
@import './upload/main.scss';
2 changes: 1 addition & 1 deletion packages/next/src/style.ts
Expand Up @@ -13,5 +13,5 @@ import './form-layout/main.scss'
import './form/main.scss'
import './preview-text/main.scss'
import './select-table/main.scss'
import './upload/main.scss'
import './space/main.scss'
import './upload/main.scss'
22 changes: 13 additions & 9 deletions packages/reactive/src/annotations/computed.ts
Expand Up @@ -45,22 +45,26 @@ function getPropertyDescriptorCache(obj: any, key: PropertyKey) {
return newDesc
}

function getGetterAndSetter(target: any, key: PropertyKey, value: any) {
function getPrototypeDescriptor(
target: any,
key: PropertyKey,
value: any
): PropertyDescriptor {
if (!target) {
if (value) {
if (isFn(value)) {
return [value]
return { get: value }
} else {
return [value.get, value.set]
return value
}
}
return []
return {}
}
const descriptor = getPropertyDescriptorCache(target, key)
if (descriptor) {
return [descriptor.get, descriptor.set]
return descriptor
}
return []
return {}
}

export const computed: IComputed = createAnnotation(
Expand All @@ -71,10 +75,10 @@ export const computed: IComputed = createAnnotation(

const context = target ? target : store
const property = target ? key : 'value'
const [getter, setter] = getGetterAndSetter(target, property, value)
const descriptor = getPrototypeDescriptor(target, property, value)

function compute() {
store.value = getter?.call(context)
store.value = descriptor.get?.call(context)
}
function reaction() {
if (ReactionStack.indexOf(reaction) === -1) {
Expand Down Expand Up @@ -126,7 +130,7 @@ export const computed: IComputed = createAnnotation(
function set(value: any) {
try {
batchStart()
setter?.call(context, value)
descriptor.set?.call(context, value)
} finally {
batchEnd()
}
Expand Down

0 comments on commit 9164a28

Please sign in to comment.