Skip to content

Commit 5b21b7e

Browse files
author
Steve Orvell
authored
Merge pull request #606 from Polymer/early-request-update
Schedules `requestUpdate` in `initialize` rather than `connectedCallback`
2 parents 34c9568 + 5c393f8 commit 5b21b7e

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
* `LitElement.renderRoot` is now `public readonly` instead of `protected`.
2020

2121
### Fixed
22+
* Initial update is scheduled at construction time rather than connected time ([#594](https://github.com/Polymer/lit-element/issues/594)).
2223
* A reflecting property set immediately after a corresponding attribute
2324
now reflects properly ([#592](https://github.com/Polymer/lit-element/issues/592)).
2425
* Properties annotated with the `@query` and `@queryAll` decorators will now

src/lib/updating-element.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ export abstract class UpdatingElement extends HTMLElement {
441441
*/
442442
protected initialize() {
443443
this._saveInstanceProperties();
444+
// ensures first update will be caught by an early access of `updateComplete`
445+
this.requestUpdate();
444446
}
445447

446448
/**
@@ -484,15 +486,13 @@ export abstract class UpdatingElement extends HTMLElement {
484486

485487
connectedCallback() {
486488
this._updateState = this._updateState | STATE_HAS_CONNECTED;
487-
// Ensure connection triggers an update. Updates cannot complete before
489+
// Ensure first connection completes an update. Updates cannot complete before
488490
// connection and if one is pending connection the `_hasConnectionResolver`
489491
// will exist. If so, resolve it to complete the update, otherwise
490492
// requestUpdate.
491493
if (this._hasConnectedResolver) {
492494
this._hasConnectedResolver();
493495
this._hasConnectedResolver = undefined;
494-
} else {
495-
this.requestUpdate();
496496
}
497497
}
498498

src/test/lib/updating-element_test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,11 +2202,6 @@ suite('UpdatingElement', () => {
22022202

22032203
@property() foo = 5;
22042204

2205-
constructor() {
2206-
super();
2207-
this.requestUpdate();
2208-
}
2209-
22102205
updated(_changedProperties: Map<PropertyKey, unknown>) {
22112206
this.updatedCalledCount++;
22122207
}
@@ -2220,6 +2215,28 @@ suite('UpdatingElement', () => {
22202215
assert.equal(a.updatedCalledCount, 1);
22212216
});
22222217

2218+
test('early access of updateComplete waits until first update', async() => {
2219+
class A extends UpdatingElement {
2220+
didUpdate = false;
2221+
2222+
updated(_changedProperties: Map<PropertyKey, unknown>) {
2223+
this.didUpdate = true;
2224+
}
2225+
}
2226+
customElements.define(generateElementName(), A);
2227+
const a = new A();
2228+
let updated = false;
2229+
a.updateComplete.then(() => {
2230+
updated = true;
2231+
assert.isTrue(a.didUpdate);
2232+
});
2233+
await new Promise((r) => setTimeout(r, 20));
2234+
assert.isFalse(updated);
2235+
container.appendChild(a);
2236+
await a.updateComplete;
2237+
assert.isTrue(updated);
2238+
});
2239+
22232240
test('property reflects after setting attribute in same update cycle', async () => {
22242241
class A extends UpdatingElement {
22252242

0 commit comments

Comments
 (0)