From 27efc3b78ab7b4e7400d6e42c7025776aff1993e Mon Sep 17 00:00:00 2001 From: waldronmatt Date: Tue, 22 Oct 2024 23:36:54 -0400 Subject: [PATCH] fix(query-template-by-id.ts): add cache param, remove unnecessary requestUpdate --- .../src/decorators/query-template-by-id.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/lit-override/src/decorators/query-template-by-id.ts b/packages/lit-override/src/decorators/query-template-by-id.ts index ea955e7e..6648966c 100644 --- a/packages/lit-override/src/decorators/query-template-by-id.ts +++ b/packages/lit-override/src/decorators/query-template-by-id.ts @@ -12,6 +12,7 @@ export interface ExtendedElement extends ReactiveElement { } export interface QueryTemplateByIdParams { + cache?: boolean; fallback?: boolean; } @@ -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 (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)}`); @@ -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]; } @@ -49,7 +51,9 @@ export const queryTemplateById = ({ fallback = false }: QueryTemplateByIdParams return null; } - this._templateCache[id] = templateElement; + if (cache) { + this._templateCache[id] = templateElement; + } return templateElement; } @@ -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,