Skip to content

Commit 289e874

Browse files
committed
fix: Allow action field decorators without makeObservable (mobxjs#3879)
1 parent 44a5fe0 commit 289e874

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

.changeset/bright-tips-invite.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx": patch
3+
---
4+
5+
Fix 2022.3 @action decorators on fields no longer require makeObservable

packages/mobx/__tests__/decorators_20223/stage3-decorators.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ function normalizeSpyEvents(events: any[]) {
334334

335335
test("action decorator (2022.3)", () => {
336336
class Store {
337-
constructor(private multiplier: number) {
338-
makeObservable(this)
339-
}
337+
constructor(private multiplier: number) {}
340338

341339
@action
342340
add(a: number, b: number): number {
@@ -366,9 +364,7 @@ test("action decorator (2022.3)", () => {
366364

367365
test("custom action decorator (2022.3)", () => {
368366
class Store {
369-
constructor(private multiplier: number) {
370-
makeObservable(this)
371-
}
367+
constructor(private multiplier: number) {}
372368

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

417413
test("action decorator on field (2022.3)", () => {
418414
class Store {
419-
constructor(private multiplier: number) {
420-
makeObservable(this)
421-
}
415+
constructor(private multiplier: number) {}
422416

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

451445
test("custom action decorator on field (2022.3)", () => {
452446
class Store {
453-
constructor(private multiplier: number) {
454-
makeObservable(this)
455-
}
447+
constructor(private multiplier: number) {}
456448

457449
@action("zoem zoem")
458450
add = (a: number, b: number) => {

packages/mobx/src/types/actionannotation.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
Annotation,
99
globalState,
1010
MakeResult,
11-
assert20223DecoratorType,
12-
storeAnnotation
11+
assert20223DecoratorType
1312
} from "../internal"
1413

1514
export function createActionAnnotation(name: string, options?: object): Annotation {
@@ -73,12 +72,8 @@ function decorate_20223_(this: Annotation, mthd, context: DecoratorContext) {
7372
const _createAction = m =>
7473
createAction(ann.options_?.name ?? name!.toString(), m, ann.options_?.autoAction ?? false)
7574

76-
// Backwards/Legacy behavior, expects makeObservable(this)
7775
if (kind == "field") {
78-
addInitializer(function () {
79-
storeAnnotation(this, name, ann)
80-
})
81-
return
76+
return _createAction
8277
}
8378

8479
if (kind == "method") {

0 commit comments

Comments
 (0)