Skip to content

Commit d97341d

Browse files
authored
Fix cached query decorator (#1142) (#1143)
1 parent 0558234 commit d97341d

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/lib/decorators.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ export function query(selector: string, cache?: boolean) {
227227
configurable: true,
228228
};
229229
if (cache) {
230-
const key = typeof name === 'symbol' ? Symbol() : `__${name}`;
230+
const prop =
231+
name !== undefined ? name : (protoOrDescriptor as ClassElement).key;
232+
const key = typeof prop === 'symbol' ? Symbol() : `__${prop}`;
231233
descriptor.get = function(this: LitElement) {
232234
if ((this as unknown as
233235
{[key: string]: Element | null})[key as string] === undefined) {

src/test/lib/decorators_test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ suite('decorators', () => {
328328
class C extends LitElement {
329329
@query('#blah') div?: HTMLDivElement;
330330

331+
@query('#blah', true) divCached?: HTMLDivElement;
332+
331333
@query('span', true) span?: HTMLSpanElement;
332334

333335
@property() condition = false;
@@ -362,6 +364,8 @@ suite('decorators', () => {
362364
c.condition = true;
363365
container.appendChild(c);
364366
await c.updateComplete;
367+
// trigger caching, so we can verify that multiple elements can be cached
368+
c.divCached;
365369
assert.equal(c.span, c.renderRoot.querySelector('span'));
366370
assert.instanceOf(c.span, HTMLSpanElement);
367371
c.condition = false;

0 commit comments

Comments
 (0)