-
Notifications
You must be signed in to change notification settings - Fork 31
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
feat: bindAllMethods util function #1476
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1476 +/- ##
==========================================
+ Coverage 45.75% 45.77% +0.02%
==========================================
Files 515 517 +2
Lines 35073 35116 +43
Branches 8784 8792 +8
==========================================
+ Hits 16048 16076 +28
- Misses 18974 18989 +15
Partials 51 51
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry. |
packages/utils/src/ClassUtils.ts
Outdated
instance[methodName] as (...args: unknown[]) => unknown | ||
).bind(instance) as T[typeof methodName]; |
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.
Since you have to cast here anyway, I don't think the generic type is worth the added complexity
packages/utils/src/ClassUtils.ts
Outdated
let current = instance; | ||
|
||
// Traverse the prototype chain until we get to Object.prototype | ||
while (current != null && current !== Object.prototype) { |
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.
Might not want to iterate all the way back through the prototype chain? Not sure if this could cause anything to break in a React class component if it were used in one
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.
Yes, I had that thought as well. I could see parameterizing it with a flag or something
packages/utils/src/ClassUtils.ts
Outdated
current = Object.getPrototypeOf(current); | ||
} | ||
|
||
return [...methodNames.keys()].sort(); |
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.
Why sort?
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.
Was easier for testing, but I could just sort in the test I guess
Util functions for binding all methods of an object to itself.
resolves #1474