Skip to content

Commit ee690f6

Browse files
author
Steven Orvell
committed
Schedules requestUpdate in initialize rather than connectedCallback
Fixes #594. Ensures an early access of `updateComplete` will not complete before the element has first updated (at connection time).
1 parent fd31a97 commit ee690f6

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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: 23 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
}
@@ -2219,4 +2214,27 @@ suite('UpdatingElement', () => {
22192214
await a.updateComplete;
22202215
assert.equal(a.updatedCalledCount, 1);
22212216
});
2217+
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+
document.body.appendChild(a);
2236+
await a.updateComplete;
2237+
assert.isTrue(updated);
2238+
document.body.removeChild(a);
2239+
});
22222240
});

0 commit comments

Comments
 (0)