@colyseus/schema 5.0.2 → 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
 
@@ -5556,6 +5569,33 @@ const Metadata = {
5556
5569
  getStreamPriority(metadata, index) {
5557
5570
  return metadata?.[$streamPriorities]?.[index];
5558
5571
  },
5572
+ /**
5573
+ * Install a single field with full encoder wiring: accessor descriptor
5574
+ * on the prototype + `metadata[$encoders]` slot for primitives. Shared
5575
+ * between `Metadata.setFields` (build path) and
5576
+ * `Reflection.makeEncodable` (Reflection upgrade path).
5577
+ */
5578
+ defineField(target, metadata, fieldIndex, fieldName, type) {
5579
+ const normalized = getNormalizedType(type);
5580
+ const { complexTypeKlass, childType } = resolveFieldType(normalized);
5581
+ Metadata.addField(metadata, fieldIndex, fieldName, normalized, getPropertyDescriptor(fieldName, fieldIndex, childType, complexTypeKlass));
5582
+ // Install accessor descriptor on the prototype (once per class field).
5583
+ if (metadata[$descriptors][fieldName]) {
5584
+ Object.defineProperty(target.prototype, fieldName, metadata[$descriptors][fieldName]);
5585
+ }
5586
+ // Pre-compute encoder function for primitive types.
5587
+ if (typeof normalized === "string") {
5588
+ if (!metadata[$encoders]) {
5589
+ Object.defineProperty(metadata, $encoders, {
5590
+ value: [],
5591
+ enumerable: false,
5592
+ configurable: true,
5593
+ writable: true,
5594
+ });
5595
+ }
5596
+ metadata[$encoders][fieldIndex] = encode[normalized];
5597
+ }
5598
+ },
5559
5599
  setFields(target, fields) {
5560
5600
  // for inheritance support
5561
5601
  const constructor = target.prototype.constructor;
@@ -5593,17 +5633,7 @@ const Metadata = {
5593
5633
  });
5594
5634
  }
5595
5635
  for (const field in fields) {
5596
- const type = getNormalizedType(fields[field]);
5597
- const { complexTypeKlass, childType } = resolveFieldType(type);
5598
- Metadata.addField(metadata, fieldIndex, field, type, getPropertyDescriptor(field, fieldIndex, childType, complexTypeKlass));
5599
- // Install accessor descriptor on the prototype (once per class field).
5600
- if (metadata[$descriptors][field]) {
5601
- Object.defineProperty(target.prototype, field, metadata[$descriptors][field]);
5602
- }
5603
- // Pre-compute encoder function for primitive types.
5604
- if (typeof type === "string") {
5605
- metadata[$encoders][fieldIndex] = encode[type];
5606
- }
5636
+ Metadata.defineField(constructor, metadata, fieldIndex, field, fields[field]);
5607
5637
  fieldIndex++;
5608
5638
  }
5609
5639
  return target;
@@ -6586,8 +6616,19 @@ class Encoder {
6586
6616
  // selected element is passed to `view.add()` which populates
6587
6617
  // view.changes with the stream-link ADD + element-field ADDs.
6588
6618
  this._emitStreamPriority(view);
6589
- // encode visibility-triggered changes collected by view.add()
6590
- 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);
6591
6632
  const changeTree = this.root.changeTrees[refId];
6592
6633
  if (changeTree === undefined) {
6593
6634
  // detached instance, remove from view and skip.