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

Add @internalProperty decorator, for private or protected fields. #881

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.0",
"tachometer": "^0.4.16",
"tslint": "^5.12.0",
"tslint": "^5.20.1",
"typedoc": "^0.14.2",
"typescript": "^3.4.1",
"uglify-es": "^3.3.9",
Expand Down
24 changes: 24 additions & 0 deletions src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const legacyProperty =
* corresponding attribute value. A `PropertyDeclaration` may optionally be
* supplied to configure property features.
*
* This decorator should only be used for public fields. Private or protected
* fields should use the internalProperty decorator.
*
* @example
*
* class MyElement {
Expand All @@ -162,6 +165,27 @@ export function property(options?: PropertyDeclaration) {
standardProperty(options!, protoOrDescriptor as ClassElement);
}

export interface InternalPropertyDeclaration<Type = unknown> {
/**
* A function that indicates if a property should be considered changed when
* it is set. The function should take the `newValue` and `oldValue` and
* return `true` if an update should be requested.
*/
hasChanged?(value: Type, oldValue: Type): boolean;
}

/**
* Declares a private or protected property that still triggers updates to the
* element when it changes.
*
* Properties declared this way must not be used from HTML or HTML templating
* systems, they're solely for properties internal to the element. These
* properties may be renamed by optimization tools like closure compiler.
*/
export function internalProperty(options?: InternalPropertyDeclaration) {
return property({attribute: false, hasChanged: options?.hasChanged});
}

/**
* A property decorator that converts a class property into a getter that
* executes a querySelector on the element's renderRoot.
Expand Down