Skip to content

Commit

Permalink
fix: Allow action field decorators without makeObservable (mobxjs#3879)
Browse files Browse the repository at this point in the history
  • Loading branch information
Obi-Dann committed May 30, 2024
1 parent 44a5fe0 commit 7129ce4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-tips-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Fix 2022.3 @action decorators on fields no longer require makeObservable
16 changes: 4 additions & 12 deletions packages/mobx/__tests__/decorators_20223/stage3-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ function normalizeSpyEvents(events: any[]) {

test("action decorator (2022.3)", () => {
class Store {
constructor(private multiplier: number) {
makeObservable(this)
}
constructor(private multiplier: number) {}

@action
add(a: number, b: number): number {
Expand Down Expand Up @@ -366,9 +364,7 @@ test("action decorator (2022.3)", () => {

test("custom action decorator (2022.3)", () => {
class Store {
constructor(private multiplier: number) {
makeObservable(this)
}
constructor(private multiplier: number) {}

@action("zoem zoem")
add(a: number, b: number): number {
Expand Down Expand Up @@ -416,9 +412,7 @@ test("custom action decorator (2022.3)", () => {

test("action decorator on field (2022.3)", () => {
class Store {
constructor(private multiplier: number) {
makeObservable(this)
}
constructor(private multiplier: number) {}

@action
add = (a: number, b: number) => {
Expand Down Expand Up @@ -450,9 +444,7 @@ test("action decorator on field (2022.3)", () => {

test("custom action decorator on field (2022.3)", () => {
class Store {
constructor(private multiplier: number) {
makeObservable(this)
}
constructor(private multiplier: number) {}

@action("zoem zoem")
add = (a: number, b: number) => {
Expand Down
6 changes: 1 addition & 5 deletions packages/mobx/src/types/actionannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ function decorate_20223_(this: Annotation, mthd, context: DecoratorContext) {
const _createAction = m =>
createAction(ann.options_?.name ?? name!.toString(), m, ann.options_?.autoAction ?? false)

// Backwards/Legacy behavior, expects makeObservable(this)
if (kind == "field") {
addInitializer(function () {
storeAnnotation(this, name, ann)
})
return
return _createAction
}

if (kind == "method") {
Expand Down

0 comments on commit 7129ce4

Please sign in to comment.