Skip to content

Commit

Permalink
Fix compatibility with JSC static property optimizations.
Browse files Browse the repository at this point in the history
Fixes #732
  • Loading branch information
aomarks committed Jul 15, 2019
1 parent 46c8f40 commit 0aa7b73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Unreleased section, uncommenting the header as necessary.
-->

<!-- ## Unreleased -->
## Unreleased
<!-- ### Added -->
<!-- ### Changed -->
<!-- ### Removed -->
<!-- ### Fixed -->
### Fixed
* Fixed compatibility with Closure JS Compiler optimizations relating to static properties ([#732](https://github.com/Polymer/lit-element/issues/732)).


## [2.2.0] - 2019-06-11
Expand Down
21 changes: 15 additions & 6 deletions src/lib/updating-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* http://polymer.github.io/PATENTS.txt
*/

// So that custom elements work after compilation to es5.
import 'goog:webcomponentsjs.custom_elements.auto_es5_shim';

/**
* When using Closure Compiler, JSCompiler_renameProperty(property, object) is
* replaced at compile time by the munged name for object[property]. We cannot
Expand Down Expand Up @@ -211,10 +214,11 @@ export abstract class UpdatingElement extends HTMLElement {
private static _attributeToPropertyMap: AttributeMap;

/**
* Marks class as having finished creating properties.
* This property was previously used to record the classes that have already
* been finalized. This property is no longer used, but is still declared here
* for backwards compatability in TypeScript typings.
*/
protected static finalized = true;

protected static finalized : boolean|undefined;
/**
* Memoized list of all class properties, including any superclass properties.
* Created lazily on user subclasses when finalizing the class.
Expand Down Expand Up @@ -315,16 +319,15 @@ export abstract class UpdatingElement extends HTMLElement {
* @nocollapse
*/
protected static finalize() {
if (this.hasOwnProperty(JSCompiler_renameProperty('finalized', this)) &&
this.finalized) {
if (finalized.has(this)) {
return;
}
// finalize any superclasses
const superCtor = Object.getPrototypeOf(this);
if (typeof superCtor.finalize === 'function') {
superCtor.finalize();
}
this.finalized = true;
finalized.add(this);
this._ensureClassProperties();
// initialize Map populated in observedAttributes
this._attributeToPropertyMap = new Map();
Expand Down Expand Up @@ -793,3 +796,9 @@ export abstract class UpdatingElement extends HTMLElement {
protected firstUpdated(_changedProperties: PropertyValues) {
}
}

/**
* Marks class as having finished creating properties.
*/
const finalized = new Set<typeof UpdatingElement>();
finalized.add(UpdatingElement);
14 changes: 6 additions & 8 deletions src/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ const flattenStyles = (styles: CSSResultArray): CSSResult[] =>
styles.flat ? styles.flat(Infinity) : arrayFlat(styles);

export class LitElement extends UpdatingElement {
/**
* Ensure this class is marked as `finalized` as an optimization ensuring
* it will not needlessly try to `finalize`.
*/
protected static finalized = true;

/**
* Render method used to render the lit-html TemplateResult to the element's
* DOM.
Expand All @@ -85,7 +79,11 @@ export class LitElement extends UpdatingElement {

/** @nocollapse */
protected static finalize() {
super.finalize();
if (this !== LitElement) {
// The Closure JS Compiler does not always preserve the correct "this"
// when calling static super methods, so explicitly bind here.
super.finalize.apply(this);
}
// Prepare styling that is stamped at first render time. Styling
// is built from user provided `styles` or is inherited from the superclass.
this._styles =
Expand Down Expand Up @@ -180,7 +178,7 @@ export class LitElement extends UpdatingElement {
// (3) shadowRoot.adoptedStyleSheets polyfilled: append styles after
// rendering
if (window.ShadyCSS !== undefined && !window.ShadyCSS.nativeShadow) {
window.ShadyCSS.ScopingShim.prepareAdoptedCssText(
window.ShadyCSS.ScopingShim!.prepareAdoptedCssText(
styles.map((s) => s.cssText), this.localName);
} else if (supportsAdoptingStyleSheets) {
(this.renderRoot as ShadowRoot).adoptedStyleSheets =
Expand Down

0 comments on commit 0aa7b73

Please sign in to comment.