emberjs / emberjs/ember.js

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

Open
#20,632 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
22.6k
Forks
4.2k
Avg merge
3d 12h
Merged PRs (30d)
15

Description

Something that comes close to this is `@dedupeTracked` from `tracked-toolbox`: https://github.com/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.
```js
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,
```js
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.
```js
@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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.