You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extending does some "magic" for properties which reduces control #450
Why do we have this magic of "auto" merging static get properties() while extending? Why not use a simple ...super.properties?
Doing this "magic" brings the following downsides:
When applying mixins that each have the same property it will execute requestUpdate for each definition.
Use-case would be Mixins that work independently but can also be combined.
When extending an element and using ...super.properties it will execute requestUpdate for each definition.
Use-case would be changing the property type on the extending element.
Putting a property in into static get properties does two things: (1) it makes an accessor for the property (unless noAccessor is set to true) and (2) it stores the given metadata for the property, controlling things like whether or not the property should reflect to an attribute.
The reason we merge the properties metadata from superclasses under the hood is for consistency. Since the class inherits form the superclass, it already has the accessor for a property that was in the superclass' properties. If we did not merge the metadata, the subclass would not have the property metadata and this could lead to unexpected behavior (e.g. why did this property stop reflecting but still trigger an update?).
It is true that by default a property that also exists in a superclass creates an accessor that wraps the superclass setter and this can cause requestUpdate to be called multiple times. This is done to make auto-generated accessors compose naturally with superclass accessors, whether they be user-defined or generated. However, requestUpdate is an extremely cheap call since all it does is request an update if one is not already pending. Further, this can be customized by putting noAccessor on the subclass property.
Finally, there's no reason you cannot use object spread like this to customize the metadata for a given property if desired.
So if I understand correctly, wrapping of the superclass setter should happen only when setters and getters are defined on the prototype. If you look at this example:
classLevel1extendsLitElement{staticgetproperties(){return{name: {type: String},foo: {type: String},};}}classLevel2extendsLevel1{staticgetproperties(){return{name: {type: String},};}// these setters should be wrapped, so they automatically trigger requestUpdatesetfoo(val){this.__foo=val;}getfoo(){returnthis._foo;}}
The foo setters and getters were put on the prototype by the developer, so they should be wrapped by accessors. This works correctly with this code:
I think the problem is that property accessors that were never put on the prototype (only on the constructor, like name in the above example), also get involved in the wrapping process.
This is because, in the _finalize method, this happens:
After this, the createProperty method is called. This means that, when the Level2.constructor.createProperty is called, Level1.constructor.createProperty already has been executed and created accessors on the prototype for name.
In Level2.constructor.createProperty, it will thus wrap the setters and getters created in Level1.constructor.createProperty.
This could probably be prevented by doing something like:
before looping over the parent constructor (L335). This then could be referenced in the createProperty method.
We (I'm a colleague of daKmoR :)) need this, because we are hooking into requestUpdate in order to get notified about synchronous updates (this in turn we need for synchronous observers for non render related updates).
We're actually considering removing the property accessor wrapping entirely. Instead, if user defined accessors are used, the property metadata should include noAccessor: true.
This would address the issue here; however, we're not considering this change specifically because of this issue. Upon further reflection, we think it's a bit too fancy and makes any future optimization of accessor generation much harder.
Thanks for your answer. It indeed feels good from the user perspective to be able to completely control getters and setters (and optionally call requestUpdate).
I tried the code in your pull request and this also does the job for us (our tests are passing again 👍).
Reacted by Thomas Allmer, Luiz Américo and AJ Klein
Activity
sorvell commentedon Jan 15, 2019
Putting a property in into
static get propertiesdoes two things: (1) it makes an accessor for the property (unlessnoAccessoris set totrue) and (2) it stores the given metadata for the property, controlling things like whether or not the property should reflect to an attribute.The reason we merge the
propertiesmetadata from superclasses under the hood is for consistency. Since the class inherits form the superclass, it already has the accessor for a property that was in the superclass'properties. If we did not merge the metadata, the subclass would not have the property metadata and this could lead to unexpected behavior (e.g. why did this property stop reflecting but still trigger an update?).It is true that by default a property that also exists in a superclass creates an accessor that wraps the superclass setter and this can cause
requestUpdateto be called multiple times. This is done to make auto-generated accessors compose naturally with superclass accessors, whether they be user-defined or generated. However,requestUpdateis an extremely cheap call since all it does is request an update if one is not already pending. Further, this can be customized by puttingnoAccessoron the subclass property.Finally, there's no reason you cannot use object spread like this to customize the metadata for a given property if desired.
tlouisse commentedon Jan 16, 2019
So if I understand correctly, wrapping of the superclass setter should happen only when setters and getters are defined on the prototype. If you look at this example:
The
foosetters and getters were put on the prototype by the developer, so they should be wrapped by accessors. This works correctly with this code:(https://github.com/Polymer/lit-element/blob/master/src/lib/updating-element.ts#L292)
I think the problem is that property accessors that were never put on the prototype (only on the constructor, like
namein the above example), also get involved in the wrapping process.This is because, in the
_finalizemethod, this happens:(https://github.com/Polymer/lit-element/blob/master/src/lib/updating-element.ts#L335)
After this, the
createPropertymethod is called. This means that, when theLevel2.constructor.createPropertyis called,Level1.constructor.createPropertyalready has been executed and created accessors on the prototype forname.In
Level2.constructor.createProperty, it will thus wrap the setters and getters created inLevel1.constructor.createProperty.This could probably be prevented by doing something like:
before looping over the parent constructor (L335). This then could be referenced in the
createPropertymethod.We (I'm a colleague of daKmoR :)) need this, because we are hooking into
requestUpdatein order to get notified about synchronous updates (this in turn we need for synchronous observers for non render related updates).Please see https://stackblitz.com/edit/lit-element-request-update-bug-djgbgg?file=level-2.js for an example of this (I would expect
nameto be logged exactly once when the button is pressed).tlouisse commentedon Jan 16, 2019
This would be a proposal to fix the issue: https://stackblitz.com/edit/lit-element-request-update-bug-cvhw7u?file=level-2-with-fix.js
In the console, you can see that requestUpdate is only called once when the fix is applied.
I needed to copy over a lot of code from UpdatingElement, so please search for
[ fixing part ]for the relevant code changes :)Removes wrapping of properties in favor of always respecting `noAcces…
sorvell commentedon Jan 17, 2019
We're actually considering removing the property accessor wrapping entirely. Instead, if user defined accessors are used, the property metadata should include
noAccessor: true.This would address the issue here; however, we're not considering this change specifically because of this issue. Upon further reflection, we think it's a bit too fancy and makes any future optimization of accessor generation much harder.
tlouisse commentedon Jan 17, 2019
Thanks for your answer. It indeed feels good from the user perspective to be able to completely control getters and setters (and optionally call
requestUpdate).I tried the code in your pull request and this also does the job for us (our tests are passing again 👍).