Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow action field decorators without makeObservable (#3879) #3883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 2 additions & 7 deletions packages/mobx/src/types/actionannotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
Annotation,
globalState,
MakeResult,
assert20223DecoratorType,
storeAnnotation
assert20223DecoratorType
} from "../internal"

export function createActionAnnotation(name: string, options?: object): Annotation {
Expand Down Expand Up @@ -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") {
Expand Down