From 289e8746a201ed026eebb08180d9c94d666f4d29 Mon Sep 17 00:00:00 2001 From: Daniil Samoylov Date: Thu, 30 May 2024 14:41:45 +1200 Subject: [PATCH] fix: Allow action field decorators without makeObservable (#3879) --- .changeset/bright-tips-invite.md | 5 +++++ .../decorators_20223/stage3-decorators.ts | 16 ++++------------ packages/mobx/src/types/actionannotation.ts | 9 ++------- 3 files changed, 11 insertions(+), 19 deletions(-) create mode 100644 .changeset/bright-tips-invite.md diff --git a/.changeset/bright-tips-invite.md b/.changeset/bright-tips-invite.md new file mode 100644 index 000000000..c11821bc5 --- /dev/null +++ b/.changeset/bright-tips-invite.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +Fix 2022.3 @action decorators on fields no longer require makeObservable diff --git a/packages/mobx/__tests__/decorators_20223/stage3-decorators.ts b/packages/mobx/__tests__/decorators_20223/stage3-decorators.ts index e76c8f9fb..3dc2fa637 100644 --- a/packages/mobx/__tests__/decorators_20223/stage3-decorators.ts +++ b/packages/mobx/__tests__/decorators_20223/stage3-decorators.ts @@ -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 { @@ -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 { @@ -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) => { @@ -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) => { diff --git a/packages/mobx/src/types/actionannotation.ts b/packages/mobx/src/types/actionannotation.ts index 831cf5236..6b2f0d151 100644 --- a/packages/mobx/src/types/actionannotation.ts +++ b/packages/mobx/src/types/actionannotation.ts @@ -8,8 +8,7 @@ import { Annotation, globalState, MakeResult, - assert20223DecoratorType, - storeAnnotation + assert20223DecoratorType } from "../internal" export function createActionAnnotation(name: string, options?: object): Annotation { @@ -73,12 +72,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") {