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

Makes createProperty easier to use to customize properties #914

Merged
merged 16 commits into from
Mar 10, 2020

Conversation

sorvell
Copy link
Member

@sorvell sorvell commented Mar 5, 2020

Fixes #911. It is now easier to override property accessor creation. A new static getPropertyDescriptor(name: PropertyKey, key: string|symbol, options: PropertyDeclaraction) is added here. This method should return a property descriptor to use when defining the property. If no descriptor is returned, the property will not become an accessor.

Example:

class MyElement extends LitElement {
  static getPropertyDescriptor(
      name: PropertyKey,
      key: string|symbol,
      options: PropertyDeclaration) {

    const defaultDescriptor = super.getPropertyDescriptor(name, key, options); 
    const setter = defaultDescriptor.set;
    return Object.assign(descriptor, {
      set(this: MyElement, value: unknown) {
        setter.call(this, value);
        console.log('Property', name, 'was set to', value);
     }
  }
  //...
}

Steven Orvell added 5 commits February 28, 2020 17:21
Fixes #905. A couple changes to make customizing property accessors easier.

* Adds static methods`setOptionsForProperty(name, options)`, `getOptionsForProperty` and renames `_requestUpdate` to `requestUpdateInternal` and makes it protected.

With these changes, users can override `createProperty` and (1) pass in and later retrieve custom property otions, (2) customize how accessors are defined and call `requestUpdateInternal` in the setter to make the property reactive.
This function receives the property declaration, key of the private storage for the property, and default property descriptor. It should return a PropertyDescriptor. Users can customer this descriptor to control how property accessors behave.
src/lib/updating-element.ts Outdated Show resolved Hide resolved
src/lib/updating-element.ts Show resolved Hide resolved
src/lib/updating-element.ts Outdated Show resolved Hide resolved
src/lib/updating-element.ts Outdated Show resolved Hide resolved
Steven Orvell added 4 commits March 9, 2020 10:35
This method is called internallly by `createProperty` to get a descriptor to install on the property. Users can implement it to customize what property accessors do.
Users can implement setters with `requestUpdate`.
src/lib/updating-element.ts Outdated Show resolved Hide resolved
src/lib/updating-element.ts Outdated Show resolved Hide resolved
src/test/lib/updating-element_test.ts Show resolved Hide resolved
src/lib/updating-element.ts Outdated Show resolved Hide resolved
Copy link
Member

@kevinpschaaf kevinpschaaf left a comment

Choose a reason for hiding this comment

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

LGTM

Ping @justinfagnani to weigh in on getPropertyOptions vs. getDeclarationForProperty naming before it's too late... "Declaration" is a sort of obtuse name for the options, and we call the argument options throughout the code, but we've already grandfathered "Declaration" in the type. I like options better, but I can see a consistency argument either way.

Copy link
Contributor

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

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

Looks good as-is, but I had a couple of small naming questions.

*
* Note, this method should be considered "final" and not overridden. To
* customize the options for a given property, override `createProperty`.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a @final annotation. Not sure if these flow through from TypeScript to Closure via tsickle or not, but it doesn't hurt.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

* customize the options for a given property, override `createProperty`.
*
*/
protected static getPropertyOptions(name: PropertyKey) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be private? It seems to only be accessed from other private methods. If so, rename to _getPropertyOptions

Copy link
Member Author

Choose a reason for hiding this comment

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

It's protected so options can be used in the element lifecycle. For example, in updated you might want to act on a given changed property if it has a specific option.

*
* @nocollapse
*/
protected static createPropertyDescriptor(name: PropertyKey,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we name this get...? On getX vs. createX it seems like the distinction is that createX is side effectful and getX isn't.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

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.

Make LitElement's property accessors creation and metadata easily customizable
4 participants