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