Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure firstUpdated is always called when the element first updated #173

Merged
merged 2 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/updating-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,17 @@ export abstract class UpdatingElement extends HTMLElement {
const needsFirstUpdate = !(this._updateState & STATE_HAS_UPDATED);
this._markUpdated();
if (needsFirstUpdate) {
this._updateState = this._updateState | STATE_HAS_UPDATED;
this.firstUpdated(changedProperties);
}
this.updated(changedProperties);
} else {
this._markUpdated();
}
}

private _markUpdated() {
this._changedProperties = new Map();
this._updateState = this._updateState & ~STATE_UPDATE_REQUESTED | STATE_HAS_UPDATED;
this._updateState = this._updateState & ~STATE_UPDATE_REQUESTED;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/test/lit-element_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,56 @@ suite('LitElement', () => {
assert.equal(el.wasFirstUpdated, 1);
});

test(
'`firstUpdated` called when element first updates even if first `shouldUpdate` returned false', async () => {
class E extends LitElement {

@property()
foo = 1;

triedToUpdatedCount = 0;
wasUpdatedCount = 0;
wasFirstUpdated = 0;
changedProperties: PropertyValues|undefined;

shouldUpdate() {
this.triedToUpdatedCount++;
return this.triedToUpdatedCount > 1;
}

update(changedProperties: PropertyValues) {
this.wasUpdatedCount++;
super.update(changedProperties);
}

render() { return html ``; }

firstUpdated(changedProperties: PropertyValues) {
this.changedProperties = changedProperties;
this.wasFirstUpdated++;
}
}

customElements.define(generateElementName(), E);
const el = new E();
container.appendChild(el);
await el.updateComplete;
const testMap = new Map();
testMap.set('foo', undefined);
assert.equal(el.triedToUpdatedCount, 1);
assert.equal(el.wasUpdatedCount, 0);
assert.equal(el.wasFirstUpdated, 0);
await el.requestUpdate();
assert.deepEqual(el.changedProperties, testMap);
assert.equal(el.triedToUpdatedCount, 2);
assert.equal(el.wasUpdatedCount, 1);
assert.equal(el.wasFirstUpdated, 1);
await el.requestUpdate();
assert.equal(el.triedToUpdatedCount, 3);
assert.equal(el.wasUpdatedCount, 2);
assert.equal(el.wasFirstUpdated, 1);
});

test(
'render lifecycle order', async () => {
class E extends LitElement {
Expand Down