xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
555 stars 25 forks source link

References -- newValue is always undefined #153

Closed kingpalethe closed 4 years ago

kingpalethe commented 4 years ago

We've been using references with Mobx Keystone, which work well so far: https://mobx-keystone.js.org/references

Our question is with regard to the onResolvedValueChange pattern:

onResolvedValueChange?(ref: Ref<T>, newValue: T | undefined, oldValue: T | undefined) {
    // what should happen when the resolved value changes?
  },

Throughout our application, we've found that ref and oldValue always show the right data -- for example, we can do getSnapshot(oldValue) and see the previously referenced model instance.

The issue is with newValue. This seems to always return undefined, no matter what we try.

I wanted to check if anyone else had this issue....if not, I would like to set up a CodePen demonstrating it... but I can't find any templates where anyone has used Mobx Keystone in CodePen (or similar web based javascript environment) --- has anyone seem a template like that?

xaviergonz commented 4 years ago

Right now there's no official sandbox, but maybe you can piggy back for example on this one to create the reproduction case?

https://codesandbox.io/s/mobx-keystone-test-zhrue

xaviergonz commented 4 years ago

Btw, the case where oldValue !== undefined and newValue === undefined will happen if the referenced object dissapears from the tree.

The case where oldValue === undefined and newValue !== undefined is when a referenced object that wasn't in the tree appears back again.

The case where oldValue !== undefined and newValue is !== undefined (a different value than oldValue) is when an object with a given id is replaced with a different object yet with the same id (actually I'm not sure this even happens, would need to check).

The case were both are undefined should never happen.

xaviergonz commented 4 years ago

Closing due to inactivity, feel free to reopen if needed