Skip to content

Commit

Permalink
Small decorators optimization (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator authored Feb 2, 2021
1 parent 5d41b64 commit 3979bee
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-hotels-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Decorators optimization
1 change: 0 additions & 1 deletion packages/mobx/src/api/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type Annotation = {
proxyTrap: boolean
): boolean | null
options_?: any
isDecorator_?: boolean
}

export type AnnotationMapEntry =
Expand Down
5 changes: 1 addition & 4 deletions packages/mobx/src/api/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ export function storeAnnotation(prototype: any, key: PropertyKey, annotation: An

// Ignore override
if (!isOverride(annotation)) {
prototype[storedAnnotationsSymbol][key] = {
...annotation,
isDecorator_: true
}
prototype[storedAnnotationsSymbol][key] = annotation
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/mobx/src/types/actionannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
isFunction,
Annotation,
recordAnnotationApplied,
globalState
globalState,
storedAnnotationsSymbol
} from "../internal"

export function createActionAnnotation(name: string, options?: object): Annotation {
Expand Down Expand Up @@ -61,7 +62,7 @@ function make_(adm: ObservableObjectAdministration, key: PropertyKey): void {
}
if (annotated) {
recordAnnotationApplied(adm, this, key)
} else if (!this.isDecorator_) {
} else if (!adm.target_[storedAnnotationsSymbol]?.[key]) {
// Throw on missing key, except for decorators:
// Decorator annotations are collected from whole prototype chain.
// When called from super() some props may not exist yet.
Expand Down
5 changes: 3 additions & 2 deletions packages/mobx/src/types/computedannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
objectPrototype,
die,
Annotation,
recordAnnotationApplied
recordAnnotationApplied,
storedAnnotationsSymbol
} from "../internal"

export function createComputedAnnotation(name: string, options?: object): Annotation {
Expand Down Expand Up @@ -36,7 +37,7 @@ function make_(adm: ObservableObjectAdministration, key: PropertyKey): void {
}
source = Object.getPrototypeOf(source)
}
if (!this.isDecorator_) {
if (!adm.target_[storedAnnotationsSymbol]?.[key]) {
// Throw on missing key, except for decorators:
// Decorator annotations are collected from whole prototype chain.
// When called from super() some props may not exist yet.
Expand Down
5 changes: 3 additions & 2 deletions packages/mobx/src/types/flowannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
isFlow,
recordAnnotationApplied,
isFunction,
globalState
globalState,
storedAnnotationsSymbol
} from "../internal"

export function createFlowAnnotation(name: string, options?: object): Annotation {
Expand Down Expand Up @@ -51,7 +52,7 @@ function make_(adm: ObservableObjectAdministration, key: PropertyKey): void {
}
if (annotated) {
recordAnnotationApplied(adm, this, key)
} else if (!this.isDecorator_) {
} else if (!adm.target_[storedAnnotationsSymbol]?.[key]) {
// Throw on missing key, except for decorators:
// Decorator annotations are collected from whole prototype chain.
// When called from super() some props may not exist yet.
Expand Down
5 changes: 3 additions & 2 deletions packages/mobx/src/types/observableannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
die,
Annotation,
recordAnnotationApplied,
objectPrototype
objectPrototype,
storedAnnotationsSymbol
} from "../internal"

export function createObservableAnnotation(name: string, options?: object): Annotation {
Expand Down Expand Up @@ -39,7 +40,7 @@ function make_(adm: ObservableObjectAdministration, key: PropertyKey): void {
}
source = Object.getPrototypeOf(source)
}
if (!this.isDecorator_) {
if (!adm.target_[storedAnnotationsSymbol]?.[key]) {
// Throw on missing key, except for decorators:
// Decorator annotations are collected from whole prototype chain.
// When called from super() some props may not exist yet.
Expand Down
4 changes: 1 addition & 3 deletions packages/mobx/src/types/observableobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,7 @@ export function recordAnnotationApplied(
adm.appliedAnnotations_![key] = annotation
}
// Remove applied decorator annotation so we don't try to apply it again in subclass constructor
if (annotation.isDecorator_) {
delete adm.target_[storedAnnotationsSymbol][key]
}
delete adm.target_[storedAnnotationsSymbol]?.[key]
}

function assertAnnotable(
Expand Down

0 comments on commit 3979bee

Please sign in to comment.