@colyseus/schema 3.0.38 → 3.0.40

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 +1 @@
1
- {"version":3,"file":"StateView.js","sourceRoot":"","sources":["../../src/encoder/StateView.ts"],"names":[],"mappings":";;;AAOA,gCAEC;AARD,8CAAuF;AACvF,gDAAkD;AAClD,2CAA6C;AAC7C,0CAAuC;AACvC,0CAA2C;AAE3C,SAAgB,UAAU,CAAC,WAAoB,KAAK;IAChD,OAAO,IAAI,SAAS,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC;AAED,MAAa,SAAS;IAyBlB,YAAmB,WAAoB,KAAK;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;QAlB5C;;WAEG;QACH,YAAO,GAAwB,IAAI,OAAO,EAAc,CAAC;QAEzD;;WAEG;QACH,cAAS,GAAwB,IAAI,OAAO,EAAc,CAAC;QAI3D;;;WAGG;QACH,YAAO,GAAG,IAAI,GAAG,EAA6B,CAAC;QAG3C,IAAI,QAAQ,EAAE,CAAC;YACX,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QACpB,CAAC;IACL,CAAC;IAED,2CAA2C;IAC3C,GAAG,CAAC,GAAQ,EAAE,MAAc,8BAAgB,EAAE,qBAA8B,IAAI;QAC5E,MAAM,UAAU,GAAe,GAAG,EAAE,CAAC,kBAAQ,CAAC,CAAC;QAE/C,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,OAAO,CAAC,IAAI,CAAC,kCAAkC,EAAE,GAAG,CAAC,CAAC;YACtD,OAAO,IAAI,CAAC;QAEhB,CAAC;aAAM,IACH,CAAC,UAAU,CAAC,MAAM;YAClB,UAAU,CAAC,KAAK,KAAK,CAAC,CAAC,oBAAoB;UAC7C,CAAC;YACC;;;;;;eAMG;YACH,MAAM,IAAI,KAAK,CACX,6EAA6E,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,mDAAmD,CAClK,CAAC;QACN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C;YACrB,CAAC;QACL,CAAC;QAED,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ;AAtTD,8BAsTC","sourcesContent":["import { ChangeSet, ChangeTree, IndexedOperations, Ref } from \"./ChangeTree\";\nimport { $changes, $fieldIndexesByViewTag, $viewFieldIndexes } from \"../types/symbols\";\nimport { DEFAULT_VIEW_TAG } from \"../annotations\";\nimport { OPERATION } from \"../encoding/spec\";\nimport { Metadata } from \"../Metadata\";\nimport { spliceOne } from \"../types/utils\";\n\nexport function createView(iterable: boolean = false) {\n return new StateView(iterable);\n}\n\nexport class StateView {\n /**\n * Iterable list of items that are visible to this view\n * (Available only if constructed with `iterable: true`)\n */\n items: Ref[];\n\n /**\n * List of ChangeTree's that are visible to this view\n */\n visible: WeakSet<ChangeTree> = new WeakSet<ChangeTree>();\n\n /**\n * List of ChangeTree's that are invisible to this view\n */\n invisible: WeakSet<ChangeTree> = new WeakSet<ChangeTree>();\n\n tags?: WeakMap<ChangeTree, Set<number>>; // TODO: use bit manipulation instead of Set<number> ()\n\n /**\n * Manual \"ADD\" operations for changes per ChangeTree, specific to this view.\n * (This is used to force encoding a property, even if it was not changed)\n */\n changes = new Map<number, IndexedOperations>();\n\n constructor(public iterable: boolean = false) {\n if (iterable) {\n this.items = [];\n }\n }\n\n // TODO: allow to set multiple tags at once\n add(obj: Ref, tag: number = DEFAULT_VIEW_TAG, checkIncludeParent: boolean = true) {\n const changeTree: ChangeTree = obj?.[$changes];\n\n if (!changeTree) {\n console.warn(\"StateView#add(), invalid object:\", obj);\n return this;\n\n } else if (\n !changeTree.parent &&\n changeTree.refId !== 0 // allow root object\n ) {\n /**\n * TODO: can we avoid this?\n *\n * When the \"parent\" structure has the @view() tag, it is currently\n * not possible to identify it has to be added to the view as well\n * (this.addParentOf() is not called).\n */\n throw new Error(\n `Cannot add a detached instance to the StateView. Make sure to assign the \"${changeTree.ref.constructor.name}\" instance to the state before calling view.add()`\n );\n }\n\n // FIXME: ArraySchema/MapSchema do not have metadata\n const metadata: Metadata = obj.constructor[Symbol.metadata];\n this.visible.add(changeTree);\n\n // add to iterable list (only the explicitly added items)\n if (this.iterable && checkIncludeParent) {\n this.items.push(obj);\n }\n\n // add parent ChangeTree's\n // - if it was invisible to this view\n // - if it were previously filtered out\n if (checkIncludeParent && changeTree.parent) {\n this.addParentOf(changeTree, tag);\n }\n\n //\n // TODO: when adding an item of a MapSchema, the changes may not\n // be set (only the parent's changes are set)\n //\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(changeTree.refId, changes);\n }\n\n // set tag\n if (tag !== DEFAULT_VIEW_TAG) {\n if (!this.tags) {\n this.tags = new WeakMap<ChangeTree, Set<number>>();\n }\n let tags: Set<number>;\n if (!this.tags.has(changeTree)) {\n tags = new Set<number>();\n this.tags.set(changeTree, tags);\n } else {\n tags = this.tags.get(changeTree);\n }\n tags.add(tag);\n\n // Ref: add tagged properties\n metadata?.[$fieldIndexesByViewTag]?.[tag]?.forEach((index) => {\n if (changeTree.getChange(index) !== OPERATION.DELETE) {\n changes[index] = OPERATION.ADD;\n }\n });\n\n } else {\n const isInvisible = this.invisible.has(changeTree);\n const changeSet = (changeTree.filteredChanges !== undefined)\n ? changeTree.allFilteredChanges\n : changeTree.allChanges;\n\n for (let i = 0, len = changeSet.operations.length; i < len; i++) {\n const index = changeSet.operations[i];\n if (index === undefined) { continue; } // skip \"undefined\" indexes\n\n const op = changeTree.indexedOperations[index] ?? OPERATION.ADD;\n const tagAtIndex = metadata?.[index].tag;\n if (\n !changeTree.isNew && // new structures will be added as part of .encode() call, no need to force it to .encodeView()\n (\n isInvisible || // if \"invisible\", include all\n tagAtIndex === undefined || // \"all change\" with no tag\n tagAtIndex === tag // tagged property\n ) &&\n op !== OPERATION.DELETE\n ) {\n changes[index] = op;\n }\n }\n }\n\n // Add children of this ChangeTree to this view\n changeTree.forEachChild((change, index) => {\n // Do not ADD children that don't have the same tag\n if (\n metadata &&\n metadata[index].tag !== undefined &&\n metadata[index].tag !== tag\n ) {\n return;\n }\n this.add(change.ref, tag, false);\n });\n\n return this;\n }\n\n protected addParentOf(childChangeTree: ChangeTree, tag: number) {\n const changeTree = childChangeTree.parent[$changes];\n const parentIndex = childChangeTree.parentIndex;\n\n if (!this.visible.has(changeTree)) {\n // view must have all \"changeTree\" parent tree\n this.visible.add(changeTree);\n\n // add parent's parent\n const parentChangeTree: ChangeTree = changeTree.parent?.[$changes];\n if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {\n this.addParentOf(changeTree, tag);\n }\n\n // // parent is already available, no need to add it!\n // if (!this.invisible.has(changeTree)) { return; }\n }\n\n // add parent's tag properties\n if (changeTree.getChange(parentIndex) !== OPERATION.DELETE) {\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(changeTree.refId, changes);\n }\n\n if (!this.tags) {\n this.tags = new WeakMap<ChangeTree, Set<number>>();\n }\n\n let tags: Set<number>;\n if (!this.tags.has(changeTree)) {\n tags = new Set<number>();\n this.tags.set(changeTree, tags);\n } else {\n tags = this.tags.get(changeTree);\n }\n tags.add(tag);\n\n changes[parentIndex] = OPERATION.ADD;\n }\n }\n\n remove(obj: Ref, tag?: number): this; // hide _isClear parameter from public API\n remove(obj: Ref, tag?: number, _isClear?: boolean): this;\n remove(obj: Ref, tag: number = DEFAULT_VIEW_TAG, _isClear: boolean = false): this {\n const changeTree: ChangeTree = obj[$changes];\n if (!changeTree) {\n console.warn(\"StateView#remove(), invalid object:\", obj);\n return this;\n }\n\n this.visible.delete(changeTree);\n\n // remove from iterable list\n if (\n this.iterable &&\n !_isClear // no need to remove during clear(), as it will be cleared entirely\n ) {\n spliceOne(this.items, this.items.indexOf(obj));\n }\n\n const ref = changeTree.ref;\n const metadata: Metadata = ref.constructor[Symbol.metadata]; // ArraySchema/MapSchema do not have metadata\n\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(changeTree.refId, changes);\n }\n\n if (tag === DEFAULT_VIEW_TAG) {\n // parent is collection (Map/Array)\n const parent = changeTree.parent;\n if (!Metadata.isValidInstance(parent) && changeTree.isFiltered) {\n const parentChangeTree = parent[$changes];\n let changes = this.changes.get(parentChangeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(parentChangeTree.refId, changes);\n }\n // DELETE / DELETE BY REF ID\n changes[changeTree.parentIndex] = OPERATION.DELETE;\n\n // Remove child schema from visible set\n changeTree.forEachChild((childChangeTree) => {\n this.visible.delete(childChangeTree);\n });\n\n } else {\n // delete all \"tagged\" properties.\n metadata?.[$viewFieldIndexes]?.forEach((index) =>\n changes[index] = OPERATION.DELETE);\n }\n\n } else {\n // delete only tagged properties\n metadata?.[$fieldIndexesByViewTag][tag].forEach((index) =>\n changes[index] = OPERATION.DELETE);\n }\n\n // remove tag\n if (this.tags && this.tags.has(changeTree)) {\n const tags = this.tags.get(changeTree);\n if (tag === undefined) {\n // delete all tags\n this.tags.delete(changeTree);\n } else {\n // delete specific tag\n tags.delete(tag);\n\n // if tag set is empty, delete it entirely\n if (tags.size === 0) {\n this.tags.delete(changeTree);\n }\n }\n }\n\n return this;\n }\n\n has(obj: Ref) {\n return this.visible.has(obj[$changes]);\n }\n\n hasTag(ob: Ref, tag: number = DEFAULT_VIEW_TAG) {\n const tags = this.tags?.get(ob[$changes]);\n return tags?.has(tag) ?? false;\n }\n\n clear() {\n if (!this.iterable) {\n throw new Error(\"StateView#clear() is only available for iterable StateView's. Use StateView(iterable: true) constructor.\");\n }\n\n for (let i = 0, l = this.items.length; i < l; i++) {\n this.remove(this.items[i], DEFAULT_VIEW_TAG, true);\n }\n\n // clear items array\n this.items.length = 0;\n }\n\n isChangeTreeVisible(changeTree: ChangeTree) {\n let isVisible = this.visible.has(changeTree);\n\n //\n // TODO: avoid checking for parent visibility, most of the time it's not needed\n // See test case: 'should not be required to manually call view.add() items to child arrays without @view() tag'\n //\n if (!isVisible && changeTree.isVisibilitySharedWithParent){\n\n // console.log(\"CHECK AGAINST PARENT...\", {\n // ref: changeTree.ref.constructor.name,\n // refId: changeTree.refId,\n // parent: changeTree.parent.constructor.name,\n // });\n\n if (this.visible.has(changeTree.parent[$changes])) {\n this.visible.add(changeTree);\n isVisible = true;\n }\n }\n\n return isVisible;\n }\n}\n"]}
1
+ {"version":3,"file":"StateView.js","sourceRoot":"","sources":["../../src/encoder/StateView.ts"],"names":[],"mappings":";;;AAOA,gCAEC;AARD,8CAAuF;AACvF,gDAAkD;AAClD,2CAA6C;AAC7C,0CAAuC;AACvC,0CAA2C;AAE3C,SAAgB,UAAU,CAAC,WAAoB,KAAK;IAChD,OAAO,IAAI,SAAS,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC;AAED,MAAa,SAAS;IAyBlB,YAAmB,WAAoB,KAAK;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;QAlB5C;;WAEG;QACH,YAAO,GAAwB,IAAI,OAAO,EAAc,CAAC;QAEzD;;WAEG;QACH,cAAS,GAAwB,IAAI,OAAO,EAAc,CAAC;QAI3D;;;WAGG;QACH,YAAO,GAAG,IAAI,GAAG,EAA6B,CAAC;QAG3C,IAAI,QAAQ,EAAE,CAAC;YACX,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QACpB,CAAC;IACL,CAAC;IAED,2CAA2C;IAC3C,GAAG,CAAC,GAAQ,EAAE,MAAc,8BAAgB,EAAE,qBAA8B,IAAI;QAC5E,MAAM,UAAU,GAAe,GAAG,EAAE,CAAC,kBAAQ,CAAC,CAAC;QAE/C,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,OAAO,CAAC,IAAI,CAAC,kCAAkC,EAAE,GAAG,CAAC,CAAC;YACtD,OAAO,IAAI,CAAC;QAEhB,CAAC;aAAM,IACH,CAAC,UAAU,CAAC,MAAM;YAClB,UAAU,CAAC,KAAK,KAAK,CAAC,CAAC,oBAAoB;UAC7C,CAAC;YACC;;;;;;eAMG;YACH,MAAM,IAAI,KAAK,CACX,6EAA6E,UAAU,CAA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AO,SAAS,CAAC;IACrB,CAAC;IAES,iCAAiC,CAAC,UAAsB;QAC9D,UAAU,CAAC,YAAY,CAAC,CAAC,eAAe,EAAE,EAAE;YACxC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;YACrC,IAAI,CAAC,iCAAiC,CAAC,eAAe,CAAC,CAAC;QAC5D,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AA3TD,8BA2TC","sourcesContent":["import { ChangeSet, ChangeTree, IndexedOperations, Ref } from \"./ChangeTree\";\nimport { $changes, $fieldIndexesByViewTag, $viewFieldIndexes } from \"../types/symbols\";\nimport { DEFAULT_VIEW_TAG } from \"../annotations\";\nimport { OPERATION } from \"../encoding/spec\";\nimport { Metadata } from \"../Metadata\";\nimport { spliceOne } from \"../types/utils\";\n\nexport function createView(iterable: boolean = false) {\n return new StateView(iterable);\n}\n\nexport class StateView {\n /**\n * Iterable list of items that are visible to this view\n * (Available only if constructed with `iterable: true`)\n */\n items: Ref[];\n\n /**\n * List of ChangeTree's that are visible to this view\n */\n visible: WeakSet<ChangeTree> = new WeakSet<ChangeTree>();\n\n /**\n * List of ChangeTree's that are invisible to this view\n */\n invisible: WeakSet<ChangeTree> = new WeakSet<ChangeTree>();\n\n tags?: WeakMap<ChangeTree, Set<number>>; // TODO: use bit manipulation instead of Set<number> ()\n\n /**\n * Manual \"ADD\" operations for changes per ChangeTree, specific to this view.\n * (This is used to force encoding a property, even if it was not changed)\n */\n changes = new Map<number, IndexedOperations>();\n\n constructor(public iterable: boolean = false) {\n if (iterable) {\n this.items = [];\n }\n }\n\n // TODO: allow to set multiple tags at once\n add(obj: Ref, tag: number = DEFAULT_VIEW_TAG, checkIncludeParent: boolean = true) {\n const changeTree: ChangeTree = obj?.[$changes];\n\n if (!changeTree) {\n console.warn(\"StateView#add(), invalid object:\", obj);\n return this;\n\n } else if (\n !changeTree.parent &&\n changeTree.refId !== 0 // allow root object\n ) {\n /**\n * TODO: can we avoid this?\n *\n * When the \"parent\" structure has the @view() tag, it is currently\n * not possible to identify it has to be added to the view as well\n * (this.addParentOf() is not called).\n */\n throw new Error(\n `Cannot add a detached instance to the StateView. Make sure to assign the \"${changeTree.ref.constructor.name}\" instance to the state before calling view.add()`\n );\n }\n\n // FIXME: ArraySchema/MapSchema do not have metadata\n const metadata: Metadata = obj.constructor[Symbol.metadata];\n this.visible.add(changeTree);\n\n // add to iterable list (only the explicitly added items)\n if (this.iterable && checkIncludeParent) {\n this.items.push(obj);\n }\n\n // add parent ChangeTree's\n // - if it was invisible to this view\n // - if it were previously filtered out\n if (checkIncludeParent && changeTree.parent) {\n this.addParentOf(changeTree, tag);\n }\n\n //\n // TODO: when adding an item of a MapSchema, the changes may not\n // be set (only the parent's changes are set)\n //\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(changeTree.refId, changes);\n }\n\n // set tag\n if (tag !== DEFAULT_VIEW_TAG) {\n if (!this.tags) {\n this.tags = new WeakMap<ChangeTree, Set<number>>();\n }\n let tags: Set<number>;\n if (!this.tags.has(changeTree)) {\n tags = new Set<number>();\n this.tags.set(changeTree, tags);\n } else {\n tags = this.tags.get(changeTree);\n }\n tags.add(tag);\n\n // Ref: add tagged properties\n metadata?.[$fieldIndexesByViewTag]?.[tag]?.forEach((index) => {\n if (changeTree.getChange(index) !== OPERATION.DELETE) {\n changes[index] = OPERATION.ADD;\n }\n });\n\n } else {\n const isInvisible = this.invisible.has(changeTree);\n const changeSet = (changeTree.filteredChanges !== undefined)\n ? changeTree.allFilteredChanges\n : changeTree.allChanges;\n\n for (let i = 0, len = changeSet.operations.length; i < len; i++) {\n const index = changeSet.operations[i];\n if (index === undefined) { continue; } // skip \"undefined\" indexes\n\n const op = changeTree.indexedOperations[index] ?? OPERATION.ADD;\n const tagAtIndex = metadata?.[index].tag;\n if (\n !changeTree.isNew && // new structures will be added as part of .encode() call, no need to force it to .encodeView()\n (\n isInvisible || // if \"invisible\", include all\n tagAtIndex === undefined || // \"all change\" with no tag\n tagAtIndex === tag // tagged property\n ) &&\n op !== OPERATION.DELETE\n ) {\n changes[index] = op;\n }\n }\n }\n\n // Add children of this ChangeTree to this view\n changeTree.forEachChild((change, index) => {\n // Do not ADD children that don't have the same tag\n if (\n metadata &&\n metadata[index].tag !== undefined &&\n metadata[index].tag !== tag\n ) {\n return;\n }\n this.add(change.ref, tag, false);\n });\n\n return this;\n }\n\n protected addParentOf(childChangeTree: ChangeTree, tag: number) {\n const changeTree = childChangeTree.parent[$changes];\n const parentIndex = childChangeTree.parentIndex;\n\n if (!this.visible.has(changeTree)) {\n // view must have all \"changeTree\" parent tree\n this.visible.add(changeTree);\n\n // add parent's parent\n const parentChangeTree: ChangeTree = changeTree.parent?.[$changes];\n if (parentChangeTree && (parentChangeTree.filteredChanges !== undefined)) {\n this.addParentOf(changeTree, tag);\n }\n\n // // parent is already available, no need to add it!\n // if (!this.invisible.has(changeTree)) { return; }\n }\n\n // add parent's tag properties\n if (changeTree.getChange(parentIndex) !== OPERATION.DELETE) {\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(changeTree.refId, changes);\n }\n\n if (!this.tags) {\n this.tags = new WeakMap<ChangeTree, Set<number>>();\n }\n\n let tags: Set<number>;\n if (!this.tags.has(changeTree)) {\n tags = new Set<number>();\n this.tags.set(changeTree, tags);\n } else {\n tags = this.tags.get(changeTree);\n }\n tags.add(tag);\n\n changes[parentIndex] = OPERATION.ADD;\n }\n }\n\n remove(obj: Ref, tag?: number): this; // hide _isClear parameter from public API\n remove(obj: Ref, tag?: number, _isClear?: boolean): this;\n remove(obj: Ref, tag: number = DEFAULT_VIEW_TAG, _isClear: boolean = false): this {\n const changeTree: ChangeTree = obj[$changes];\n if (!changeTree) {\n console.warn(\"StateView#remove(), invalid object:\", obj);\n return this;\n }\n\n this.visible.delete(changeTree);\n\n // remove from iterable list\n if (\n this.iterable &&\n !_isClear // no need to remove during clear(), as it will be cleared entirely\n ) {\n spliceOne(this.items, this.items.indexOf(obj));\n }\n\n const ref = changeTree.ref;\n const metadata: Metadata = ref.constructor[Symbol.metadata]; // ArraySchema/MapSchema do not have metadata\n\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(changeTree.refId, changes);\n }\n\n if (tag === DEFAULT_VIEW_TAG) {\n // parent is collection (Map/Array)\n const parent = changeTree.parent;\n if (!Metadata.isValidInstance(parent) && changeTree.isFiltered) {\n const parentChangeTree = parent[$changes];\n let changes = this.changes.get(parentChangeTree.refId);\n if (changes === undefined) {\n changes = {};\n this.changes.set(parentChangeTree.refId, changes);\n }\n // DELETE / DELETE BY REF ID\n changes[changeTree.parentIndex] = OPERATION.DELETE;\n\n // Remove child schema from visible set\n this._recursiveDeleteVisibleChangeTree(changeTree);\n\n } else {\n // delete all \"tagged\" properties.\n metadata?.[$viewFieldIndexes]?.forEach((index) =>\n changes[index] = OPERATION.DELETE);\n }\n\n } else {\n // delete only tagged properties\n metadata?.[$fieldIndexesByViewTag][tag].forEach((index) =>\n changes[index] = OPERATION.DELETE);\n }\n\n // remove tag\n if (this.tags && this.tags.has(changeTree)) {\n const tags = this.tags.get(changeTree);\n if (tag === undefined) {\n // delete all tags\n this.tags.delete(changeTree);\n } else {\n // delete specific tag\n tags.delete(tag);\n\n // if tag set is empty, delete it entirely\n if (tags.size === 0) {\n this.tags.delete(changeTree);\n }\n }\n }\n\n return this;\n }\n\n has(obj: Ref) {\n return this.visible.has(obj[$changes]);\n }\n\n hasTag(ob: Ref, tag: number = DEFAULT_VIEW_TAG) {\n const tags = this.tags?.get(ob[$changes]);\n return tags?.has(tag) ?? false;\n }\n\n clear() {\n if (!this.iterable) {\n throw new Error(\"StateView#clear() is only available for iterable StateView's. Use StateView(iterable: true) constructor.\");\n }\n\n for (let i = 0, l = this.items.length; i < l; i++) {\n this.remove(this.items[i], DEFAULT_VIEW_TAG, true);\n }\n\n // clear items array\n this.items.length = 0;\n }\n\n isChangeTreeVisible(changeTree: ChangeTree) {\n let isVisible = this.visible.has(changeTree);\n\n //\n // TODO: avoid checking for parent visibility, most of the time it's not needed\n // See test case: 'should not be required to manually call view.add() items to child arrays without @view() tag'\n //\n if (!isVisible && changeTree.isVisibilitySharedWithParent){\n\n // console.log(\"CHECK AGAINST PARENT...\", {\n // ref: changeTree.ref.constructor.name,\n // refId: changeTree.refId,\n // parent: changeTree.parent.constructor.name,\n // });\n\n if (this.visible.has(changeTree.parent[$changes])) {\n this.visible.add(changeTree);\n isVisible = true;\n }\n }\n\n return isVisible;\n }\n\n protected _recursiveDeleteVisibleChangeTree(changeTree: ChangeTree) {\n changeTree.forEachChild((childChangeTree) => {\n this.visible.delete(childChangeTree);\n this._recursiveDeleteVisibleChangeTree(childChangeTree);\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "3.0.38",
3
+ "version": "3.0.40",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "bin/schema-codegen",
package/src/Schema.ts CHANGED
@@ -9,6 +9,8 @@ import { StateView } from './encoder/StateView';
9
9
 
10
10
  import { encodeSchemaOperation } from './encoder/EncodeOperation';
11
11
  import { decodeSchemaOperation } from './decoder/DecodeOperation';
12
+
13
+ import type { Decoder } from './decoder/Decoder';
12
14
  import type { Metadata } from './Metadata';
13
15
  import { getIndent } from './utils';
14
16
 
@@ -183,25 +185,30 @@ export class Schema {
183
185
  * @param showContents display JSON contents of the instance
184
186
  * @returns
185
187
  */
186
- static debugRefIds(ref: Ref, showContents: boolean = false, level: number = 0) {
188
+ static debugRefIds(ref: Ref, showContents: boolean = false, level: number = 0, decoder?: Decoder) {
187
189
  const contents = (showContents) ? ` - ${JSON.stringify(ref.toJSON())}` : "";
188
-
189
190
  const changeTree: ChangeTree = ref[$changes];
190
- const refId = changeTree.refId;
191
191
 
192
- // log reference count if > 1
193
- const refCount = (changeTree.root?.refCount?.[refId] > 1)
194
- ? ` [×${changeTree.root.refCount[refId]}]`
192
+ const refId = (decoder) ? decoder.root.refIds.get(ref) : changeTree.refId;
193
+ const root = (decoder) ? decoder.root : changeTree.root;
194
+
195
+ // log reference count if > 1
196
+ const refCount = (root?.refCount?.[refId] > 1)
197
+ ? ` [×${root.refCount[refId]}]`
195
198
  : '';
196
199
 
197
200
  let output = `${getIndent(level)}${ref.constructor.name} (refId: ${refId})${refCount}${contents}\n`;
198
201
 
199
202
  changeTree.forEachChild((childChangeTree) =>
200
- output += this.debugRefIds(childChangeTree.ref, showContents, level + 1));
203
+ output += this.debugRefIds(childChangeTree.ref, showContents, level + 1, decoder));
201
204
 
202
205
  return output;
203
206
  }
204
207
 
208
+ static debugRefIdsDecoder(decoder: Decoder) {
209
+ return this.debugRefIds(decoder.state, false, 0, decoder);
210
+ }
211
+
205
212
  /**
206
213
  * Return a string representation of the changes on a Schema instance.
207
214
  * The list of changes is cleared after each encode.
@@ -58,16 +58,23 @@ export class Decoder<T extends Schema = any> {
58
58
  if (bytes[it.offset] == SWITCH_TO_STRUCTURE) {
59
59
  it.offset++;
60
60
 
61
- this.currentRefId = decode.number(bytes, it);
62
- const nextRef = $root.refs.get(this.currentRefId) as Schema;
61
+ const nextRefId = decode.number(bytes, it);
62
+ const nextRef = $root.refs.get(nextRefId);
63
63
 
64
64
  //
65
65
  // Trying to access a reference that haven't been decoded yet.
66
66
  //
67
- if (!nextRef) { throw new Error(`"refId" not found: ${this.currentRefId}`); }
67
+ if (!nextRef) {
68
+ console.error(`"refId" not found: ${nextRefId}`, { previousRef: this, previousRefId: this.currentRefId });
69
+ console.warn("Please report this to the developers. All refIds =>");
70
+ console.warn(Schema.debugRefIdsDecoder(this));
71
+ this.skipCurrentStructure(bytes, it, totalBytes);
72
+ }
68
73
  ref[$onDecodeEnd]?.()
69
- ref = nextRef;
70
74
 
75
+ this.currentRefId = nextRefId;
76
+
77
+ ref = nextRef;
71
78
  decoder = ref.constructor[$decoder];
72
79
 
73
80
  continue;
@@ -77,22 +84,7 @@ export class Decoder<T extends Schema = any> {
77
84
 
78
85
  if (result === DEFINITION_MISMATCH) {
79
86
  console.warn("@colyseus/schema: definition mismatch");
80
-
81
- //
82
- // keep skipping next bytes until reaches a known structure
83
- // by local decoder.
84
- //
85
- const nextIterator: Iterator = { offset: it.offset };
86
- while (it.offset < totalBytes) {
87
- if (bytes[it.offset] === SWITCH_TO_STRUCTURE) {
88
- nextIterator.offset = it.offset + 1;
89
- if ($root.refs.has(decode.number(bytes, nextIterator))) {
90
- break;
91
- }
92
- }
93
-
94
- it.offset++;
95
- }
87
+ this.skipCurrentStructure(bytes, it, totalBytes);
96
88
  continue;
97
89
  }
98
90
  }
@@ -109,6 +101,23 @@ export class Decoder<T extends Schema = any> {
109
101
  return allChanges;
110
102
  }
111
103
 
104
+ skipCurrentStructure(bytes: Buffer, it: Iterator, totalBytes: number) {
105
+ //
106
+ // keep skipping next bytes until reaches a known structure
107
+ // by local decoder.
108
+ //
109
+ const nextIterator: Iterator = { offset: it.offset };
110
+ while (it.offset < totalBytes) {
111
+ if (bytes[it.offset] === SWITCH_TO_STRUCTURE) {
112
+ nextIterator.offset = it.offset + 1;
113
+ if (this.root.refs.has(decode.number(bytes, nextIterator))) {
114
+ break;
115
+ }
116
+ }
117
+ it.offset++;
118
+ }
119
+ }
120
+
112
121
  getInstanceType(bytes: Buffer, it: Iterator, defaultType: typeof Schema): typeof Schema {
113
122
  let type: typeof Schema;
114
123
 
@@ -26,7 +26,7 @@ export class ReferenceTracker {
26
26
  public refs = new Map<number, Ref>();
27
27
  public refIds = new WeakMap<Ref, number>();
28
28
 
29
- public refCounts: { [refId: number]: number; } = {};
29
+ public refCount: { [refId: number]: number; } = {};
30
30
  public deletedRefs = new Set<number>();
31
31
 
32
32
  public callbacks: { [refId: number]: SchemaCallbacks } = {};
@@ -42,7 +42,7 @@ export class ReferenceTracker {
42
42
  this.refIds.set(ref, refId);
43
43
 
44
44
  if (incrementCount) {
45
- this.refCounts[refId] = (this.refCounts[refId] || 0) + 1;
45
+ this.refCount[refId] = (this.refCount[refId] || 0) + 1;
46
46
  }
47
47
 
48
48
  if (this.deletedRefs.has(refId)) {
@@ -52,7 +52,7 @@ export class ReferenceTracker {
52
52
 
53
53
  // for decoding
54
54
  removeRef(refId: number) {
55
- const refCount = this.refCounts[refId];
55
+ const refCount = this.refCount[refId];
56
56
 
57
57
  if (refCount === undefined) {
58
58
  try {
@@ -73,7 +73,7 @@ export class ReferenceTracker {
73
73
  return;
74
74
  }
75
75
 
76
- if ((this.refCounts[refId] = refCount - 1) <= 0) {
76
+ if ((this.refCount[refId] = refCount - 1) <= 0) {
77
77
  this.deletedRefs.add(refId);
78
78
  }
79
79
  }
@@ -82,7 +82,7 @@ export class ReferenceTracker {
82
82
  this.refs.clear();
83
83
  this.deletedRefs.clear();
84
84
  this.callbacks = {};
85
- this.refCounts = {};
85
+ this.refCount = {};
86
86
  }
87
87
 
88
88
  // for decoding
@@ -91,7 +91,7 @@ export class ReferenceTracker {
91
91
  //
92
92
  // Skip active references.
93
93
  //
94
- if (this.refCounts[refId] > 0) { return; }
94
+ if (this.refCount[refId] > 0) { return; }
95
95
 
96
96
  const ref = this.refs.get(refId);
97
97
 
@@ -121,7 +121,7 @@ export class ReferenceTracker {
121
121
  }
122
122
 
123
123
  this.refs.delete(refId); // remove ref
124
- delete this.refCounts[refId]; // remove ref count
124
+ delete this.refCount[refId]; // remove ref count
125
125
  delete this.callbacks[refId]; // remove callbacks
126
126
  });
127
127
 
@@ -238,9 +238,7 @@ export class StateView {
238
238
  changes[changeTree.parentIndex] = OPERATION.DELETE;
239
239
 
240
240
  // Remove child schema from visible set
241
- changeTree.forEachChild((childChangeTree) => {
242
- this.visible.delete(childChangeTree);
243
- });
241
+ this._recursiveDeleteVisibleChangeTree(changeTree);
244
242
 
245
243
  } else {
246
244
  // delete all "tagged" properties.
@@ -319,4 +317,11 @@ export class StateView {
319
317
 
320
318
  return isVisible;
321
319
  }
320
+
321
+ protected _recursiveDeleteVisibleChangeTree(changeTree: ChangeTree) {
322
+ changeTree.forEachChild((childChangeTree) => {
323
+ this.visible.delete(childChangeTree);
324
+ this._recursiveDeleteVisibleChangeTree(childChangeTree);
325
+ });
326
+ }
322
327
  }