-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Prevent cloning objects into previousData that are not plain objects #2939
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
Conversation
3724c4f to
ef75dc2
Compare
fb6c826 to
2bd9451
Compare
|
Can you paste pseudocode that describes " |
src/core/component.js
Outdated
| data = typeof schema.default === 'object' ? utils.clone(schema.default) : schema.default; | ||
| // Clone default value if plain object so components don't share the same object | ||
| // that might be modified by the user. | ||
| data = typeof schema.default === 'object' && schema.default.constructor === Object ? utils.clone(schema.default) : schema.default; |
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.
Do we need both typeof and .constructor checks?
| */ | ||
| function cloneData (data) { | ||
| var clone = {}; | ||
| var parsedProperty; |
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.
declare vars up top.
i wonder if we should start to use for (key in obj) for every loop over an object where we know it's a plain JS object. e.g., change some of the code in the buildData to also use for...in
|
An example is: |
2bd9451 to
7212794
Compare
|
@fernandojsg is this true that this blocks aframevr/a-saturday-night#118 (comment) ? |
|
@cvan yes, it was described on my PR aframevr/a-saturday-night#117 (I closed it because @caseyyee and I were doing practically the same) |
|
Speaking towards the technicals, I start to often use strings rather than |
default values that are not plain objects into data.
7212794 to
a6fa87c
Compare
|
I addressed the review comments and fixed the test. Ok to merge? |
| data = typeof previousData === 'object' ? utils.clone(previousData) : {}; | ||
| // Clone previous data to prevent sharing references with attrValue that might be | ||
| // modified by the user. | ||
| data = typeof previousData === 'object' ? cloneData(previousData) : {}; |
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 believe you can use utils.extend({}, previousData). The problem with utils.clone is it uses JSON to do it. Getting rid of the clone function you made.
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.
utils.extend copies references. I still want to clone the plain object properties
|
r+wc |
a-saturday-nightusesselectorproperties that change at run time. This triggeredConverted circular structure to JSONerrors when trying to clone into previousData A-Frame entity elements that hold references to other A-Frame entities via components .We should only be cloning plain objects.