@colyseus/schema 5.0.3 → 5.0.5

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