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

Conversation

dbenson24
Copy link

currently all functions returned by createAction are named res, which is fine but isn't helpful. I'd like to define the name of the function to be the actionName.

Thanks for taking the effort to create a PR!

If you are creating an extensive PR, you might want to open an issue with your idea first, so that you don't put a lot of effort in an PR that wouldn't be accepted. Please prepend pull requests with WIP: if they are not yet finished
PR checklist:

  • Added unit tests
  • Updated changelog
  • Updated docs (either in the description of this PR as markdown, or as separate PR on the gh-pages branch. Please refer to this PR). For new functionality, at least API.md should be updated
  • Added typescript typings
  • Verified that there is no significant performance drop (npm run perf)

Feel free to ask help with any of these boxes!

The above process doesn't apply to doc updates etc.

currently all functions returned by createAction are named res, which is fine but isn't helpful. I'd like to define the name of the function to be the actionName.
@mweststrate
Copy link
Member

Hey, thanks for this patch and idea!

Please note that function names are not always configurable (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name for details), so that would need to be checked on a per-browser basis first.

Secondly, I think it would be great if this would be done only in development mode.

@dbenson24
Copy link
Author

Restricted the feature to development only, I'm checking to make sure that the descriptor is exists and is configurable before I set a new one. I'm not sure if this is enough, do you have any examples of other browser specific code in the mobx codebase?

@@ -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?

Copy link
Member

@mweststrate mweststrate left a comment

Choose a reason for hiding this comment

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

See comments :)

@danielkcz
Copy link
Contributor

@dbenson24 Are you willing to continue with this PR?

@nadavkaner
Copy link
Contributor

Hey, can I continue with this? I opened a PR with the changes for this #2262

@danielkcz
Copy link
Contributor

@nadavkaner definitely :)

@danielkcz danielkcz closed this Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants