Skip to content
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

Support ES6 getters and setters for data binding #4815

Closed

Conversation

ChadKillingsworth
Copy link
Contributor

Fixes #4595

Adds support for ES6 getters and setters. Getters are used as-is. Setters are wrapped to enable change notification. Getters without a corresponding setter are treated as a read-only property.

…dden by PropertyEffects to be synchronous, we do not need to be concerned about a getter value changing between the time when a notification is triggered and processed.
Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talked about this at the summit, I like this change a lot! 2 small comments, other than that LGTM


// For any predefined getters without setters, add a read only effect
// so that change notifications don't attempt to update the data cache
if (this.ACCESSOR_TYPE && this.__dataHasAccessor[prop] === this.ACCESSOR_TYPE.PRE_DEFINED) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this code here instead of in _createPropertyAccessor, any particular reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was since this was adding a property effect - it shouldn't be in property accessors. I was trying to consider the cases where PropertyAccessors was mixed in without mixing in PropertyEffects.

let originalProp2TwoWay = el.$.prop2TwoWay.value;
assert.equal(originalProp2TwoWay.indexOf('prop2'), 0);

el.prop1_ = 'prop1changed';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to see a test that sets both prop1 and prop2 and asserts that el.$.addProps is set correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and initially I missed the fact that this is prop1_. Maybe rename this variable to make it clearer that this is the internal property cache, to make it more distinguishable. Something like _internal_prop1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prop2 has a getter but no setter. It's read only. Do you want me to add a 3rd property to test that?

I initially had that but removed it. Since PropertyEffects overrides PropertyAccessor to make _propertiesChanged synchronous, I removed the 3rd property.

…ery clear that we are changing the internal version and bypassing automatic change notification.
@arthurevans
Copy link

Not sure I understand how bindings & observers work for getters without setters. Are these only updated once, when the element is initialized?

@ChadKillingsworth
Copy link
Contributor Author

@arthurevans Getters without setters would be only updated once - unless you manually called notifyPath(). That would trigger an update.

Also I have a theory that using linkPaths should be able to trigger an update on a getter from another property. I haven't tested that yet though.

@sorvell
Copy link
Contributor

sorvell commented Sep 14, 2017

See comments posted on the related issue for some general points.

I think it would probably make more sense if the existing setter became a property effect. This way it would run, for example, when setProperties and _setPendingProperty was called.

I also think we'd want to make sure that any relevant code that de-references from this.__data in property-effects was changed to use the getter.

Finally, there is the question of how this interplays with the rest of the system, specifically _propertiesChanged and properties bound to user defined getters. I think we'd probably need to work this out before accepting this PR.

@ChadKillingsworth
Copy link
Contributor Author

@sorvell I'll try to spend some on this next week. I'd really like to figure out some solution here. This very much feels like a #UseThePlatform thing.

In addition, the sheer amount of property reflection needed with ClosureCompiler is extremely tedious - anything to alleviate that would make a big difference on internal Google projects.

@TimvdLippe
Copy link
Contributor

I recall an earlier discussion we had about this PR. We are disinclined to add support for this in the data binding system, as making the data sync properly is computationally intensive. Instead, with lit-element this should work out of the box where you have more control on when to invoke the getter/setter. Therefore I am closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants