@colyseus/schema 3.0.23 → 3.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4732,13 +4732,25 @@ class StateView {
4732
4732
  }
4733
4733
  // TODO: allow to set multiple tags at once
4734
4734
  add(obj, tag = DEFAULT_VIEW_TAG, checkIncludeParent = true) {
4735
- if (!obj?.[$changes]) {
4735
+ const changeTree = obj?.[$changes];
4736
+ if (!changeTree) {
4736
4737
  console.warn("StateView#add(), invalid object:", obj);
4737
4738
  return this;
4738
4739
  }
4740
+ else if (!changeTree.parent &&
4741
+ changeTree.refId !== 0 // allow root object
4742
+ ) {
4743
+ /**
4744
+ * TODO: can we avoid this?
4745
+ *
4746
+ * When the "parent" structure has the @view() tag, it is currently
4747
+ * not possible to identify it has to be added to the view as well
4748
+ * (this.addParentOf() is not called).
4749
+ */
4750
+ throw new Error(`Cannot add a detached instance to the StateView. Make sure to assign the "${changeTree.ref.constructor.name}" instance to the state before calling view.add()`);
4751
+ }
4739
4752
  // FIXME: ArraySchema/MapSchema do not have metadata
4740
4753
  const metadata = obj.constructor[Symbol.metadata];
4741
- const changeTree = obj[$changes];
4742
4754
  this.visible.add(changeTree);
4743
4755
  // add to iterable list (only the explicitly added items)
4744
4756
  if (this.iterable && checkIncludeParent) {