@colyseus/schema 5.0.3 → 5.0.4

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.
@@ -1239,11 +1239,24 @@ function checkInheritedFlags(tree, parent, parentIndex) {
1239
1239
  const refType = Metadata.isValidInstance(tree.ref)
1240
1240
  ? tree.ref.constructor
1241
1241
  : tree.ref[$childType];
1242
+ // #218: nested Schema fields inherit visibility from a @view-gated
1243
+ // parent regardless of whether the parent is a collection. The
1244
+ // `parentIsCollection` constraint that used to live here blocked
1245
+ // nested-Schema-field-of-@view-tagged-Schema from sharing visibility,
1246
+ // forcing users to wrap the child in an ArraySchema as a workaround.
1247
+ //
1248
+ // #226 (4.0.25): items inside a non-default-tag `@view(N)` collection
1249
+ // also inherit visibility from the parent collection, so items
1250
+ // pushed/set after `view.add(state, N)` show up automatically.
1251
+ // Default-tag `@view()` collections keep per-item gating —
1252
+ // `view.add(item)` is still required to opt each one in.
1253
+ // The `parentMetadata[parentIndex].tag` access is safe inside the
1254
+ // `fieldHasViewTag` short-circuit (the metadata entry and its `tag`
1255
+ // are guaranteed to exist when that flag is set).
1242
1256
  tree.isVisibilitySharedWithParent = (parentChangeTree.isFiltered
1243
1257
  && typeof refType !== "string"
1244
- && !fieldHasViewTag
1245
1258
  && !fieldHasStream
1246
- && parentIsCollection);
1259
+ && (!fieldHasViewTag || (parentIsCollection && parentMetadata[parentIndex].tag !== DEFAULT_VIEW_TAG)));
1247
1260
  }
1248
1261
  }
1249
1262
 
@@ -6603,8 +6616,19 @@ class Encoder {
6603
6616
  // selected element is passed to `view.add()` which populates
6604
6617
  // view.changes with the stream-link ADD + element-field ADDs.
6605
6618
  this._emitStreamPriority(view);
6606
- // encode visibility-triggered changes collected by view.add()
6607
- for (const [refId, changes] of view.changes) {
6619
+ //
6620
+ // `view.changes` Map insertion order IS topological order:
6621
+ // - `view.add` walks the parent chain to root via `addParentOf`
6622
+ // (depth-first ancestor-first), inserting every ancestor's
6623
+ // entry before the descendant's.
6624
+ // - `view.remove` calls `_touchAncestorsOf` before its own
6625
+ // write to insert any missing ancestors at the front of the
6626
+ // chain — empty entries that get skipped by the size==0
6627
+ // check below but establish Map position.
6628
+ // No per-encode topo sort needed.
6629
+ //
6630
+ for (const refId of view.changes.keys()) {
6631
+ const changes = view.changes.get(refId);
6608
6632
  const changeTree = this.root.changeTrees[refId];
6609
6633
  if (changeTree === undefined) {
6610
6634
  // detached instance, remove from view and skip.