@colyseus/schema 3.0.27 → 3.0.28

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.
@@ -1173,18 +1173,6 @@ class ChangeTree {
1173
1173
  for (const key in changeSet.indexes) {
1174
1174
  newIndexes[newKey++] = changeSet.indexes[key];
1175
1175
  }
1176
- // const newIndexes = {};
1177
- // let newKey = 0;
1178
- // for (const key in changeSet.indexes) {
1179
- // const index = changeSet.indexes[key];
1180
- // newIndexes[newKey++] = changeSet.indexes[key];
1181
- // console.log("...shiftAllChangeIndexes", { index: key, targetIndex: index, startIndex, shiftIndex });
1182
- // if (index > startIndex) {
1183
- // newIndexes[Number(key) + shiftIndex] = index;
1184
- // } else {
1185
- // newIndexes[Number(key)] = index;
1186
- // }
1187
- // }
1188
1176
  changeSet.indexes = newIndexes;
1189
1177
  for (let i = 0; i < changeSet.operations.length; i++) {
1190
1178
  const index = changeSet.operations[i];
@@ -1373,25 +1361,35 @@ class ChangeTree {
1373
1361
  const refType = Metadata.isValidInstance(this.ref)
1374
1362
  ? this.ref.constructor
1375
1363
  : this.ref[$childType];
1376
- if (!Metadata.isValidInstance(parent)) {
1377
- const parentChangeTree = parent[$changes];
1364
+ let parentChangeTree;
1365
+ let parentIsCollection = !Metadata.isValidInstance(parent);
1366
+ if (parentIsCollection) {
1367
+ parentChangeTree = parent[$changes];
1378
1368
  parent = parentChangeTree.parent;
1379
1369
  parentIndex = parentChangeTree.parentIndex;
1380
1370
  }
1371
+ else {
1372
+ parentChangeTree = parent[$changes];
1373
+ }
1381
1374
  const parentConstructor = parent.constructor;
1382
1375
  let key = `${this.root.types.getTypeId(refType)}`;
1383
1376
  if (parentConstructor) {
1384
1377
  key += `-${this.root.types.schemas.get(parentConstructor)}`;
1385
1378
  }
1386
1379
  key += `-${parentIndex}`;
1380
+ const fieldHasViewTag = parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1387
1381
  this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered
1388
1382
  || this.root.types.parentFiltered[key]
1389
- || parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1383
+ || fieldHasViewTag;
1390
1384
  //
1391
1385
  // "isFiltered" may not be imedialely available during `change()` due to the instance not being attached to the root yet.
1392
1386
  // when it's available, we need to enqueue the "changes" changeset into the "filteredChanges" changeset.
1393
1387
  //
1394
1388
  if (this.isFiltered) {
1389
+ this.isVisibilitySharedWithParent = (parentChangeTree.isFiltered &&
1390
+ typeof (refType) !== "string" &&
1391
+ !fieldHasViewTag &&
1392
+ parentIsCollection);
1395
1393
  if (!this.filteredChanges) {
1396
1394
  this.filteredChanges = createChangeSet();
1397
1395
  this.allFilteredChanges = createChangeSet();
@@ -1849,8 +1847,7 @@ class ArraySchema {
1849
1847
  static [(_a$4 = $encoder, _b$4 = $decoder, $filter)](ref, index, view) {
1850
1848
  return (!view ||
1851
1849
  typeof (ref[$childType]) === "string" ||
1852
- // view.items.has(ref[$getByIndex](index)[$changes])
1853
- view.visible.has(ref['tmpItems'][index]?.[$changes]));
1850
+ view.isChangeTreeVisible(ref['tmpItems'][index]?.[$changes]));
1854
1851
  }
1855
1852
  static is(type) {
1856
1853
  return (
@@ -2531,7 +2528,7 @@ class MapSchema {
2531
2528
  static [(_a$3 = $encoder, _b$3 = $decoder, $filter)](ref, index, view) {
2532
2529
  return (!view ||
2533
2530
  typeof (ref[$childType]) === "string" ||
2534
- view.visible.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
2531
+ view.isChangeTreeVisible((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
2535
2532
  }
2536
2533
  static is(type) {
2537
2534
  return type['map'] !== undefined;
@@ -3148,7 +3145,7 @@ class Schema {
3148
3145
  }
3149
3146
  else if (tag === DEFAULT_VIEW_TAG) {
3150
3147
  // view pass: default tag
3151
- return view.visible.has(ref[$changes]);
3148
+ return view.isChangeTreeVisible(ref[$changes]);
3152
3149
  }
3153
3150
  else {
3154
3151
  // view pass: custom tag
@@ -3361,7 +3358,7 @@ class CollectionSchema {
3361
3358
  static [(_a$1 = $encoder, _b$1 = $decoder, $filter)](ref, index, view) {
3362
3359
  return (!view ||
3363
3360
  typeof (ref[$childType]) === "string" ||
3364
- view.visible.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3361
+ view.isChangeTreeVisible((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3365
3362
  }
3366
3363
  static is(type) {
3367
3364
  return type['collection'] !== undefined;
@@ -3835,13 +3832,12 @@ class Encoder {
3835
3832
  continue;
3836
3833
  }
3837
3834
  if (hasView) {
3838
- if (!view.visible.has(changeTree)) {
3835
+ if (!view.isChangeTreeVisible(changeTree)) {
3836
+ // console.log("MARK AS INVISIBLE:", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, raw: changeTree.ref.toJSON() });
3839
3837
  view.invisible.add(changeTree);
3840
3838
  continue; // skip this change tree
3841
3839
  }
3842
- else {
3843
- view.invisible.delete(changeTree); // remove from invisible list
3844
- }
3840
+ view.invisible.delete(changeTree); // remove from invisible list
3845
3841
  }
3846
3842
  const changeSet = changeTree[changeSetName];
3847
3843
  const ref = changeTree.ref;
@@ -4975,6 +4971,25 @@ class StateView {
4975
4971
  // clear items array
4976
4972
  this.items.length = 0;
4977
4973
  }
4974
+ isChangeTreeVisible(changeTree) {
4975
+ let isVisible = this.visible.has(changeTree);
4976
+ //
4977
+ // TODO: avoid checking for parent visibility, most of the time it's not needed
4978
+ // See test case: 'should not be required to manually call view.add() items to child arrays without @view() tag'
4979
+ //
4980
+ if (!isVisible && changeTree.isVisibilitySharedWithParent) {
4981
+ // console.log("CHECK AGAINST PARENT...", {
4982
+ // ref: changeTree.ref.constructor.name,
4983
+ // refId: changeTree.refId,
4984
+ // parent: changeTree.parent.constructor.name,
4985
+ // });
4986
+ if (this.visible.has(changeTree.parent[$changes])) {
4987
+ this.visible.add(changeTree);
4988
+ isVisible = true;
4989
+ }
4990
+ }
4991
+ return isVisible;
4992
+ }
4978
4993
  }
4979
4994
 
4980
4995
  registerType("map", { constructor: MapSchema });