@colyseus/schema 3.0.26 → 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.
@@ -1,6 +1,6 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('util')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'util'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.schema = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
@@ -977,10 +977,18 @@
977
977
  }
978
978
  }
979
979
  function deleteOperationAtIndex(changeSet, index) {
980
- const operationsIndex = changeSet.indexes[index];
981
- if (operationsIndex !== undefined) {
982
- changeSet.operations[operationsIndex] = undefined;
980
+ let operationsIndex = changeSet.indexes[index];
981
+ if (operationsIndex === undefined) {
982
+ //
983
+ // if index is not found, we need to find the last operation
984
+ // FIXME: this is not very efficient
985
+ //
986
+ // > See "should allow consecutive splices (same place)" tests
987
+ //
988
+ operationsIndex = Object.values(changeSet.indexes).at(-1);
989
+ index = Object.entries(changeSet.indexes).find(([_, value]) => value === operationsIndex)?.[0];
983
990
  }
991
+ changeSet.operations[operationsIndex] = undefined;
984
992
  delete changeSet.indexes[index];
985
993
  }
986
994
  function enqueueChangeTree(root, changeTree, changeSet, queueRootIndex = changeTree[changeSet].queueRootIndex) {
@@ -1165,14 +1173,9 @@
1165
1173
  }
1166
1174
  _shiftAllChangeIndexes(shiftIndex, startIndex = 0, changeSet) {
1167
1175
  const newIndexes = {};
1176
+ let newKey = 0;
1168
1177
  for (const key in changeSet.indexes) {
1169
- const index = changeSet.indexes[key];
1170
- if (index > startIndex) {
1171
- newIndexes[Number(key) + shiftIndex] = index;
1172
- }
1173
- else {
1174
- newIndexes[key] = index;
1175
- }
1178
+ newIndexes[newKey++] = changeSet.indexes[key];
1176
1179
  }
1177
1180
  changeSet.indexes = newIndexes;
1178
1181
  for (let i = 0; i < changeSet.operations.length; i++) {
@@ -1362,25 +1365,35 @@
1362
1365
  const refType = Metadata.isValidInstance(this.ref)
1363
1366
  ? this.ref.constructor
1364
1367
  : this.ref[$childType];
1365
- if (!Metadata.isValidInstance(parent)) {
1366
- const parentChangeTree = parent[$changes];
1368
+ let parentChangeTree;
1369
+ let parentIsCollection = !Metadata.isValidInstance(parent);
1370
+ if (parentIsCollection) {
1371
+ parentChangeTree = parent[$changes];
1367
1372
  parent = parentChangeTree.parent;
1368
1373
  parentIndex = parentChangeTree.parentIndex;
1369
1374
  }
1375
+ else {
1376
+ parentChangeTree = parent[$changes];
1377
+ }
1370
1378
  const parentConstructor = parent.constructor;
1371
1379
  let key = `${this.root.types.getTypeId(refType)}`;
1372
1380
  if (parentConstructor) {
1373
1381
  key += `-${this.root.types.schemas.get(parentConstructor)}`;
1374
1382
  }
1375
1383
  key += `-${parentIndex}`;
1384
+ const fieldHasViewTag = parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1376
1385
  this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered
1377
1386
  || this.root.types.parentFiltered[key]
1378
- || parentConstructor?.[Symbol.metadata]?.[$viewFieldIndexes]?.includes(parentIndex);
1387
+ || fieldHasViewTag;
1379
1388
  //
1380
1389
  // "isFiltered" may not be imedialely available during `change()` due to the instance not being attached to the root yet.
1381
1390
  // when it's available, we need to enqueue the "changes" changeset into the "filteredChanges" changeset.
1382
1391
  //
1383
1392
  if (this.isFiltered) {
1393
+ this.isVisibilitySharedWithParent = (parentChangeTree.isFiltered &&
1394
+ typeof (refType) !== "string" &&
1395
+ !fieldHasViewTag &&
1396
+ parentIsCollection);
1384
1397
  if (!this.filteredChanges) {
1385
1398
  this.filteredChanges = createChangeSet();
1386
1399
  this.allFilteredChanges = createChangeSet();
@@ -1516,6 +1529,7 @@
1516
1529
  }
1517
1530
  const type = changeTree.getType(field);
1518
1531
  const value = changeTree.getValue(field, isEncodeAll);
1532
+ // console.log({ type, field, value });
1519
1533
  // console.log("encodeArray -> ", {
1520
1534
  // ref: changeTree.ref.constructor.name,
1521
1535
  // field,
@@ -1837,8 +1851,7 @@
1837
1851
  static [(_a$4 = $encoder, _b$4 = $decoder, $filter)](ref, index, view) {
1838
1852
  return (!view ||
1839
1853
  typeof (ref[$childType]) === "string" ||
1840
- // view.items.has(ref[$getByIndex](index)[$changes])
1841
- view.visible.has(ref['tmpItems'][index]?.[$changes]));
1854
+ view.isChangeTreeVisible(ref['tmpItems'][index]?.[$changes]));
1842
1855
  }
1843
1856
  static is(type) {
1844
1857
  return (
@@ -1994,8 +2007,6 @@
1994
2007
  return undefined;
1995
2008
  }
1996
2009
  this[$changes].delete(index, undefined, this.items.length - 1);
1997
- // this.tmpItems[index] = undefined;
1998
- // this.tmpItems.pop();
1999
2010
  this.deletedIndexes[index] = true;
2000
2011
  return this.items.pop();
2001
2012
  }
@@ -2146,31 +2157,50 @@
2146
2157
  * @param deleteCount The number of elements to remove.
2147
2158
  * @param insertItems Elements to insert into the array in place of the deleted elements.
2148
2159
  */
2149
- splice(start, deleteCount = this.items.length - start, ...insertItems) {
2160
+ splice(start, deleteCount, ...insertItems) {
2150
2161
  const changeTree = this[$changes];
2162
+ const itemsLength = this.items.length;
2151
2163
  const tmpItemsLength = this.tmpItems.length;
2152
2164
  const insertCount = insertItems.length;
2153
2165
  // build up-to-date list of indexes, excluding removed values.
2154
2166
  const indexes = [];
2155
2167
  for (let i = 0; i < tmpItemsLength; i++) {
2156
- // if (this.tmpItems[i] !== undefined) {
2157
2168
  if (this.deletedIndexes[i] !== true) {
2158
2169
  indexes.push(i);
2159
2170
  }
2160
2171
  }
2161
- // delete operations at correct index
2162
- for (let i = start; i < start + deleteCount; i++) {
2163
- const index = indexes[i];
2164
- changeTree.delete(index);
2165
- // this.tmpItems[index] = undefined;
2166
- this.deletedIndexes[index] = true;
2172
+ if (itemsLength > start) {
2173
+ // if deleteCount is not provided, delete all items from start to end
2174
+ if (deleteCount === undefined) {
2175
+ deleteCount = itemsLength - start;
2176
+ }
2177
+ //
2178
+ // delete operations at correct index
2179
+ //
2180
+ for (let i = start; i < start + deleteCount; i++) {
2181
+ const index = indexes[i];
2182
+ changeTree.delete(index, exports.OPERATION.DELETE);
2183
+ this.deletedIndexes[index] = true;
2184
+ }
2167
2185
  }
2168
- // force insert operations
2169
- for (let i = 0; i < insertCount; i++) {
2170
- const addIndex = indexes[start] + i;
2171
- changeTree.indexedOperation(addIndex, exports.OPERATION.ADD);
2172
- // set value's parent/root
2173
- insertItems[i][$changes]?.setParent(this, changeTree.root, addIndex);
2186
+ else {
2187
+ // not enough items to delete
2188
+ deleteCount = 0;
2189
+ }
2190
+ // insert operations
2191
+ if (insertCount > 0) {
2192
+ if (insertCount > deleteCount) {
2193
+ console.error("Inserting more elements than deleting during ArraySchema#splice()");
2194
+ throw new Error("ArraySchema#splice(): insertCount must be equal or lower than deleteCount.");
2195
+ }
2196
+ for (let i = 0; i < insertCount; i++) {
2197
+ const addIndex = (indexes[start] ?? itemsLength) + i;
2198
+ changeTree.indexedOperation(addIndex, (this.deletedIndexes[addIndex])
2199
+ ? exports.OPERATION.DELETE_AND_ADD
2200
+ : exports.OPERATION.ADD);
2201
+ // set value's parent/root
2202
+ insertItems[i][$changes]?.setParent(this, changeTree.root, addIndex);
2203
+ }
2174
2204
  }
2175
2205
  //
2176
2206
  // delete exceeding indexes from "allChanges"
@@ -2178,6 +2208,16 @@
2178
2208
  //
2179
2209
  if (deleteCount > insertCount) {
2180
2210
  changeTree.shiftAllChangeIndexes(-(deleteCount - insertCount), indexes[start + insertCount]);
2211
+ // debugChangeSet("AFTER SHIFT indexes", changeTree.allChanges);
2212
+ }
2213
+ //
2214
+ // FIXME: this code block is duplicated on ChangeTree
2215
+ //
2216
+ if (changeTree.filteredChanges !== undefined) {
2217
+ enqueueChangeTree(changeTree.root, changeTree, 'filteredChanges');
2218
+ }
2219
+ else {
2220
+ enqueueChangeTree(changeTree.root, changeTree, 'changes');
2181
2221
  }
2182
2222
  return this.items.splice(start, deleteCount, ...insertItems);
2183
2223
  }
@@ -2433,9 +2473,6 @@
2433
2473
  : this.deletedIndexes[index]
2434
2474
  ? this.items[index]
2435
2475
  : this.tmpItems[index] || this.items[index];
2436
- // return (isEncodeAll)
2437
- // ? this.items[index]
2438
- // : this.tmpItems[index] ?? this.items[index];
2439
2476
  }
2440
2477
  [$deleteByIndex](index) {
2441
2478
  this.items[index] = undefined;
@@ -2495,7 +2532,7 @@
2495
2532
  static [(_a$3 = $encoder, _b$3 = $decoder, $filter)](ref, index, view) {
2496
2533
  return (!view ||
2497
2534
  typeof (ref[$childType]) === "string" ||
2498
- view.visible.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
2535
+ view.isChangeTreeVisible((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
2499
2536
  }
2500
2537
  static is(type) {
2501
2538
  return type['map'] !== undefined;
@@ -3112,7 +3149,7 @@
3112
3149
  }
3113
3150
  else if (tag === DEFAULT_VIEW_TAG) {
3114
3151
  // view pass: default tag
3115
- return view.visible.has(ref[$changes]);
3152
+ return view.isChangeTreeVisible(ref[$changes]);
3116
3153
  }
3117
3154
  else {
3118
3155
  // view pass: custom tag
@@ -3325,7 +3362,7 @@
3325
3362
  static [(_a$1 = $encoder, _b$1 = $decoder, $filter)](ref, index, view) {
3326
3363
  return (!view ||
3327
3364
  typeof (ref[$childType]) === "string" ||
3328
- view.visible.has((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3365
+ view.isChangeTreeVisible((ref[$getByIndex](index) ?? ref.deletedItems[index])[$changes]));
3329
3366
  }
3330
3367
  static is(type) {
3331
3368
  return type['collection'] !== undefined;
@@ -3799,18 +3836,17 @@
3799
3836
  continue;
3800
3837
  }
3801
3838
  if (hasView) {
3802
- if (!view.visible.has(changeTree)) {
3839
+ if (!view.isChangeTreeVisible(changeTree)) {
3840
+ // console.log("MARK AS INVISIBLE:", { ref: changeTree.ref.constructor.name, refId: changeTree.refId, raw: changeTree.ref.toJSON() });
3803
3841
  view.invisible.add(changeTree);
3804
3842
  continue; // skip this change tree
3805
3843
  }
3806
- else {
3807
- view.invisible.delete(changeTree); // remove from invisible list
3808
- }
3844
+ view.invisible.delete(changeTree); // remove from invisible list
3809
3845
  }
3810
- const operations = changeTree[changeSetName];
3846
+ const changeSet = changeTree[changeSetName];
3811
3847
  const ref = changeTree.ref;
3812
3848
  // TODO: avoid iterating over change tree if no changes were made
3813
- const numChanges = operations.operations.length;
3849
+ const numChanges = changeSet.operations.length;
3814
3850
  if (numChanges === 0) {
3815
3851
  continue;
3816
3852
  }
@@ -3825,7 +3861,7 @@
3825
3861
  encode.number(buffer, changeTree.refId, it);
3826
3862
  }
3827
3863
  for (let j = 0; j < numChanges; j++) {
3828
- const fieldIndex = operations.operations[j];
3864
+ const fieldIndex = changeSet.operations[j];
3829
3865
  const operation = (fieldIndex < 0)
3830
3866
  ? Math.abs(fieldIndex) // "pure" operation without fieldIndex (e.g. CLEAR, REVERSE, etc.)
3831
3867
  : (isEncodeAll)
@@ -4939,6 +4975,25 @@
4939
4975
  // clear items array
4940
4976
  this.items.length = 0;
4941
4977
  }
4978
+ isChangeTreeVisible(changeTree) {
4979
+ let isVisible = this.visible.has(changeTree);
4980
+ //
4981
+ // TODO: avoid checking for parent visibility, most of the time it's not needed
4982
+ // See test case: 'should not be required to manually call view.add() items to child arrays without @view() tag'
4983
+ //
4984
+ if (!isVisible && changeTree.isVisibilitySharedWithParent) {
4985
+ // console.log("CHECK AGAINST PARENT...", {
4986
+ // ref: changeTree.ref.constructor.name,
4987
+ // refId: changeTree.refId,
4988
+ // parent: changeTree.parent.constructor.name,
4989
+ // });
4990
+ if (this.visible.has(changeTree.parent[$changes])) {
4991
+ this.visible.add(changeTree);
4992
+ isVisible = true;
4993
+ }
4994
+ }
4995
+ return isVisible;
4996
+ }
4942
4997
  }
4943
4998
 
4944
4999
  registerType("map", { constructor: MapSchema });
package/lib/Schema.js CHANGED
@@ -60,7 +60,7 @@ class Schema {
60
60
  }
61
61
  else if (tag === annotations_1.DEFAULT_VIEW_TAG) {
62
62
  // view pass: default tag
63
- return view.visible.has(ref[symbols_1.$changes]);
63
+ return view.isChangeTreeVisible(ref[symbols_1.$changes]);
64
64
  }
65
65
  else {
66
66
  // view pass: custom tag
package/lib/Schema.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Schema.js","sourceRoot":"","sources":["../src/Schema.ts"],"names":[],"mappings":";;;;AAAA,0CAA4C;AAC5C,+CAAsE;AAItE,qDAAkE;AAClE,6CAA2H;AAG3H,+DAAkE;AAClE,+DAAkE;AAElE,mCAAoC;AAEpC;;GAEG;AACH,MAAa,MAAM;aACR,QAAU,GAAG,uCAAqB,CAAC;aACnC,QAAU,GAAG,uCAAqB,CAAC;IAE1C;;;OAGG;IACH,MAAM,CAAC,UAAU,CAAC,QAAa;QAC3B,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,kBAAQ,EAAE;YACtC,KAAK,EAAE,IAAI,uBAAU,CAAC,QAAQ,CAAC;YAC/B,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;SACjB,CAAC,CAAC;QAEH,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,sBAAY,CAAC,IAAI,EAAE,CAAC,CAAC;IACnG,CAAC;IAED,MAAM,CAAC,EAAE,CAAC,IAAoB;QAC1B,OAAO,OAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,QAAQ,CAAC;QAClD,0CAA0C;QAC1C,yEAAyE;IAC7E,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OA1BC,kBAAQ,OACR,kBAAQ,EAyBR,gBAAM,EAAC,CAAE,UAAsB,EAAE,KAAa,EAAE,YAAu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WAAW,CAAC,IAAI,YAAY,UAAU,CAAC,KAAK,gBAAgB,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC;YAE/I,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;gBAC1B,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;gBACjC,MAAM,IAAI,GAAG,IAAA,iBAAS,EAAC,KAAK,GAAG,CAAC,CAAC,GAAG,gBAAS,CAAC,SAAS,CAAC,KAAK,KAAK,IAAI,CAAC;YAC3E,CAAC;QACL,CAAC;QAED,OAAO,GAAG,MAAM,EAAE,CAAC;IACvB,CAAC;;AA1SL,wBA6SC","sourcesContent":["import { OPERATION } from './encoding/spec';\nimport { DEFAULT_VIEW_TAG, type DefinitionType } from \"./annotations\";\n\nimport { NonFunctionPropNames, ToJSON } from './types/HelperTypes';\n\nimport { ChangeSet, ChangeTree, Ref } from './encoder/ChangeTree';\nimport { $changes, $decoder, $deleteByIndex, $descriptors, $encoder, $filter, $getByIndex, $track } from './types/symbols';\nimport { StateView } from './encoder/StateView';\n\nimport { encodeSchemaOperation } from './encoder/EncodeOperation';\nimport { decodeSchemaOperation } from './decoder/DecodeOperation';\nimport type { Metadata } from './Metadata';\nimport { getIndent } from './utils';\n\n/**\n * Schema encoder / decoder\n */\nexport class Schema {\n static [$encoder] = encodeSchemaOperation;\n static [$decoder] = decodeSchemaOperation;\n\n /**\n * Assign the property descriptors required to track changes on this instance.\n * @param instance\n */\n static initialize(instance: any) {\n Object.defineProperty(instance, $changes, {\n value: new ChangeTree(instance),\n enumerable: false,\n writable: true\n });\n\n Object.defineProperties(instance, instance.constructor[Symbol.metadata]?.[$descriptors] || {});\n }\n\n static is(type: DefinitionType) {\n return typeof(type[Symbol.metadata]) === \"object\";\n // const metadata = type[Symbol.metadata];\n // return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);\n }\n\n /**\n * Track property changes\n */\n static [$track] (changeTree: ChangeTree, index: number, operation: OPERATION = OPERATION.ADD) {\n changeTree.change(index, operation);\n }\n\n /**\n * Determine if a property must be filtered.\n * - If returns false, the property is NOT going to be encoded.\n * - If returns true, the property is going to be encoded.\n *\n * Encoding with \"filters\" happens in two steps:\n * - First, the encoder iterates over all \"not owned\" properties and encodes them.\n * - Then, the encoder iterates over all \"owned\" properties per instance and encodes them.\n */\n static [$filter] (ref: Schema, index: number, view: StateView) {\n const metadata: Metadata = ref.constructor[Symbol.metadata];\n const tag = metadata[index]?.tag;\n\n if (view === undefined) {\n // shared pass/encode: encode if doesn't have a tag\n return tag === undefined;\n\n } else if (tag === undefined) {\n // view pass: no tag\n return true;\n\n } else if (tag === DEFAULT_VIEW_TAG) {\n // view pass: default tag\n return view.visible.has(ref[$changes]);\n\n } else {\n // view pass: custom tag\n const tags = view.tags?.get(ref[$changes]);\n return tags && tags.has(tag);\n }\n }\n\n // allow inherited classes to have a constructor\n constructor(...args: any[]) {\n //\n // inline\n // Schema.initialize(this);\n //\n Schema.initialize(this);\n\n //\n // Assign initial values\n //\n if (args[0]) {\n Object.assign(this, args[0]);\n }\n }\n\n public assign(\n props: { [prop in NonFunctionPropNames<this>]?: this[prop] } | ToJSON<this>,\n ) {\n Object.assign(this, props);\n return this;\n }\n\n /**\n * (Server-side): Flag a property to be encoded for the next patch.\n * @param instance Schema instance\n * @param property string representing the property name, or number representing the index of the property.\n * @param operation OPERATION to perform (detected automatically)\n */\n public setDirty<K extends NonFunctionPropNames<this>>(property: K | number, operation?: OPERATION) {\n const metadata: Metadata = this.constructor[Symbol.metadata];\n this[$changes].change(\n metadata[metadata[property as string]].index,\n operation\n );\n }\n\n clone (): this {\n const cloned = new ((this as any).constructor);\n const metadata: Metadata = this.constructor[Symbol.metadata];\n\n //\n // TODO: clone all properties, not only annotated ones\n //\n // for (const field in this) {\n for (const fieldIndex in metadata) {\n // const field = metadata[metadata[fieldIndex]].name;\n const field = metadata[fieldIndex as any as number].name;\n\n if (\n typeof (this[field]) === \"object\" &&\n typeof (this[field]?.clone) === \"function\"\n ) {\n // deep clone\n cloned[field] = this[field].clone();\n\n } else {\n // primitive values\n cloned[field] = this[field];\n }\n }\n\n return cloned;\n }\n\n toJSON () {\n const obj: unknown = {};\n const metadata = this.constructor[Symbol.metadata];\n for (const index in metadata) {\n const field = metadata[index];\n const fieldName = field.name;\n if (!field.deprecated && this[fieldName] !== null && typeof (this[fieldName]) !== \"undefined\") {\n obj[fieldName] = (typeof (this[fieldName]['toJSON']) === \"function\")\n ? this[fieldName]['toJSON']()\n : this[fieldName];\n }\n }\n return obj as ToJSON<typeof this>;\n }\n\n discardAllChanges() {\n this[$changes].discardAll();\n }\n\n protected [$getByIndex](index: number) {\n const metadata: Metadata = this.constructor[Symbol.metadata];\n return this[metadata[index].name];\n }\n\n protected [$deleteByIndex](index: number) {\n const metadata: Metadata = this.constructor[Symbol.metadata];\n this[metadata[index].name] = undefined;\n }\n\n /**\n * Inspect the `refId` of all Schema instances in the tree. Optionally display the contents of the instance.\n *\n * @param ref Schema instance\n * @param showContents display JSON contents of the instance\n * @returns\n */\n static debugRefIds(ref: Ref, showContents: boolean = false, level: number = 0) {\n const contents = (showContents) ? ` - ${JSON.stringify(ref.toJSON())}` : \"\";\n\n const changeTree: ChangeTree = ref[$changes];\n const refId = changeTree.refId;\n\n let output = \"\";\n output += `${getIndent(level)}${ref.constructor.name} (refId: ${refId})${contents}\\n`;\n\n changeTree.forEachChild((childChangeTree) =>\n output += this.debugRefIds(childChangeTree.ref, showContents, level + 1));\n\n return output;\n }\n\n /**\n * Return a string representation of the changes on a Schema instance.\n * The list of changes is cleared after each encode.\n *\n * @param instance Schema instance\n * @param isEncodeAll Return \"full encode\" instead of current change set.\n * @returns\n */\n static debugChanges(instance: Ref, isEncodeAll: boolean = false) {\n const changeTree: ChangeTree = instance[$changes];\n\n const changeSet = (isEncodeAll) ? changeTree.allChanges : changeTree.changes;\n const changeSetName = (isEncodeAll) ? \"allChanges\" : \"changes\";\n\n let output = `${instance.constructor.name} (${changeTree.refId}) -> .${changeSetName}:\\n`;\n\n function dumpChangeSet(changeSet: ChangeSet) {\n changeSet.operations\n .filter(op => op)\n .forEach((index) => {\n const operation = changeTree.indexedOperations[index];\n console.log({ index, operation })\n output += `- [${index}]: ${OPERATION[operation]} (${JSON.stringify(changeTree.getValue(Number(index), isEncodeAll))})\\n`\n });\n }\n\n dumpChangeSet(changeSet);\n\n // display filtered changes\n if (\n !isEncodeAll &&\n changeTree.filteredChanges &&\n (changeTree.filteredChanges.operations).filter(op => op).length > 0\n ) {\n output += `${instance.constructor.name} (${changeTree.refId}) -> .filteredChanges:\\n`;\n dumpChangeSet(changeTree.filteredChanges);\n }\n\n // display filtered changes\n if (\n isEncodeAll &&\n changeTree.allFilteredChanges &&\n (changeTree.allFilteredChanges.operations).filter(op => op).length > 0\n ) {\n output += `${instance.constructor.name} (${changeTree.refId}) -> .allFilteredChanges:\\n`;\n dumpChangeSet(changeTree.allFilteredChanges);\n }\n\n return output;\n }\n\n static debugChangesDeep(ref: Ref, changeSetName: \"changes\" | \"allChanges\" | \"allFilteredChanges\" | \"filteredChanges\" = \"changes\") {\n let output = \"\";\n\n const rootChangeTree = ref[$changes];\n const root = rootChangeTree.root;\n const changeTrees: Map<ChangeTree, ChangeTree[]> = new Map();\n\n const instanceRefIds = [];\n let totalOperations = 0;\n\n for (const [refId, changes] of Object.entries(root[changeSetName])) {\n const changeTree = root.changeTrees[refId];\n\n let includeChangeTree = false;\n let parentChangeTrees: ChangeTree[] = [];\n let parentChangeTree = changeTree.parent?.[$changes];\n\n if (changeTree === rootChangeTree) {\n includeChangeTree = true;\n\n } else {\n while (parentChangeTree !== undefined) {\n parentChangeTrees.push(parentChangeTree);\n if (parentChangeTree.ref === ref) {\n includeChangeTree = true;\n break;\n }\n parentChangeTree = parentChangeTree.parent?.[$changes];\n }\n }\n\n if (includeChangeTree) {\n instanceRefIds.push(changeTree.refId);\n totalOperations += Object.keys(changes).length;\n changeTrees.set(changeTree, parentChangeTrees.reverse());\n }\n }\n\n output += \"---\\n\"\n output += `root refId: ${rootChangeTree.refId}\\n`;\n output += `Total instances: ${instanceRefIds.length} (refIds: ${instanceRefIds.join(\", \")})\\n`;\n output += `Total changes: ${totalOperations}\\n`;\n output += \"---\\n\"\n\n // based on root.changes, display a tree of changes that has the \"ref\" instance as parent\n const visitedParents = new WeakSet<ChangeTree>();\n for (const [changeTree, parentChangeTrees] of changeTrees.entries()) {\n parentChangeTrees.forEach((parentChangeTree, level) => {\n if (!visitedParents.has(parentChangeTree)) {\n output += `${getIndent(level)}${parentChangeTree.ref.constructor.name} (refId: ${parentChangeTree.refId})\\n`;\n visitedParents.add(parentChangeTree);\n }\n });\n\n const changes = changeTree.indexedOperations;\n const level = parentChangeTrees.length;\n const indent = getIndent(level);\n\n const parentIndex = (level > 0) ? `(${changeTree.parentIndex}) ` : \"\";\n output += `${indent}${parentIndex}${changeTree.ref.constructor.name} (refId: ${changeTree.refId}) - changes: ${Object.keys(changes).length}\\n`;\n\n for (const index in changes) {\n const operation = changes[index];\n output += `${getIndent(level + 1)}${OPERATION[operation]}: ${index}\\n`;\n }\n }\n\n return `${output}`;\n }\n\n\n}\n\n"]}
1
+ {"version":3,"file":"Schema.js","sourceRoot":"","sources":["../src/Schema.ts"],"names":[],"mappings":";;;;AAAA,0CAA4C;AAC5C,+CAAsE;AAItE,qDAAkE;AAClE,6CAA2H;AAG3H,+DAAkE;AAClE,+DAAkE;AAElE,mCAAoC;AAEpC;;GAEG;AACH,MAAa,MAAM;aACR,QAAU,GAAG,uCAAqB,CAAC;aACnC,QAAU,GAAG,uCAAqB,CAAC;IAE1C;;;OAGG;IACH,MAAM,CAAC,UAAU,CAAC,QAAa;QAC3B,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,kBAAQ,EAAE;YACtC,KAAK,EAAE,IAAI,uBAAU,CAAC,QAAQ,CAAC;YAC/B,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;SACjB,CAAC,CAAC;QAEH,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AAgB,CAAC,CAAC;gBACzC,CAAC;YACL,CAAC,CAAC,CAAC;YAEH,MAAM,OAAO,GAAG,UAAU,CAAC,iBAAiB,CAAC;YAC7C,MAAM,KAAK,GAAG,iBAAiB,CAAC,MAAM,CAAC;YACvC,MAAM,MAAM,GAAG,IAAA,iBAAS,EAAC,KAAK,CAAC,CAAC;YAEhC,MAAM,WAAW,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,WAAW,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,YAAY,UAAU,CAAC,KAAK,gBAAgB,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC;YAE/I,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;gBAC1B,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;gBACjC,MAAM,IAAI,GAAG,IAAA,iBAAS,EAAC,KAAK,GAAG,CAAC,CAAC,GAAG,gBAAS,CAAC,SAAS,CAAC,KAAK,KAAK,IAAI,CAAC;YAC3E,CAAC;QACL,CAAC;QAED,OAAO,GAAG,MAAM,EAAE,CAAC;IACvB,CAAC;;AA1SL,wBA6SC","sourcesContent":["import { OPERATION } from './encoding/spec';\nimport { DEFAULT_VIEW_TAG, type DefinitionType } from \"./annotations\";\n\nimport { NonFunctionPropNames, ToJSON } from './types/HelperTypes';\n\nimport { ChangeSet, ChangeTree, Ref } from './encoder/ChangeTree';\nimport { $changes, $decoder, $deleteByIndex, $descriptors, $encoder, $filter, $getByIndex, $track } from './types/symbols';\nimport { StateView } from './encoder/StateView';\n\nimport { encodeSchemaOperation } from './encoder/EncodeOperation';\nimport { decodeSchemaOperation } from './decoder/DecodeOperation';\nimport type { Metadata } from './Metadata';\nimport { getIndent } from './utils';\n\n/**\n * Schema encoder / decoder\n */\nexport class Schema {\n static [$encoder] = encodeSchemaOperation;\n static [$decoder] = decodeSchemaOperation;\n\n /**\n * Assign the property descriptors required to track changes on this instance.\n * @param instance\n */\n static initialize(instance: any) {\n Object.defineProperty(instance, $changes, {\n value: new ChangeTree(instance),\n enumerable: false,\n writable: true\n });\n\n Object.defineProperties(instance, instance.constructor[Symbol.metadata]?.[$descriptors] || {});\n }\n\n static is(type: DefinitionType) {\n return typeof(type[Symbol.metadata]) === \"object\";\n // const metadata = type[Symbol.metadata];\n // return metadata && Object.prototype.hasOwnProperty.call(metadata, -1);\n }\n\n /**\n * Track property changes\n */\n static [$track] (changeTree: ChangeTree, index: number, operation: OPERATION = OPERATION.ADD) {\n changeTree.change(index, operation);\n }\n\n /**\n * Determine if a property must be filtered.\n * - If returns false, the property is NOT going to be encoded.\n * - If returns true, the property is going to be encoded.\n *\n * Encoding with \"filters\" happens in two steps:\n * - First, the encoder iterates over all \"not owned\" properties and encodes them.\n * - Then, the encoder iterates over all \"owned\" properties per instance and encodes them.\n */\n static [$filter] (ref: Schema, index: number, view: StateView) {\n const metadata: Metadata = ref.constructor[Symbol.metadata];\n const tag = metadata[index]?.tag;\n\n if (view === undefined) {\n // shared pass/encode: encode if doesn't have a tag\n return tag === undefined;\n\n } else if (tag === undefined) {\n // view pass: no tag\n return true;\n\n } else if (tag === DEFAULT_VIEW_TAG) {\n // view pass: default tag\n return view.isChangeTreeVisible(ref[$changes]);\n\n } else {\n // view pass: custom tag\n const tags = view.tags?.get(ref[$changes]);\n return tags && tags.has(tag);\n }\n }\n\n // allow inherited classes to have a constructor\n constructor(...args: any[]) {\n //\n // inline\n // Schema.initialize(this);\n //\n Schema.initialize(this);\n\n //\n // Assign initial values\n //\n if (args[0]) {\n Object.assign(this, args[0]);\n }\n }\n\n public assign(\n props: { [prop in NonFunctionPropNames<this>]?: this[prop] } | ToJSON<this>,\n ) {\n Object.assign(this, props);\n return this;\n }\n\n /**\n * (Server-side): Flag a property to be encoded for the next patch.\n * @param instance Schema instance\n * @param property string representing the property name, or number representing the index of the property.\n * @param operation OPERATION to perform (detected automatically)\n */\n public setDirty<K extends NonFunctionPropNames<this>>(property: K | number, operation?: OPERATION) {\n const metadata: Metadata = this.constructor[Symbol.metadata];\n this[$changes].change(\n metadata[metadata[property as string]].index,\n operation\n );\n }\n\n clone (): this {\n const cloned = new ((this as any).constructor);\n const metadata: Metadata = this.constructor[Symbol.metadata];\n\n //\n // TODO: clone all properties, not only annotated ones\n //\n // for (const field in this) {\n for (const fieldIndex in metadata) {\n // const field = metadata[metadata[fieldIndex]].name;\n const field = metadata[fieldIndex as any as number].name;\n\n if (\n typeof (this[field]) === \"object\" &&\n typeof (this[field]?.clone) === \"function\"\n ) {\n // deep clone\n cloned[field] = this[field].clone();\n\n } else {\n // primitive values\n cloned[field] = this[field];\n }\n }\n\n return cloned;\n }\n\n toJSON () {\n const obj: unknown = {};\n const metadata = this.constructor[Symbol.metadata];\n for (const index in metadata) {\n const field = metadata[index];\n const fieldName = field.name;\n if (!field.deprecated && this[fieldName] !== null && typeof (this[fieldName]) !== \"undefined\") {\n obj[fieldName] = (typeof (this[fieldName]['toJSON']) === \"function\")\n ? this[fieldName]['toJSON']()\n : this[fieldName];\n }\n }\n return obj as ToJSON<typeof this>;\n }\n\n discardAllChanges() {\n this[$changes].discardAll();\n }\n\n protected [$getByIndex](index: number) {\n const metadata: Metadata = this.constructor[Symbol.metadata];\n return this[metadata[index].name];\n }\n\n protected [$deleteByIndex](index: number) {\n const metadata: Metadata = this.constructor[Symbol.metadata];\n this[metadata[index].name] = undefined;\n }\n\n /**\n * Inspect the `refId` of all Schema instances in the tree. Optionally display the contents of the instance.\n *\n * @param ref Schema instance\n * @param showContents display JSON contents of the instance\n * @returns\n */\n static debugRefIds(ref: Ref, showContents: boolean = false, level: number = 0) {\n const contents = (showContents) ? ` - ${JSON.stringify(ref.toJSON())}` : \"\";\n\n const changeTree: ChangeTree = ref[$changes];\n const refId = changeTree.refId;\n\n let output = \"\";\n output += `${getIndent(level)}${ref.constructor.name} (refId: ${refId})${contents}\\n`;\n\n changeTree.forEachChild((childChangeTree) =>\n output += this.debugRefIds(childChangeTree.ref, showContents, level + 1));\n\n return output;\n }\n\n /**\n * Return a string representation of the changes on a Schema instance.\n * The list of changes is cleared after each encode.\n *\n * @param instance Schema instance\n * @param isEncodeAll Return \"full encode\" instead of current change set.\n * @returns\n */\n static debugChanges(instance: Ref, isEncodeAll: boolean = false) {\n const changeTree: ChangeTree = instance[$changes];\n\n const changeSet = (isEncodeAll) ? changeTree.allChanges : changeTree.changes;\n const changeSetName = (isEncodeAll) ? \"allChanges\" : \"changes\";\n\n let output = `${instance.constructor.name} (${changeTree.refId}) -> .${changeSetName}:\\n`;\n\n function dumpChangeSet(changeSet: ChangeSet) {\n changeSet.operations\n .filter(op => op)\n .forEach((index) => {\n const operation = changeTree.indexedOperations[index];\n console.log({ index, operation })\n output += `- [${index}]: ${OPERATION[operation]} (${JSON.stringify(changeTree.getValue(Number(index), isEncodeAll))})\\n`\n });\n }\n\n dumpChangeSet(changeSet);\n\n // display filtered changes\n if (\n !isEncodeAll &&\n changeTree.filteredChanges &&\n (changeTree.filteredChanges.operations).filter(op => op).length > 0\n ) {\n output += `${instance.constructor.name} (${changeTree.refId}) -> .filteredChanges:\\n`;\n dumpChangeSet(changeTree.filteredChanges);\n }\n\n // display filtered changes\n if (\n isEncodeAll &&\n changeTree.allFilteredChanges &&\n (changeTree.allFilteredChanges.operations).filter(op => op).length > 0\n ) {\n output += `${instance.constructor.name} (${changeTree.refId}) -> .allFilteredChanges:\\n`;\n dumpChangeSet(changeTree.allFilteredChanges);\n }\n\n return output;\n }\n\n static debugChangesDeep(ref: Ref, changeSetName: \"changes\" | \"allChanges\" | \"allFilteredChanges\" | \"filteredChanges\" = \"changes\") {\n let output = \"\";\n\n const rootChangeTree = ref[$changes];\n const root = rootChangeTree.root;\n const changeTrees: Map<ChangeTree, ChangeTree[]> = new Map();\n\n const instanceRefIds = [];\n let totalOperations = 0;\n\n for (const [refId, changes] of Object.entries(root[changeSetName])) {\n const changeTree = root.changeTrees[refId];\n\n let includeChangeTree = false;\n let parentChangeTrees: ChangeTree[] = [];\n let parentChangeTree = changeTree.parent?.[$changes];\n\n if (changeTree === rootChangeTree) {\n includeChangeTree = true;\n\n } else {\n while (parentChangeTree !== undefined) {\n parentChangeTrees.push(parentChangeTree);\n if (parentChangeTree.ref === ref) {\n includeChangeTree = true;\n break;\n }\n parentChangeTree = parentChangeTree.parent?.[$changes];\n }\n }\n\n if (includeChangeTree) {\n instanceRefIds.push(changeTree.refId);\n totalOperations += Object.keys(changes).length;\n changeTrees.set(changeTree, parentChangeTrees.reverse());\n }\n }\n\n output += \"---\\n\"\n output += `root refId: ${rootChangeTree.refId}\\n`;\n output += `Total instances: ${instanceRefIds.length} (refIds: ${instanceRefIds.join(\", \")})\\n`;\n output += `Total changes: ${totalOperations}\\n`;\n output += \"---\\n\"\n\n // based on root.changes, display a tree of changes that has the \"ref\" instance as parent\n const visitedParents = new WeakSet<ChangeTree>();\n for (const [changeTree, parentChangeTrees] of changeTrees.entries()) {\n parentChangeTrees.forEach((parentChangeTree, level) => {\n if (!visitedParents.has(parentChangeTree)) {\n output += `${getIndent(level)}${parentChangeTree.ref.constructor.name} (refId: ${parentChangeTree.refId})\\n`;\n visitedParents.add(parentChangeTree);\n }\n });\n\n const changes = changeTree.indexedOperations;\n const level = parentChangeTrees.length;\n const indent = getIndent(level);\n\n const parentIndex = (level > 0) ? `(${changeTree.parentIndex}) ` : \"\";\n output += `${indent}${parentIndex}${changeTree.ref.constructor.name} (refId: ${changeTree.refId}) - changes: ${Object.keys(changes).length}\\n`;\n\n for (const index in changes) {\n const operation = changes[index];\n output += `${getIndent(level + 1)}${OPERATION[operation]}: ${index}\\n`;\n }\n }\n\n return `${output}`;\n }\n\n\n}\n\n"]}
@@ -24,11 +24,12 @@ export interface ChangeSet {
24
24
  indexes: {
25
25
  [index: number]: number;
26
26
  };
27
- operations: OPERATION[];
27
+ operations: number[];
28
28
  queueRootIndex?: number;
29
29
  }
30
30
  export declare function setOperationAtIndex(changeSet: ChangeSet, index: number): void;
31
- export declare function deleteOperationAtIndex(changeSet: ChangeSet, index: number): void;
31
+ export declare function deleteOperationAtIndex(changeSet: ChangeSet, index: number | string): void;
32
+ export declare function debugChangeSet(label: string, changeSet: ChangeSet): void;
32
33
  export declare function enqueueChangeTree(root: Root, changeTree: ChangeTree, changeSet: 'changes' | 'filteredChanges' | 'allFilteredChanges', queueRootIndex?: number): void;
33
34
  export declare class ChangeTree<T extends Ref = any> {
34
35
  ref: T;
@@ -40,6 +41,7 @@ export declare class ChangeTree<T extends Ref = any> {
40
41
  * Whether this structure is parent of a filtered structure.
41
42
  */
42
43
  isFiltered: boolean;
44
+ isVisibilitySharedWithParent?: boolean;
43
45
  indexedOperations: IndexedOperations;
44
46
  changes: ChangeSet;
45
47
  allChanges: ChangeSet;
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ChangeTree = void 0;
4
4
  exports.setOperationAtIndex = setOperationAtIndex;
5
5
  exports.deleteOperationAtIndex = deleteOperationAtIndex;
6
+ exports.debugChangeSet = debugChangeSet;
6
7
  exports.enqueueChangeTree = enqueueChangeTree;
8
+ const util = require("util");
7
9
  const spec_1 = require("../encoding/spec");
8
10
  const symbols_1 = require("../types/symbols");
9
11
  const Metadata_1 = require("../Metadata");
@@ -20,12 +22,37 @@ function setOperationAtIndex(changeSet, index) {
20
22
  }
21
23
  }
22
24
  function deleteOperationAtIndex(changeSet, index) {
23
- const operationsIndex = changeSet.indexes[index];
24
- if (operationsIndex !== undefined) {
25
- changeSet.operations[operationsIndex] = undefined;
25
+ let operationsIndex = changeSet.indexes[index];
26
+ if (operationsIndex === undefined) {
27
+ //
28
+ // if index is not found, we need to find the last operation
29
+ // FIXME: this is not very efficient
30
+ //
31
+ // > See "should allow consecutive splices (same place)" tests
32
+ //
33
+ operationsIndex = Object.values(changeSet.indexes).at(-1);
34
+ index = Object.entries(changeSet.indexes).find(([_, value]) => value === operationsIndex)?.[0];
26
35
  }
36
+ changeSet.operations[operationsIndex] = undefined;
27
37
  delete changeSet.indexes[index];
28
38
  }
39
+ function debugChangeSet(label, changeSet) {
40
+ let indexes = [];
41
+ let operations = [];
42
+ for (const index in changeSet.indexes) {
43
+ indexes.push(`\t${util.inspect(index, { colors: true })} => [${util.inspect(changeSet.indexes[index], { colors: true })}]`);
44
+ }
45
+ for (let i = 0; i < changeSet.operations.length; i++) {
46
+ const index = changeSet.operations[i];
47
+ if (index !== undefined) {
48
+ operations.push(`\t[${util.inspect(i, { colors: true })}] => ${util.inspect(index, { colors: true })}`);
49
+ }
50
+ }
51
+ console.log(`${label} =>\nindexes (${Object.keys(changeSet.indexes).length}) {`);
52
+ console.log(indexes.join("\n"), "\n}");
53
+ console.log(`operations (${changeSet.operations.filter(op => op !== undefined).length}) {`);
54
+ console.log(operations.join("\n"), "\n}");
55
+ }
29
56
  function enqueueChangeTree(root, changeTree, changeSet, queueRootIndex = changeTree[changeSet].queueRootIndex) {
30
57
  if (!root) {
31
58
  // skip
@@ -208,14 +235,9 @@ class ChangeTree {
208
235
  }
209
236
  _shiftAllChangeIndexes(shiftIndex, startIndex = 0, changeSet) {
210
237
  const newIndexes = {};
238
+ let newKey = 0;
211
239
  for (const key in changeSet.indexes) {
212
- const index = changeSet.indexes[key];
213
- if (index > startIndex) {
214
- newIndexes[Number(key) + shiftIndex] = index;
215
- }
216
- else {
217
- newIndexes[key] = index;
218
- }
240
+ newIndexes[newKey++] = changeSet.indexes[key];
219
241
  }
220
242
  changeSet.indexes = newIndexes;
221
243
  for (let i = 0; i < changeSet.operations.length; i++) {
@@ -405,25 +427,35 @@ class ChangeTree {
405
427
  const refType = Metadata_1.Metadata.isValidInstance(this.ref)
406
428
  ? this.ref.constructor
407
429
  : this.ref[symbols_1.$childType];
408
- if (!Metadata_1.Metadata.isValidInstance(parent)) {
409
- const parentChangeTree = parent[symbols_1.$changes];
430
+ let parentChangeTree;
431
+ let parentIsCollection = !Metadata_1.Metadata.isValidInstance(parent);
432
+ if (parentIsCollection) {
433
+ parentChangeTree = parent[symbols_1.$changes];
410
434
  parent = parentChangeTree.parent;
411
435
  parentIndex = parentChangeTree.parentIndex;
412
436
  }
437
+ else {
438
+ parentChangeTree = parent[symbols_1.$changes];
439
+ }
413
440
  const parentConstructor = parent.constructor;
414
441
  let key = `${this.root.types.getTypeId(refType)}`;
415
442
  if (parentConstructor) {
416
443
  key += `-${this.root.types.schemas.get(parentConstructor)}`;
417
444
  }
418
445
  key += `-${parentIndex}`;
446
+ const fieldHasViewTag = parentConstructor?.[Symbol.metadata]?.[symbols_1.$viewFieldIndexes]?.includes(parentIndex);
419
447
  this.isFiltered = parent[symbols_1.$changes].isFiltered // in case parent is already filtered
420
448
  || this.root.types.parentFiltered[key]
421
- || parentConstructor?.[Symbol.metadata]?.[symbols_1.$viewFieldIndexes]?.includes(parentIndex);
449
+ || fieldHasViewTag;
422
450
  //
423
451
  // "isFiltered" may not be imedialely available during `change()` due to the instance not being attached to the root yet.
424
452
  // when it's available, we need to enqueue the "changes" changeset into the "filteredChanges" changeset.
425
453
  //
426
454
  if (this.isFiltered) {
455
+ this.isVisibilitySharedWithParent = (parentChangeTree.isFiltered &&
456
+ typeof (refType) !== "string" &&
457
+ !fieldHasViewTag &&
458
+ parentIsCollection);
427
459
  if (!this.filteredChanges) {
428
460
  this.filteredChanges = createChangeSet();
429
461
  this.allFilteredChanges = createChangeSet();