Skip to content

Commit

Permalink
Switch finalized tracking from Set to string property.
Browse files Browse the repository at this point in the history
Also moves the finalized check earlier, which optimizes out unnecessary
super calls.
  • Loading branch information
aomarks committed Jul 15, 2019
1 parent 51ff3d0 commit e9c3be3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
27 changes: 11 additions & 16 deletions src/lib/updating-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ type UpdateState = typeof STATE_HAS_UPDATED|typeof STATE_UPDATE_REQUESTED|
typeof STATE_IS_REFLECTING_TO_ATTRIBUTE|
typeof STATE_IS_REFLECTING_TO_PROPERTY|typeof STATE_HAS_CONNECTED;

/**
* The Closure JS Compiler doesn't currently have good support for static
* properties semantics where "this" is dynamic, so we use this hack to bypass
* any rewriting by the compiler.
*/
const finalized = '__finalized__';

/**
* Base element class which manages element properties and attributes. When
* properties change, the `update` method is asynchronously called. This method
Expand All @@ -210,12 +217,6 @@ export abstract class UpdatingElement extends HTMLElement {
*/
private static _attributeToPropertyMap: AttributeMap;

/**
* 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: 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 @@ -316,15 +317,12 @@ export abstract class UpdatingElement extends HTMLElement {
* @nocollapse
*/
protected static finalize() {
if (finalized.has(this)) {
return;
}
// finalize any superclasses
const superCtor = Object.getPrototypeOf(this);
if (typeof superCtor.finalize === 'function') {
if (!superCtor.hasOwnProperty(finalized)) {
superCtor.finalize();
}
finalized.add(this);
(this as unknown as {[key: string]: unknown})[finalized] = true;
this._ensureClassProperties();
// initialize Map populated in observedAttributes
this._attributeToPropertyMap = new Map();
Expand Down Expand Up @@ -794,8 +792,5 @@ export abstract class UpdatingElement extends HTMLElement {
}
}

/**
* Marks class as having finished creating properties.
*/
const finalized = new Set<typeof UpdatingElement>();
finalized.add(UpdatingElement);
// UpdatingElement doesn't need finalization.
(UpdatingElement as unknown as {[key: string]: unknown})[finalized] = true;
11 changes: 6 additions & 5 deletions src/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ export class LitElement extends UpdatingElement {

/** @nocollapse */
protected static 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);
}
// 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 @@ -236,3 +234,6 @@ export class LitElement extends UpdatingElement {
protected render(): TemplateResult|void {
}
}

// LitElement doesn't need finalizing.
(LitElement as unknown as {[key: string]: unknown})['__finalized__'] = true;

0 comments on commit e9c3be3

Please sign in to comment.