-
Notifications
You must be signed in to change notification settings - Fork 66
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 shared instance fields #79
base: master
Are you sure you want to change the base?
Conversation
src/__tests__/test.js
Outdated
@@ -68,6 +68,36 @@ describe('autobind method decorator: @boundMethod', () => { | |||
assert(value === 50); | |||
}); | |||
|
|||
test('should not share instance methods between instances', () => { | |||
const a = new A('foo'); | |||
const b = new A('bar'); |
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.
This test seems too much. Why can't we just change a method on a
and see if that method is the same on b
?
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.
I think we also need a test:
Once instance method is changed, it shouldn't be bound.
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.
For the first point: don't think that this will work, methods won't be equal, they will be bound instances of the same underlying function. So theoretically we need to check underlying method. Though I think that test case may be made cleaner. And just realised that
const aOriginalGetValue = a.getValue;
a.getValue = function () {
return 'foo' + '-' + aOriginalGetValue();
};
And
a.getValue = function () {
return 'fake-foo';
};
Will hit different code paths, will need to cover this too
For the second point: this one is interesting. Are you saing that if we run setter with new function it shouldn't be auto-bound? If yes, this is not how existing code works (before my change), so I've mimicked this behaviour
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.
For the second point: this one is interesting. Are you saing that if we run setter with new function it shouldn't be auto-bound?
What do you think? I feel like autobind is bound on the method not the property. Let's also take performance into consideration.
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.
I agree, we should not continue auto-binding if somebody changes method, or more interesting - plays with prototype chain of parents
But this will be breaking change and should be done as separate PR
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.
But this will be breaking change and should be done as separate PR
Since this change is quite big I'd release a major for this anyways...
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.
we should not continue auto-binding if somebody changes method, or more interesting - plays with prototype chain of parents
Will this improve the performance?
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.
In certain use-cases, most likely, but this needs to be proved
Thanks for the PR! |
Updated PR, simplified tests Removed code that auto-binds super method. Correct solution seems to be more complicated and tends to be over-engineered. Will live my thoughts on this here:
|
What about the performance? How hard is it to implement? |
This will impact performance for classes that have |
This comment has been minimized.
This comment has been minimized.
Hi @betalb , could you please add the caveats of new behaviours in README? And then we'll merge this :) |
Sorry, was caught with other things, will try to finish everything by the end of the year |
Thanks for your works, when will this be merged? |
As long as docs is added I'll merge this. |
This PR makes pretty big changes
Some of them will have performance impact, like binding every time when we call getter on
super
(see comment in code)Both: setter and getter of original descriptor may now create instance-bound property: though actual binding will happen on real get (callGetter argument of reDefineProperty)