Skip to content

Commit

Permalink
fix(query-template-by-id.ts): add cache param, remove unnecessary req…
Browse files Browse the repository at this point in the history
…uestUpdate
  • Loading branch information
waldronmatt committed Oct 23, 2024
1 parent 43ffb10 commit 27efc3b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/lit-override/src/decorators/query-template-by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ExtendedElement extends ReactiveElement {
}

export interface QueryTemplateByIdParams {
cache?: boolean;
fallback?: boolean;
}

Expand All @@ -21,9 +22,10 @@ export interface QueryTemplateByIdParams {
* Gets a template element by id that is provided to the `templateId` property.
* Will cache the template element on successful query.
*
* @param cache caches the template element between component renders when enabled. Defaults to `true`.
* @param fallback gets a template element if an id is not provided (not cached). Defaults to `false`.
*/
export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams = {}) => {
export const queryTemplateById = ({ cache = true, fallback = false }: QueryTemplateByIdParams = {}) => {
return <T extends ReactiveElement>(proto: T, propName: string) => {
// this is a 'wrapper' around a custom property accessor: https://lit.dev/docs/components/properties/#accessors
const internalKey = Symbol(`_${String(propName)}`);
Expand All @@ -38,7 +40,7 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams
this._templateCache = {};
}

if (id && this._templateCache[id]) {
if (id && this._templateCache[id] && cache) {
return this._templateCache[id];
}

Expand All @@ -49,7 +51,9 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams
return null;
}

this._templateCache[id] = templateElement;
if (cache) {
this._templateCache[id] = templateElement;
}
return templateElement;
}

Expand All @@ -59,11 +63,9 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams
const oldValue = this[internalKey];
this[internalKey] = value;

if (this._templateCache && oldValue) {
if (this._templateCache && oldValue && cache) {
delete this._templateCache[oldValue];
}

this.requestUpdate('templateId', oldValue);
},
enumerable: true,
configurable: true,
Expand Down

0 comments on commit 27efc3b

Please sign in to comment.