mobx-keystone icon indicating copy to clipboard operation
mobx-keystone copied to clipboard

How to destroy a reference?

Open gbcreation opened this issue 2 years ago • 4 comments

It seems that just creating a new reference to a tree node is enough for getRefsResolvingTo() to return a Set with one Ref pointing to this tree node. My question is how can we destroy this ref?

In the Todo List example from the documentation about References, the reference to the selected Todo is somehow destroyed by setting selectedRef to undefined. But how to do that if the created Ref is not attached to a prop<Ref<Todo>> of an other model? It is possible to destroy directly the Ref itself?

gbcreation avatar Feb 28 '22 12:02 gbcreation

you mean a root ref or a custom one? if custom, how does it work?

xaviergonz avatar Mar 06 '22 01:03 xaviergonz

It is a custom ref that uses a context in resolve() to resolve the reference to the target model. This latter is in model store (a simple list of models).

gbcreation avatar Mar 07 '22 08:03 gbcreation

In theory, as soon as the custom reference resolve function stops returning a resolved model (returns undefined) it should automatically be removed from the getRefsResolvingTo list.

Maybe you could add a blacklist in the context, make the custom ref return undefined when it is blacklisted and push something to that context blacklist when you want a ref to get "invalidated"?

xaviergonz avatar Mar 07 '22 19:03 xaviergonz

Thank you for the explanations and the suggestion.

as soon as the custom reference resolve function stops returning a resolved model (returns undefined) it should automatically be removed from the getRefsResolvingTo list.

This is the important point to know.

gbcreation avatar Mar 07 '22 20:03 gbcreation