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

How do you tell a getter to only invalidate if the _value_ changes, rather than the tracked references accessed invalidate (for any reason) #20632

Open
NullVoxPopuli opened this issue Jan 27, 2024 · 0 comments

Comments

@NullVoxPopuli
Copy link
Contributor

Something that comes close to this is @dedupeTracked from tracked-toolbox: https:/tracked-tools/tracked-toolbox/tree/master#dedupetracked

but it only works by intercepting the setting of a what-would-be-@tracked property.

The situation I have is related to the URL, but the source of the data could be anything.

get localData() {
  // currentURL will invalidate for *any* URL change, 
  // including QP-only transitions
  // but I want a way to discard the invalidations where the returned value
  // would be the same.
  return this.routerService.currentURL.split('?')[0];
}

or generically,

get localData() {
  let [dataYouCareAbout, dataYouDont] = this.upstreamDataYouDontControl;

  return dataYouCareAbout;
}

for rendering performance reasons (like, maybe something downstream, depending on localData causes other things to happen, you want those other things to execute as few times as possible -- in this case, based on value-equality rather than reference-dirtiness.

@cached is a reference-based cache, and the references do change, so it doesn't work.

What we need is something like @dedupeTracked, but for getters, and absorbing upstream changes.

@dedupeTracked  // @cached + value equality checking
get localData() {
  // ...
}

I don't think ember has a way to handle this.

Starbeam does, tho. So maybe the answer is to wait for better reactivity primitives.
https://www.starbeamjs.com/guides/advanced/equivalence.html

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

No branches or pull requests

1 participant