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

Conversation

LostInBrittany
Copy link
Contributor

Reference Issue

Fix #172

As described in #172, the firstUpdated lifecycle method is not always called.

The problem was in the _validate() method of lib/update-element.ts:

  private _validate() {
    // Mixin instance properties once, if they exist.
    if (this._instanceProperties) {
      this._applyInstanceProperties();
    }
    if (this.shouldUpdate(this._changedProperties)) {
      const changedProperties = this._changedProperties;
      this.update(changedProperties);
      const needsFirstUpdate = !(this._updateState & STATE_HAS_UPDATED);
      this._markUpdated();
      if (needsFirstUpdate) {
        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;
  }

If shouldUpdated return false, the element is still marked as updated in _markUpdated, but the element isn't updated.
So if the first time the element tries to update shouldUpdated return false, the element is marked as updated. Then when the element is really going to be updated, the check const needsFirstUpdate = !(this._updateState & STATE_HAS_UPDATED); is false, and firstUpdated isn't called.

I have added a new test case to test this problem, and the version 0.6.0-dev.6 doesn't pass it.
Then I have modified lib/update-element.ts with a new _markNotUpdated() method that is called when shouldUpdate returns false, and that doesn't mark the element as updated.

With that fix firstUpdated is always called the first time and all the other tests are still valid.

Copy link
Contributor

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome patch!

LGTM. cc @sorvell

@LostInBrittany
Copy link
Contributor Author

Thanks a lot @justinfagnani 😊

@abdonrd
Copy link
Contributor

abdonrd commented Sep 11, 2018

Copy link
Member

@sorvell sorvell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Good fix, just a small suggestion.

src/lib/updating-element.ts Outdated Show resolved Hide resolved
@LostInBrittany
Copy link
Contributor Author

I am not sure if I have understood, something like ce2e44a?

@sorvell sorvell added this to the 0.6.0 milestone Sep 11, 2018
Copy link
Member

@sorvell sorvell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sorvell sorvell merged commit 5ffb22a into lit:master Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[0.6.0-dev.6] firstUpdated never called if first shouldUpdate returned false
4 participants