ember-polyfills / ember-cached-decorator-polyfill

Polyfill for RFC 566: @cached
MIT License
20 stars 6 forks source link

Requires `dependentKeyCompat` for consumption by computed properties #112

Open runspired opened 2 years ago

runspired commented 2 years ago

If creating a cached property that is only consumed by one computed property (e.g. nothing else would cause it to recompute), specifying that property as a dependent key requires that the property be defined with both cached and dependentKeyCompat for it to work properly, like so:

import { cached, tracked } from "@glimmer/tracking";
import { dependentKeyCompat } from "@ember/object/compat";

class Foo {
  @tracked biz;

  @cached
  @dependentKeyCompat
  get bar() { return this.biz; }
}
chriskrycho commented 2 years ago

This is expected—but likely worth documenting somewhere.

For other folks who hit this: any getter which only interacts with the auto-tracking system (which includes getters using @cached) will require @dependentKeyCompat for downstream computed properties to be updated: it's a necessary bridge between the "pull"-based system of Octane/auto-tracking, where everything between a tracked property and the reactivity layer (mostly templates) is totally ignorant of the reactivity in general, and the "push" based system (where we actively notified subscribers, who were also actively caching their dependent keys' values).