Tree semantics in detail
MST trees have very specific semantics. These semantics purposefully constrain what you can do with MST. The reward for that is all kinds of generic features out of the box like snapshots, replayability, etc... If these constraints don't suit your app, you are probably better off using plain mobx
with your own model classes, which is perfectly fine as well.
- Each object in a MST tree is considered a node. Each primitive (and frozen) value is considered a leaf.
- MST has only three types of nodes: model, array, and map.
- Every node tree in a MST tree is a tree in itself. Any operation that can be invoked on the complete tree can also be applied to a sub tree.
- A node can only exist exactly once in a tree. This ensures it has a unique, identifiable position.
- It is, however, possible to refer to another object in the same tree by using references.
- There is no limit to the amount of MST trees that live in an application. However, each node can only live in exactly one tree.
- All leaves in the tree must be serializable; it is not possible to store, for example, functions in a MST.
- The only free-from type in MST is frozen, with the requirement that frozen values are immutable and serializable so that the MST semantics can still be upheld.
- At any point in the tree, it is possible to assign a snapshot to the tree instead of a concrete instance of the expected type. In that case, an instance of the correct type, based on the snapshot, will be automatically created for you.
- Nodes in the MST tree will be reconciled (the exact same instance will be reused) when updating the tree by any means, based on their identifier property. If there is no identifier property, instances won't be reconciled.
- If a node in the tree is replaced by another node, the original node will die and become unusable. This makes sure you are not accidentally holding on to stale objects anywhere in your application.
- If you want to create a new node based on an existing node in a tree, you can either
detach
that node, orclone
it.