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

define the name property of function return by createAction #2131

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 6 additions & 0 deletions src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export function createAction(actionName: string, fn: Function, ref?: Object): Fu
return executeAction(actionName, fn, ref || this, arguments)
}
;(res as any).isMobxAction = true
if (process.env.NODE_ENV !== "production") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing it this way will run the check every time an action is created, however, since this code might be hit hunderds of thousands times, and the result only depends on the environment, you only need to run it once. I suggest to pull this code up, run the check only once during module initialization, and reuse the result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(either name is configurable on all Functions, or none)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, could you add a unit test verifying this behavior?

const descriptor = Object.getOwnPropertyDescriptor(res, "name");
if (descriptor && descriptor.configurable) {
Object.defineProperty(res, "name", {value: actionName});
}
}
return res as any
}

Expand Down