ember-deep-tracked icon indicating copy to clipboard operation
ember-deep-tracked copied to clipboard

Does it support deduped tracking?

Open simonihmig opened this issue 4 years ago • 1 comments

Hey, this looks pretty close to what I need, but I am not sure how it would deal with an immutable object, that is replaced for every change.

To give you an example: a (non-Ember) library is returning an immutable object, that drives the UI state. On every change of the application state, that library is asked to return a new object, which is then assigned to a tracked property, to update the UI. That even works without any "deep" tracking, just plain @tracked, as the whole object is replaced. But this causes all parts of the UI to rerender, even those where the actual value didn't change.

Some pseudo code to illustrate the example:

class GlobalStateService extends Service {
  @tracked globalState = { foo: 0, bar: 0};

  @action
  updateFoo() {
    // instead of mutating the foo property, we simulate the immutable behavior by assigned a new POJO
    this.globalState = { ...this.globalState, foo: this.globalState.foo + 1 };
  }
}

When I consume both globalState.foo and globalState.bar in a template, after calling updateFoo() both would re-render, as the whole globalState has been updated. But what I would like to see is that only the parts depending on globalState.foo re-render, but those depending on globalState.bar don't, as the actual value didn't change.

That's kinda the behavior of @dedupeTracked, but with deep tracking.

I assume that's not what ember-deep-tracked currently does, right? Do you think it would be feasible to support that?

simonihmig avatar Dec 09 '21 09:12 simonihmig

Perhaps! Idk if it should be default or a new decorator. I imagine we'd want to pair with immer or some other immutability-optimizing library?

NullVoxPopuli avatar Dec 09 '21 11:12 NullVoxPopuli