-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[BUGFIX beta] Allow accessors in mixins #17710
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/@ember/-internals/metal/tests/mixin/accessor_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Mixin } from '../..'; | ||
import { moduleFor, AbstractTestCase } from 'internal-test-helpers'; | ||
|
||
moduleFor( | ||
'Mixin Accessors', | ||
class extends AbstractTestCase { | ||
['@test works with getters'](assert) { | ||
let count = 0; | ||
|
||
let MixinA = Mixin.create({ | ||
get prop() { | ||
return count++; | ||
}, | ||
}); | ||
|
||
let obj = {}; | ||
MixinA.apply(obj); | ||
|
||
assert.equal(obj.prop, 0, 'getter defined correctly'); | ||
assert.equal(obj.prop, 1, 'getter defined correctly'); | ||
} | ||
|
||
['@test works with setters'](assert) { | ||
let MixinA = Mixin.create({ | ||
set prop(value) { | ||
this._prop = value + 1; | ||
}, | ||
}); | ||
|
||
let obj = {}; | ||
MixinA.apply(obj); | ||
|
||
obj.prop = 0; | ||
|
||
assert.equal(obj._prop, 1, 'setter defined correctly'); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/@ember/-internals/utils/lib/get-own-property-descriptors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
let getOwnPropertyDescriptors: (obj: { [x: string]: any }) => { [x: string]: PropertyDescriptor }; | ||
|
||
if (Object.getOwnPropertyDescriptors !== undefined) { | ||
getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors; | ||
} else { | ||
getOwnPropertyDescriptors = function(obj: object) { | ||
let descriptors = {}; | ||
|
||
Object.keys(obj).forEach(key => { | ||
descriptors[key] = Object.getOwnPropertyDescriptor(obj, key); | ||
}); | ||
|
||
return descriptors; | ||
}; | ||
} | ||
|
||
export default getOwnPropertyDescriptors; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
.some
isAccessor
twiceSuggest:
What do you think?
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.
The reason why I run over the object twice is it's also part of the cloning process. We can't do
extracted = assign({}, properties)
because that'll actually trigger any native getters that exist there, we have to skip over native getters. This means we need to iterate the full list of properties.However, we don't want to start cloning eagerly, and create a new object, unless one of the values in the object is an accessor. So we need to check to see if anything is an accessor first, then we need to loop over all the keys again to assign them conditionally, based on whether or not one is an accessor.
We could alternatively mutate the
properties
object in place' but in order to do that we would need to useObject.defineProperty
since we're talking about overriding native getters/setters:I figured the double whammy of using
Object.defineProperty
(which I understand to be slow-ish) and mutating the object in place didn't make sense, but happy to change it if that makes more senseThere 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.
Gotcha, yes I agree. Also, I was surprised that IE9+ has
Array.prototype.some
.