-
Notifications
You must be signed in to change notification settings - Fork 319
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
Conversation
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.
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`.
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.
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.
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.
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`. | ||
* |
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.
Maybe add a @final
annotation. Not sure if these flow through from TypeScript to Closure via tsickle or not, but it doesn't hurt.
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.
Done.
* customize the options for a given property, override `createProperty`. | ||
* | ||
*/ | ||
protected static getPropertyOptions(name: PropertyKey) { |
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.
Can this be private? It seems to only be accessed from other private methods. If so, rename to _getPropertyOptions
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.
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.
src/lib/updating-element.ts
Outdated
* | ||
* @nocollapse | ||
*/ | ||
protected static createPropertyDescriptor(name: PropertyKey, |
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.
Should we name this get...
? On getX
vs. createX
it seems like the distinction is that createX
is side effectful and getX
isn't.
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.
Sure.
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: