NullVoxPopuli / ember-resources

An implementation of Resources. Supports ember 3.28+
https://github.com/NullVoxPopuli/ember-resources/blob/main/docs/docs/README.md
MIT License
91 stars 37 forks source link

Fix issue with keepLatest and empty values #1051

Closed wagenet closed 9 months ago

wagenet commented 10 months ago

tl;dr:


The way to provide a default value when the value of keepLatest is false is to use another getter.

This PR adds a couple tests clarifying that behavior:

class Demo {
  @use latest = keepLatest({
    value: () => this.data.value?.meta,
    when: () => this.data.isLoading,    
  })

  get theData() {
    return this.latest ?? 'default value';
  }
}

It's important to not do

   value: () => this.data.value?.meta ?? {},

because that means falsey responses (on data), will resolve to {}, and cause content flashes.

So to keep the latest value, you want to let the data response be what it actually is.

In traditional logic, this could maybe be expressed this way:

value ?? default ?? previous

when default is always truthy vs

(value ?? previous) ?? default
stackblitz[bot] commented 10 months ago

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

changeset-bot[bot] commented 10 months ago

⚠️ No Changeset found

Latest commit: 7970e8935265c375209f3b988299a18a702ef1d3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR