-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
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.
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. |
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") { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments :)
@dbenson24 Are you willing to continue with this PR? |
Hey, can I continue with this? I opened a PR with the changes for this #2262 |
@nadavkaner definitely :) |
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 finishedPR checklist:
gh-pages
branch. Please refer to this PR). For new functionality, at least API.md should be updatednpm run perf
)Feel free to ask help with any of these boxes!
The above process doesn't apply to doc updates etc.