Skip to content

Commit 6451733

Browse files
author
Steven Orvell
committed
Address review feedback.
1 parent 9120ac3 commit 6451733

File tree

3 files changed

+12
-40
lines changed

3 files changed

+12
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
* The value returned by `render` is always rendered, even if it isn't a `TemplateResult`. ([#712](https://github.com/Polymer/lit-element/issues/712)
2323

2424
### Added
25+
* Added `@asyncQuery(selector)` decorator which returns a Promise resolves to the result of querying for the given selector after the element's `updateComplete` Promise resolves ([#903](https://github.com/Polymer/lit-element/issues/903)).
2526
* Added `enableUpdating()` to `UpdatingElement` to enable customizing when updating is enabled [#860](https://github.com/Polymer/lit-element/pull/860).
2627
* Added `queryAssignedNodes(slotName, flatten)` to enable querying assignedNodes for a given slot [#860](https://github.com/Polymer/lit-element/pull/860).
2728
* Added `getStyles()` to `LitElement` to allow hooks into style gathering for component sets [#866](https://github.com/Polymer/lit-element/pull/866).

src/lib/decorators.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ export function internalProperty(options?: InternalPropertyDeclaration) {
207207
* `;
208208
* }
209209
* }
210+
*
211+
* // external usage
212+
* async doSomethingWithFirst() {
213+
* (await aMyElement.first).doSomething();
214+
* }
210215
*/
211216
export function query(selector: string) {
212217
return (protoOrDescriptor: Object|ClassElement,
@@ -229,8 +234,9 @@ export function query(selector: string) {
229234
* A property decorator that converts a class property into a getter that
230235
* returns a promise that resolves to the result of a querySelector on the
231236
* element's renderRoot done after the element's `updateComplete` promise
232-
* resolves and after the queried element's `updateComplete` promise resolves
233-
* (if it is a LitElement).
237+
* resolves. When queried property may change with element state, this decorator
238+
* can be used instead of requiring users to await the `updateComplete` before
239+
* accessing the property.
234240
*
235241
* @param selector A DOMString containing one or more selectors to match.
236242
*
@@ -257,12 +263,7 @@ export function asyncQuery(selector: string) {
257263
const descriptor = {
258264
async get(this: LitElement) {
259265
await this.updateComplete;
260-
const el = this.renderRoot.querySelector(selector);
261-
// if this is a LitElement, then await updateComplete
262-
if (el) {
263-
await (el as LitElement).updateComplete;
264-
}
265-
return el;
266+
return this.renderRoot.querySelector(selector);
266267
},
267268
enumerable: true,
268269
configurable: true,

src/test/lib/decorators_test.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -461,27 +461,11 @@ suite('decorators', () => {
461461
});
462462

463463
suite('@asyncQuery', () => {
464-
@customElement('dep-el' as keyof HTMLElementTagNameMap)
465-
class D extends LitElement {
466-
467-
wasUpdated = false;
468-
469-
render() {
470-
return html`dep`;
471-
}
472-
473-
firstUpdated() {
474-
this.wasUpdated = true;
475-
}
476-
}
477-
478464

479465
@customElement(generateElementName() as keyof HTMLElementTagNameMap)
480466
class C extends LitElement {
481467
@asyncQuery('#blah') blah!: Promise<HTMLDivElement>;
482468

483-
@asyncQuery('dep-el') dep!: Promise<D>;
484-
485469
@asyncQuery('span') nope!: Promise<HTMLSpanElement|null>;
486470

487471
@property()
@@ -491,8 +475,8 @@ suite('decorators', () => {
491475
return html`
492476
<div>Not this one</div>
493477
${this.foo ?
494-
html`<div id="blah" foo>This one</div><dep-el foo></dep-el>` :
495-
html`<div id="blah">This one</div><dep-el></dep-el>` }
478+
html`<div id="blah" foo>This one</div>` :
479+
html`<div id="blah">This one</div>` }
496480
`;
497481
}
498482
}
@@ -509,20 +493,6 @@ suite('decorators', () => {
509493
assert.isTrue(div.hasAttribute('foo'));
510494
});
511495

512-
test('returns an element when it exists after update that is itself updated', async () => {
513-
const c = new C();
514-
container.appendChild(c);
515-
let dep = await c.dep;
516-
assert.instanceOf(dep, D);
517-
assert.isFalse(dep.hasAttribute('foo'));
518-
assert.isTrue(dep.wasUpdated);
519-
c.foo = true;
520-
dep = await c.dep;
521-
assert.instanceOf(dep, D);
522-
assert.isTrue(dep.hasAttribute('foo'));
523-
assert.isTrue(dep.wasUpdated);
524-
});
525-
526496
test('returns null when no match', async () => {
527497
const c = new C();
528498
container.appendChild(c);

0 commit comments

Comments
 (0)