Skip to content

Commit

Permalink
minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Matchlighter committed Jul 25, 2023
1 parent c5ff4f9 commit 63acafc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
4 changes: 3 additions & 1 deletion docs/enabling-decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ MobX' 2022.3 Decorators are very similar to the pre-MobX 6 decorators, so usage
- Addressing rest-destructuring, such is an anti-pattern in MobX - doing so would (likely unwantedly) touch all observables and make the observer overly-reactive).
- `@action some_field = () => {}` was and is valid usage (_if_ `makeObservable()` is also used). However, `@action accessor some_field = () => {}` is never valid.

## Legacy Decorators
<details id="legacy-decorators"><summary>Legacy Decorators<a href="#legacy-decorators" class="tip-anchor"></a></summary>

We do not recommend new codebases that use MobX use legacy decorators until the point when they become an official part of the language, but you can still use them. It does require setup for transpilation so you have to use Babel or TypeScript.

</details>

## MobX Core decorators {🚀}

MobX before version 6 encouraged the use of ES.next decorators to mark things as `observable`, `computed` and `action`. While MobX 6 recommends against using these decorators (and instead using [`makeObservable` / `makeAutoObservable`](observable-state.md)), it is still possible.
Expand Down
6 changes: 3 additions & 3 deletions packages/mobx/__tests__/decorators_20223/stage3-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,13 +682,13 @@ test("reusing initializers", () => {

test("enumerability", () => {
class A {
@observable accessor a = 1 // enumerable, on proto
@observable accessor a = 1
@computed
get b() {
return this.a
} // non-enumerable, (and, ideally, on proto)
}
@action
m() {} // non-enumerable, on proto
m() {}
}

const a = new A()
Expand Down
38 changes: 17 additions & 21 deletions packages/mobx/src/types/observableannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,26 @@ function decorate_20223_(
initializedObjects.add(target)
}

if (kind == "accessor") {
return {
get() {
if (!initializedObjects.has(this)) {
initializeObservable(this, desc.get.call(this))
}
return this[$mobx].getObservablePropValue_(name)
},
set(value) {
if (!initializedObjects.has(this)) {
initializeObservable(this, value)
}
return this[$mobx].setObservablePropValue_(name, value)
},
init(value) {
if (!initializedObjects.has(this)) {
initializeObservable(this, value)
}
return value
return {
get() {
if (!initializedObjects.has(this)) {
initializeObservable(this, desc.get.call(this))
}
return this[$mobx].getObservablePropValue_(name)
},
set(value) {
if (!initializedObjects.has(this)) {
initializeObservable(this, value)
}
return this[$mobx].setObservablePropValue_(name, value)
},
init(value) {
if (!initializedObjects.has(this)) {
initializeObservable(this, value)
}
return value
}
}

return
}

function assertObservableDescriptor(
Expand Down

0 comments on commit 63acafc

Please sign in to comment.