@colyseus/schema 3.0.43 → 3.0.44

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":"ChangeTree.js","sourceRoot":"","sources":["../../src/encoder/ChangeTree.ts"],"names":[],"mappings":";;;AAgEA,oDAEC;AAED,kDAeC;AAED,kDAOC;AAED,wDAcC;AAED,wCAmBC;AAED,8CAcC;AAjJD,2CAA6C;AAE7C,8CAAgJ;AAQhJ,0CAAuC;AAiDvC,SAAS,eAAe;IACpB,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;AAC3C,CAAC;AAED,+BAA+B;AAC/B,SAAgB,oBAAoB;IAChC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;AAC3D,CAAC;AAED,SAAgB,mBAAmB,CAAC,IAAoB,EAAE,UAAsB;IAC5E,MAAM,IAAI,GAAmB,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IAE9E,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QACb,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;SAAM,CAAC;QACJ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,IAAK,CAAC,IAAI,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,IAAI,CAAC,MAAM,EAAE,CAAC;IAEd,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,SAAgB,mBAAmB,CAAC,SAAoB,EAAE,KAAa;IACnE,MAAM,eAAe,GAAG,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC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C,EAAC,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAC,CAAC,CAAC;YACvD,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,CAAC;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;CAEJ;AAljBD,gCAkjBC","sourcesContent":["import { OPERATION } from \"../encoding/spec\";\nimport { Schema } from \"../Schema\";\nimport { $changes, $childType, $decoder, $onEncodeEnd, $encoder, $getByIndex, $refTypeFieldIndexes, $viewFieldIndexes } from \"../types/symbols\";\n\nimport type { MapSchema } from \"../types/custom/MapSchema\";\nimport type { ArraySchema } from \"../types/custom/ArraySchema\";\nimport type { CollectionSchema } from \"../types/custom/CollectionSchema\";\nimport type { SetSchema } from \"../types/custom/SetSchema\";\n\nimport { Root } from \"./Root\";\nimport { Metadata } from \"../Metadata\";\nimport type { EncodeOperation } from \"./EncodeOperation\";\nimport type { DecodeOperation } from \"../decoder/DecodeOperation\";\n\ndeclare global {\n interface Object {\n // FIXME: not a good practice to extend globals here\n [$changes]?: ChangeTree;\n [$encoder]?: EncodeOperation,\n [$decoder]?: DecodeOperation,\n }\n}\n\nexport type Ref = Schema\n | ArraySchema\n | MapSchema\n | CollectionSchema\n | SetSchema;\n\nexport type ChangeSetName = \"changes\"\n | \"allChanges\"\n | \"filteredChanges\"\n | \"allFilteredChanges\";\n\nexport interface IndexedOperations {\n [index: number]: OPERATION;\n}\n\n// Linked list node for change trees\nexport interface ChangeTreeNode {\n changeTree: ChangeTree;\n next?: ChangeTreeNode;\n prev?: ChangeTreeNode;\n}\n\n// Linked list for change trees\nexport interface ChangeTreeList {\n next?: ChangeTreeNode;\n tail?: ChangeTreeNode;\n length: number;\n}\n\nexport interface ChangeSet {\n // field index -> operation index\n indexes: { [index: number]: number };\n operations: number[];\n queueRootNode?: ChangeTreeNode; // direct reference to ChangeTreeNode in the linked list\n}\n\nfunction createChangeSet(): ChangeSet {\n return { indexes: {}, operations: [] };\n}\n\n// Linked list helper functions\nexport function createChangeTreeList(): ChangeTreeList {\n return { next: undefined, tail: undefined, length: 0 };\n}\n\nexport function addToChangeTreeList(list: ChangeTreeList, changeTree: ChangeTree): ChangeTreeNode {\n const node: ChangeTreeNode = { changeTree, next: undefined, prev: undefined };\n\n if (!list.next) {\n list.next = node;\n list.tail = node;\n } else {\n node.prev = list.tail;\n list.tail!.next = node;\n list.tail = node;\n }\n\n list.length++;\n\n return node;\n}\n\nexport function setOperationAtIndex(changeSet: ChangeSet, index: number) {\n const operationsIndex = changeSet.indexes[index];\n if (operationsIndex === undefined) {\n changeSet.indexes[index] = changeSet.operations.push(index) - 1;\n } else {\n changeSet.operations[operationsIndex] = index;\n }\n}\n\nexport function deleteOperationAtIndex(changeSet: ChangeSet, index: number | string) {\n let operationsIndex = changeSet.indexes[index];\n if (operationsIndex === undefined) {\n //\n // if index is not found, we need to find the last operation\n // FIXME: this is not very efficient\n //\n // > See \"should allow consecutive splices (same place)\" tests\n //\n operationsIndex = Object.values(changeSet.indexes).at(-1);\n index = Object.entries(changeSet.indexes).find(([_, value]) => value === operationsIndex)?.[0];\n }\n changeSet.operations[operationsIndex] = undefined;\n delete changeSet.indexes[index];\n}\n\nexport function debugChangeSet(label: string, changeSet: ChangeSet) {\n let indexes: string[] = [];\n let operations: string[] = [];\n\n for (const index in changeSet.indexes) {\n indexes.push(`\\t${index} => [${changeSet.indexes[index]}]`);\n }\n\n for (let i = 0; i < changeSet.operations.length; i++) {\n const index = changeSet.operations[i];\n if (index !== undefined) {\n operations.push(`\\t[${i}] => ${index}`);\n }\n }\n\n console.log(`${label} =>\\nindexes (${Object.keys(changeSet.indexes).length}) {`);\n console.log(indexes.join(\"\\n\"), \"\\n}\");\n console.log(`operations (${changeSet.operations.filter(op => op !== undefined).length}) {`);\n console.log(operations.join(\"\\n\"), \"\\n}\");\n}\n\nexport function enqueueChangeTree(\n root: Root,\n changeTree: ChangeTree,\n changeSet: 'changes' | 'filteredChanges' | 'allFilteredChanges' | 'allChanges',\n queueRootNode = changeTree[changeSet].queueRootNode\n) {\n // skip\n if (!root) { return; }\n\n if (queueRootNode) {\n } else {\n // Add to linked list if not already present\n changeTree[changeSet].queueRootNode = addToChangeTreeList(root[changeSet], changeTree);\n }\n}\n\nexport interface ParentChain {\n ref: Ref;\n index: number;\n next?: ParentChain;\n}\n\nexport class ChangeTree<T extends Ref=any> {\n ref: T;\n refId: number;\n metadata: Metadata;\n\n root?: Root;\n parentChain?: ParentChain; // Linked list for tracking parents\n\n /**\n * Whether this structure is parent of a filtered structure.\n */\n isFiltered: boolean = false;\n isVisibilitySharedWithParent?: boolean; // See test case: 'should not be required to manually call view.add() items to child arrays without @view() tag'\n\n indexedOperations: IndexedOperations = {};\n\n //\n // TODO:\n // try storing the index + operation per item.\n // example: 1024 & 1025 => ADD, 1026 => DELETE\n //\n // => https://chatgpt.com/share/67107d0c-bc20-8004-8583-83b17dd7c196\n //\n changes: ChangeSet = { indexes: {}, operations: [] };\n allChanges: ChangeSet = { indexes: {}, operations: [] };\n filteredChanges: ChangeSet;\n allFilteredChanges: ChangeSet;\n\n indexes: {[index: string]: any}; // TODO: remove this, only used by MapSchema/SetSchema/CollectionSchema (`encodeKeyValueOperation`)\n\n /**\n * Is this a new instance? Used on ArraySchema to determine OPERATION.MOVE_AND_ADD operation.\n */\n isNew = true;\n\n constructor(ref: T) {\n this.ref = ref;\n this.metadata = ref.constructor[Symbol.metadata];\n\n //\n // Does this structure have \"filters\" declared?\n //\n if (this.metadata?.[$viewFieldIndexes]) {\n this.allFilteredChanges = { indexes: {}, operations: [] };\n this.filteredChanges = { indexes: {}, operations: [] };\n }\n }\n\n setRoot(root: Root) {\n this.root = root;\n\n const isNewChangeTree = this.root.add(this);\n\n this.checkIsFiltered(this.parent, this.parentIndex, isNewChangeTree);\n\n // Recursively set root on child structures\n if (isNewChangeTree) {\n this.forEachChild((child, _) => {\n if (child.root !== root) {\n child.setRoot(root);\n } else {\n root.add(child); // increment refCount\n }\n });\n }\n }\n\n setParent(\n parent: Ref,\n root?: Root,\n parentIndex?: number,\n ) {\n this.addParent(parent, parentIndex);\n\n // avoid setting parents with empty `root`\n if (!root) { return; }\n\n const isNewChangeTree = root.add(this);\n\n // skip if parent is already set\n if (root !== this.root) {\n this.root = root;\n this.checkIsFiltered(parent, parentIndex, isNewChangeTree);\n }\n\n // assign same parent on child structures\n if (isNewChangeTree) {\n //\n // assign same parent on child structures\n //\n this.forEachChild((child, index) => {\n if (child.root === root) {\n //\n // re-assigning a child of the same root, move it to the end\n // of the changes queue so encoding order is preserved\n //\n root.moveToEndOfChanges(child);\n return;\n }\n child.setParent(this.ref, root, index);\n });\n }\n }\n\n forEachChild(callback: (change: ChangeTree, at: any) => void) {\n //\n // assign same parent on child structures\n //\n if (this.ref[$childType]) {\n if (typeof(this.ref[$childType]) !== \"string\") {\n // MapSchema / ArraySchema, etc.\n for (const [key, value] of (this.ref as MapSchema).entries()) {\n callback(value[$changes], key);\n };\n }\n\n } else {\n for (const index of this.metadata?.[$refTypeFieldIndexes] ?? []) {\n const field = this.metadata[index as any as number];\n const value = this.ref[field.name];\n if (!value) { continue; }\n callback(value[$changes], index);\n }\n }\n }\n\n operation(op: OPERATION) {\n // operations without index use negative values to represent them\n // this is checked during .encode() time.\n if (this.filteredChanges !== undefined) {\n this.filteredChanges.operations.push(-op);\n enqueueChangeTree(this.root, this, 'filteredChanges');\n\n } else {\n this.changes.operations.push(-op);\n enqueueChangeTree(this.root, this, 'changes');\n }\n }\n\n change(index: number, operation: OPERATION = OPERATION.ADD) {\n const isFiltered = this.isFiltered || (this.metadata?.[index]?.tag !== undefined);\n const changeSet = (isFiltered)\n ? this.filteredChanges\n : this.changes;\n\n const previousOperation = this.indexedOperations[index];\n if (!previousOperation || previousOperation === OPERATION.DELETE) {\n const op = (!previousOperation)\n ? operation\n : (previousOperation === OPERATION.DELETE)\n ? OPERATION.DELETE_AND_ADD\n : operation\n //\n // TODO: are DELETE operations being encoded as ADD here ??\n //\n this.indexedOperations[index] = op;\n }\n\n setOperationAtIndex(changeSet, index);\n\n if (isFiltered) {\n setOperationAtIndex(this.allFilteredChanges, index);\n\n if (this.root) {\n enqueueChangeTree(this.root, this, 'filteredChanges');\n enqueueChangeTree(this.root, this, 'allFilteredChanges');\n }\n\n } else {\n setOperationAtIndex(this.allChanges, index);\n enqueueChangeTree(this.root, this, 'changes');\n }\n }\n\n shiftChangeIndexes(shiftIndex: number) {\n //\n // Used only during:\n //\n // - ArraySchema#unshift()\n //\n const changeSet = (this.isFiltered)\n ? this.filteredChanges\n : this.changes;\n\n const newIndexedOperations = {};\n const newIndexes = {};\n for (const index in this.indexedOperations) {\n newIndexedOperations[Number(index) + shiftIndex] = this.indexedOperations[index];\n newIndexes[Number(index) + shiftIndex] = changeSet.indexes[index];\n }\n this.indexedOperations = newIndexedOperations;\n changeSet.indexes = newIndexes;\n\n changeSet.operations = changeSet.operations.map((index) => index + shiftIndex);\n }\n\n shiftAllChangeIndexes(shiftIndex: number, startIndex: number = 0) {\n //\n // Used only during:\n //\n // - ArraySchema#splice()\n //\n if (this.filteredChanges !== undefined) {\n this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allFilteredChanges);\n this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);\n\n } else {\n this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);\n }\n }\n\n private _shiftAllChangeIndexes(shiftIndex: number, startIndex: number = 0, changeSet: ChangeSet) {\n const newIndexes = {};\n let newKey = 0;\n for (const key in changeSet.indexes) {\n newIndexes[newKey++] = changeSet.indexes[key];\n }\n changeSet.indexes = newIndexes;\n\n for (let i = 0; i < changeSet.operations.length; i++) {\n const index = changeSet.operations[i];\n if (index > startIndex) {\n changeSet.operations[i] = index + shiftIndex;\n }\n }\n }\n\n indexedOperation(index: number, operation: OPERATION, allChangesIndex: number = index) {\n this.indexedOperations[index] = operation;\n\n if (this.filteredChanges !== undefined) {\n setOperationAtIndex(this.allFilteredChanges, allChangesIndex);\n setOperationAtIndex(this.filteredChanges, index);\n enqueueChangeTree(this.root, this, 'filteredChanges');\n\n } else {\n setOperationAtIndex(this.allChanges, allChangesIndex);\n setOperationAtIndex(this.changes, index);\n enqueueChangeTree(this.root, this, 'changes');\n }\n }\n\n getType(index?: number) {\n return (\n //\n // Get the child type from parent structure.\n // - [\"string\"] => \"string\"\n // - { map: \"string\" } => \"string\"\n // - { set: \"string\" } => \"string\"\n //\n this.ref[$childType] || // ArraySchema | MapSchema | SetSchema | CollectionSchema\n this.metadata[index].type // Schema\n );\n }\n\n getChange(index: number) {\n return this.indexedOperations[index];\n }\n\n //\n // used during `.encode()`\n //\n getValue(index: number, isEncodeAll: boolean = false) {\n //\n // `isEncodeAll` param is only used by ArraySchema\n //\n return this.ref[$getByIndex](index, isEncodeAll);\n }\n\n delete(index: number, operation?: OPERATION, allChangesIndex = index) {\n if (index === undefined) {\n try {\n throw new Error(`@colyseus/schema ${this.ref.constructor.name}: trying to delete non-existing index '${index}'`);\n } catch (e) {\n console.warn(e);\n }\n return;\n }\n\n const changeSet = (this.filteredChanges !== undefined)\n ? this.filteredChanges\n : this.changes;\n\n this.indexedOperations[index] = operation ?? OPERATION.DELETE;\n setOperationAtIndex(changeSet, index);\n deleteOperationAtIndex(this.allChanges, allChangesIndex);\n\n const previousValue = this.getValue(index);\n\n // remove `root` reference\n if (previousValue && previousValue[$changes]) {\n //\n // FIXME: this.root is \"undefined\"\n //\n // This method is being called at decoding time when a DELETE operation is found.\n //\n // - This is due to using the concrete Schema class at decoding time.\n // - \"Reflected\" structures do not have this problem.\n //\n // (The property descriptors should NOT be used at decoding time. only at encoding time.)\n //\n this.root?.remove(previousValue[$changes]);\n }\n\n //\n // FIXME: this is looking a ugly and repeated\n //\n if (this.filteredChanges !== undefined) {\n deleteOperationAtIndex(this.allFilteredChanges, allChangesIndex);\n enqueueChangeTree(this.root, this, 'filteredChanges');\n\n } else {\n enqueueChangeTree(this.root, this, 'changes');\n }\n\n return previousValue;\n }\n\n endEncode(changeSetName: ChangeSetName) {\n this.indexedOperations = {};\n\n // clear changeset\n this[changeSetName] = createChangeSet();\n\n // ArraySchema and MapSchema have a custom \"encode end\" method\n this.ref[$onEncodeEnd]?.();\n\n // Not a new instance anymore\n this.isNew = false;\n }\n\n discard(discardAll: boolean = false) {\n //\n // > MapSchema:\n // Remove cached key to ensure ADD operations is unsed instead of\n // REPLACE in case same key is used on next patches.\n //\n this.ref[$onEncodeEnd]?.();\n\n this.indexedOperations = {};\n this.changes = createChangeSet();\n\n if (this.filteredChanges !== undefined) {\n this.filteredChanges = createChangeSet();\n }\n\n if (discardAll) {\n this.allChanges = createChangeSet();\n\n if (this.allFilteredChanges !== undefined) {\n this.allFilteredChanges = createChangeSet();\n }\n }\n }\n\n /**\n * Recursively discard all changes from this, and child structures.\n * (Used in tests only)\n */\n discardAll() {\n const keys = Object.keys(this.indexedOperations);\n for (let i = 0, len = keys.length; i < len; i++) {\n const value = this.getValue(Number(keys[i]));\n\n if (value && value[$changes]) {\n value[$changes].discardAll();\n }\n }\n\n this.discard();\n }\n\n get changed() {\n return (Object.entries(this.indexedOperations).length > 0);\n }\n\n protected checkIsFiltered(parent: Ref, parentIndex: number, isNewChangeTree: boolean) {\n if (this.root.types.hasFilters) {\n //\n // At Schema initialization, the \"root\" structure might not be available\n // yet, as it only does once the \"Encoder\" has been set up.\n //\n // So the \"parent\" may be already set without a \"root\".\n //\n this._checkFilteredByParent(parent, parentIndex);\n\n if (this.filteredChanges !== undefined) {\n enqueueChangeTree(this.root, this, 'filteredChanges');\n if (isNewChangeTree) {\n enqueueChangeTree(this.root, this, 'allFilteredChanges');\n }\n }\n }\n\n if (!this.isFiltered) {\n enqueueChangeTree(this.root, this, 'changes');\n if (isNewChangeTree) {\n enqueueChangeTree(this.root, this, 'allChanges');\n }\n }\n }\n\n protected _checkFilteredByParent(parent: Ref, parentIndex: number) {\n // skip if parent is not set\n if (!parent) { return; }\n\n //\n // ArraySchema | MapSchema - get the child type\n // (if refType is typeof string, the parentFiltered[key] below will always be invalid)\n //\n const refType = Metadata.isValidInstance(this.ref)\n ? this.ref.constructor\n : this.ref[$childType];\n\n let parentChangeTree: ChangeTree;\n\n let parentIsCollection = !Metadata.isValidInstance(parent);\n if (parentIsCollection) {\n parentChangeTree = parent[$changes];\n parent = parentChangeTree.parent;\n parentIndex = parentChangeTree.parentIndex;\n\n } else {\n parentChangeTree = parent[$changes]\n }\n\n const parentConstructor = parent.constructor as typeof Schema;\n\n let key = `${this.root.types.getTypeId(refType as typeof Schema)}`;\n if (parentConstructor) {\n key += `-${this.root.types.schemas.get(parentConstructor)}`;\n }\n key += `-${parentIndex}`;\n\n const fieldHasViewTag = Metadata.hasViewTagAtIndex(parentConstructor?.[Symbol.metadata], parentIndex);\n\n this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered\n || this.root.types.parentFiltered[key]\n || fieldHasViewTag;\n\n //\n // \"isFiltered\" may not be imedialely available during `change()` due to the instance not being attached to the root yet.\n // when it's available, we need to enqueue the \"changes\" changeset into the \"filteredChanges\" changeset.\n //\n if (this.isFiltered) {\n\n this.isVisibilitySharedWithParent = (\n parentChangeTree.isFiltered &&\n typeof (refType) !== \"string\" &&\n !fieldHasViewTag &&\n parentIsCollection\n );\n\n if (!this.filteredChanges) {\n this.filteredChanges = createChangeSet();\n this.allFilteredChanges = createChangeSet();\n }\n\n if (this.changes.operations.length > 0) {\n this.changes.operations.forEach((index) =>\n setOperationAtIndex(this.filteredChanges, index));\n\n this.allChanges.operations.forEach((index) =>\n setOperationAtIndex(this.allFilteredChanges, index));\n\n this.changes = createChangeSet();\n this.allChanges = createChangeSet();\n }\n }\n }\n\n /**\n * Get the immediate parent\n */\n get parent(): Ref | undefined {\n return this.parentChain?.ref;\n }\n\n /**\n * Get the immediate parent index\n */\n get parentIndex(): number | undefined {\n return this.parentChain?.index;\n }\n\n /**\n * Add a parent to the chain\n */\n addParent(parent: Ref, index: number) {\n // Check if this parent already exists in the chain\n if (this.hasParent((p, i) => p === parent && i === index)) {\n return;\n }\n\n this.parentChain = {\n ref: parent,\n index,\n next: this.parentChain\n };\n }\n\n /**\n * Remove a parent from the chain\n * @param parent - The parent to remove\n * @returns true if parent was removed\n */\n removeParent(parent: Ref): boolean {\n let current = this.parentChain;\n let previous = null;\n while (current) {\n //\n // FIXME: it is required to check against `$changes` here because\n // ArraySchema is instance of Proxy\n //\n if (current.ref[$changes] === parent[$changes]) {\n if (previous) {\n previous.next = current.next;\n } else {\n this.parentChain = current.next;\n }\n return true;\n }\n previous = current;\n current = current.next;\n }\n return this.parentChain === undefined;\n }\n\n /**\n * Find a specific parent in the chain\n */\n findParent(predicate: (parent: Ref, index: number) => boolean): ParentChain | undefined {\n let current = this.parentChain;\n while (current) {\n if (predicate(current.ref, current.index)) {\n return current;\n }\n current = current.next;\n }\n return undefined;\n }\n\n /**\n * Check if this ChangeTree has a specific parent\n */\n hasParent(predicate: (parent: Ref, index: number) => boolean): boolean {\n return this.findParent(predicate) !== undefined;\n }\n\n /**\n * Get all parents as an array (for debugging/testing)\n */\n getAllParents(): Array<{ref: Ref, index: number}> {\n const parents: Array<{ref: Ref, index: number}> = [];\n let current = this.parentChain;\n while (current) {\n parents.push({ref: current.ref, index: current.index});\n current = current.next;\n }\n return parents;\n }\n\n}"]}
1
+ {"version":3,"file":"ChangeTree.js","sourceRoot":"","sources":["../../src/encoder/ChangeTree.ts"],"names":[],"mappings":";;;AAgEA,oDAEC;AAED,kDAeC;AAED,kDAOC;AAED,wDAcC;AAED,wCAmBC;AAED,8CAcC;AAjJD,2CAA6C;AAE7C,8CAAgJ;AAQhJ,0CAAuC;AAiDvC,SAAS,eAAe;IACpB,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;AAC3C,CAAC;AAED,+BAA+B;AAC/B,SAAgB,oBAAoB;IAChC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,EAAE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OAAO,IAAI,CAAC;YAChB,CAAC;YACD,QAAQ,GAAG,OAAO,CAAC;YACnB,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,CAAC;QACD,OAAO,IAAI,CAAC,WAAW,KAAK,SAAS,CAAC;IAC1C,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,SAAkD;QACzD,IAAI,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC;QAC/B,OAAO,OAAO,EAAE,CAAC;YACb,IAAI,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBACxC,OAAO,OAAO,CAAC;YACnB,CAAC;YACD,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,SAAkD;QACxD,OAAO,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,SAAS,CAAC;IACpD,CAAC;IAED;;OAEG;IACH,aAAa;QACT,MAAM,OAAO,GAAuC,EAAE,CAAC;QACvD,IAAI,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC;QAC/B,OAAO,OAAO,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;YACzD,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;QAC3B,CAAC;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;CAEJ;AAnjBD,gCAmjBC","sourcesContent":["import { OPERATION } from \"../encoding/spec\";\nimport { Schema } from \"../Schema\";\nimport { $changes, $childType, $decoder, $onEncodeEnd, $encoder, $getByIndex, $refTypeFieldIndexes, $viewFieldIndexes } from \"../types/symbols\";\n\nimport type { MapSchema } from \"../types/custom/MapSchema\";\nimport type { ArraySchema } from \"../types/custom/ArraySchema\";\nimport type { CollectionSchema } from \"../types/custom/CollectionSchema\";\nimport type { SetSchema } from \"../types/custom/SetSchema\";\n\nimport { Root } from \"./Root\";\nimport { Metadata } from \"../Metadata\";\nimport type { EncodeOperation } from \"./EncodeOperation\";\nimport type { DecodeOperation } from \"../decoder/DecodeOperation\";\n\ndeclare global {\n interface Object {\n // FIXME: not a good practice to extend globals here\n [$changes]?: ChangeTree;\n [$encoder]?: EncodeOperation,\n [$decoder]?: DecodeOperation,\n }\n}\n\nexport type Ref = Schema\n | ArraySchema\n | MapSchema\n | CollectionSchema\n | SetSchema;\n\nexport type ChangeSetName = \"changes\"\n | \"allChanges\"\n | \"filteredChanges\"\n | \"allFilteredChanges\";\n\nexport interface IndexedOperations {\n [index: number]: OPERATION;\n}\n\n// Linked list node for change trees\nexport interface ChangeTreeNode {\n changeTree: ChangeTree;\n next?: ChangeTreeNode;\n prev?: ChangeTreeNode;\n}\n\n// Linked list for change trees\nexport interface ChangeTreeList {\n next?: ChangeTreeNode;\n tail?: ChangeTreeNode;\n length: number;\n}\n\nexport interface ChangeSet {\n // field index -> operation index\n indexes: { [index: number]: number };\n operations: number[];\n queueRootNode?: ChangeTreeNode; // direct reference to ChangeTreeNode in the linked list\n}\n\nfunction createChangeSet(): ChangeSet {\n return { indexes: {}, operations: [] };\n}\n\n// Linked list helper functions\nexport function createChangeTreeList(): ChangeTreeList {\n return { next: undefined, tail: undefined, length: 0 };\n}\n\nexport function addToChangeTreeList(list: ChangeTreeList, changeTree: ChangeTree): ChangeTreeNode {\n const node: ChangeTreeNode = { changeTree, next: undefined, prev: undefined };\n\n if (!list.next) {\n list.next = node;\n list.tail = node;\n } else {\n node.prev = list.tail;\n list.tail!.next = node;\n list.tail = node;\n }\n\n list.length++;\n\n return node;\n}\n\nexport function setOperationAtIndex(changeSet: ChangeSet, index: number) {\n const operationsIndex = changeSet.indexes[index];\n if (operationsIndex === undefined) {\n changeSet.indexes[index] = changeSet.operations.push(index) - 1;\n } else {\n changeSet.operations[operationsIndex] = index;\n }\n}\n\nexport function deleteOperationAtIndex(changeSet: ChangeSet, index: number | string) {\n let operationsIndex = changeSet.indexes[index];\n if (operationsIndex === undefined) {\n //\n // if index is not found, we need to find the last operation\n // FIXME: this is not very efficient\n //\n // > See \"should allow consecutive splices (same place)\" tests\n //\n operationsIndex = Object.values(changeSet.indexes).at(-1);\n index = Object.entries(changeSet.indexes).find(([_, value]) => value === operationsIndex)?.[0];\n }\n changeSet.operations[operationsIndex] = undefined;\n delete changeSet.indexes[index];\n}\n\nexport function debugChangeSet(label: string, changeSet: ChangeSet) {\n let indexes: string[] = [];\n let operations: string[] = [];\n\n for (const index in changeSet.indexes) {\n indexes.push(`\\t${index} => [${changeSet.indexes[index]}]`);\n }\n\n for (let i = 0; i < changeSet.operations.length; i++) {\n const index = changeSet.operations[i];\n if (index !== undefined) {\n operations.push(`\\t[${i}] => ${index}`);\n }\n }\n\n console.log(`${label} =>\\nindexes (${Object.keys(changeSet.indexes).length}) {`);\n console.log(indexes.join(\"\\n\"), \"\\n}\");\n console.log(`operations (${changeSet.operations.filter(op => op !== undefined).length}) {`);\n console.log(operations.join(\"\\n\"), \"\\n}\");\n}\n\nexport function enqueueChangeTree(\n root: Root,\n changeTree: ChangeTree,\n changeSet: 'changes' | 'filteredChanges' | 'allFilteredChanges' | 'allChanges',\n queueRootNode = changeTree[changeSet].queueRootNode\n) {\n // skip\n if (!root) { return; }\n\n if (queueRootNode) {\n } else {\n // Add to linked list if not already present\n changeTree[changeSet].queueRootNode = addToChangeTreeList(root[changeSet], changeTree);\n }\n}\n\nexport interface ParentChain {\n ref: Ref;\n index: number;\n next?: ParentChain;\n}\n\nexport class ChangeTree<T extends Ref = any> {\n ref: T;\n refId: number;\n metadata: Metadata;\n\n root?: Root;\n parentChain?: ParentChain; // Linked list for tracking parents\n\n /**\n * Whether this structure is parent of a filtered structure.\n */\n isFiltered: boolean = false;\n isVisibilitySharedWithParent?: boolean; // See test case: 'should not be required to manually call view.add() items to child arrays without @view() tag'\n\n indexedOperations: IndexedOperations = {};\n\n //\n // TODO:\n // try storing the index + operation per item.\n // example: 1024 & 1025 => ADD, 1026 => DELETE\n //\n // => https://chatgpt.com/share/67107d0c-bc20-8004-8583-83b17dd7c196\n //\n changes: ChangeSet = { indexes: {}, operations: [] };\n allChanges: ChangeSet = { indexes: {}, operations: [] };\n filteredChanges: ChangeSet;\n allFilteredChanges: ChangeSet;\n\n indexes: { [index: string]: any }; // TODO: remove this, only used by MapSchema/SetSchema/CollectionSchema (`encodeKeyValueOperation`)\n\n /**\n * Is this a new instance? Used on ArraySchema to determine OPERATION.MOVE_AND_ADD operation.\n */\n isNew = true;\n\n constructor(ref: T) {\n this.ref = ref;\n this.metadata = ref.constructor[Symbol.metadata];\n\n //\n // Does this structure have \"filters\" declared?\n //\n if (this.metadata?.[$viewFieldIndexes]) {\n this.allFilteredChanges = { indexes: {}, operations: [] };\n this.filteredChanges = { indexes: {}, operations: [] };\n }\n }\n\n setRoot(root: Root) {\n this.root = root;\n\n const isNewChangeTree = this.root.add(this);\n\n this.checkIsFiltered(this.parent, this.parentIndex, isNewChangeTree);\n\n // Recursively set root on child structures\n if (isNewChangeTree) {\n this.forEachChild((child, _) => {\n if (child.root !== root) {\n child.setRoot(root);\n } else {\n root.add(child); // increment refCount\n }\n });\n }\n }\n\n setParent(\n parent: Ref,\n root?: Root,\n parentIndex?: number,\n ) {\n this.addParent(parent, parentIndex);\n\n // avoid setting parents with empty `root`\n if (!root) { return; }\n\n const isNewChangeTree = root.add(this);\n\n // skip if parent is already set\n if (root !== this.root) {\n this.root = root;\n this.checkIsFiltered(parent, parentIndex, isNewChangeTree);\n }\n\n // assign same parent on child structures\n if (isNewChangeTree) {\n //\n // assign same parent on child structures\n //\n this.forEachChild((child, index) => {\n if (child.root === root) {\n //\n // re-assigning a child of the same root, move it to the end\n // of the changes queue so encoding order is preserved\n //\n root.add(child);\n root.moveToEndOfChanges(child);\n return;\n }\n child.setParent(this.ref, root, index);\n });\n }\n }\n\n forEachChild(callback: (change: ChangeTree, at: any) => void) {\n //\n // assign same parent on child structures\n //\n if (this.ref[$childType]) {\n if (typeof (this.ref[$childType]) !== \"string\") {\n // MapSchema / ArraySchema, etc.\n for (const [key, value] of (this.ref as MapSchema).entries()) {\n callback(value[$changes], key);\n };\n }\n\n } else {\n for (const index of this.metadata?.[$refTypeFieldIndexes] ?? []) {\n const field = this.metadata[index as any as number];\n const value = this.ref[field.name];\n if (!value) { continue; }\n callback(value[$changes], index);\n }\n }\n }\n\n operation(op: OPERATION) {\n // operations without index use negative values to represent them\n // this is checked during .encode() time.\n if (this.filteredChanges !== undefined) {\n this.filteredChanges.operations.push(-op);\n enqueueChangeTree(this.root, this, 'filteredChanges');\n\n } else {\n this.changes.operations.push(-op);\n enqueueChangeTree(this.root, this, 'changes');\n }\n }\n\n change(index: number, operation: OPERATION = OPERATION.ADD) {\n const isFiltered = this.isFiltered || (this.metadata?.[index]?.tag !== undefined);\n const changeSet = (isFiltered)\n ? this.filteredChanges\n : this.changes;\n\n const previousOperation = this.indexedOperations[index];\n if (!previousOperation || previousOperation === OPERATION.DELETE) {\n const op = (!previousOperation)\n ? operation\n : (previousOperation === OPERATION.DELETE)\n ? OPERATION.DELETE_AND_ADD\n : operation\n //\n // TODO: are DELETE operations being encoded as ADD here ??\n //\n this.indexedOperations[index] = op;\n }\n\n setOperationAtIndex(changeSet, index);\n\n if (isFiltered) {\n setOperationAtIndex(this.allFilteredChanges, index);\n\n if (this.root) {\n enqueueChangeTree(this.root, this, 'filteredChanges');\n enqueueChangeTree(this.root, this, 'allFilteredChanges');\n }\n\n } else {\n setOperationAtIndex(this.allChanges, index);\n enqueueChangeTree(this.root, this, 'changes');\n }\n }\n\n shiftChangeIndexes(shiftIndex: number) {\n //\n // Used only during:\n //\n // - ArraySchema#unshift()\n //\n const changeSet = (this.isFiltered)\n ? this.filteredChanges\n : this.changes;\n\n const newIndexedOperations = {};\n const newIndexes = {};\n for (const index in this.indexedOperations) {\n newIndexedOperations[Number(index) + shiftIndex] = this.indexedOperations[index];\n newIndexes[Number(index) + shiftIndex] = changeSet.indexes[index];\n }\n this.indexedOperations = newIndexedOperations;\n changeSet.indexes = newIndexes;\n\n changeSet.operations = changeSet.operations.map((index) => index + shiftIndex);\n }\n\n shiftAllChangeIndexes(shiftIndex: number, startIndex: number = 0) {\n //\n // Used only during:\n //\n // - ArraySchema#splice()\n //\n if (this.filteredChanges !== undefined) {\n this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allFilteredChanges);\n this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);\n\n } else {\n this._shiftAllChangeIndexes(shiftIndex, startIndex, this.allChanges);\n }\n }\n\n private _shiftAllChangeIndexes(shiftIndex: number, startIndex: number = 0, changeSet: ChangeSet) {\n const newIndexes = {};\n let newKey = 0;\n for (const key in changeSet.indexes) {\n newIndexes[newKey++] = changeSet.indexes[key];\n }\n changeSet.indexes = newIndexes;\n\n for (let i = 0; i < changeSet.operations.length; i++) {\n const index = changeSet.operations[i];\n if (index > startIndex) {\n changeSet.operations[i] = index + shiftIndex;\n }\n }\n }\n\n indexedOperation(index: number, operation: OPERATION, allChangesIndex: number = index) {\n this.indexedOperations[index] = operation;\n\n if (this.filteredChanges !== undefined) {\n setOperationAtIndex(this.allFilteredChanges, allChangesIndex);\n setOperationAtIndex(this.filteredChanges, index);\n enqueueChangeTree(this.root, this, 'filteredChanges');\n\n } else {\n setOperationAtIndex(this.allChanges, allChangesIndex);\n setOperationAtIndex(this.changes, index);\n enqueueChangeTree(this.root, this, 'changes');\n }\n }\n\n getType(index?: number) {\n return (\n //\n // Get the child type from parent structure.\n // - [\"string\"] => \"string\"\n // - { map: \"string\" } => \"string\"\n // - { set: \"string\" } => \"string\"\n //\n this.ref[$childType] || // ArraySchema | MapSchema | SetSchema | CollectionSchema\n this.metadata[index].type // Schema\n );\n }\n\n getChange(index: number) {\n return this.indexedOperations[index];\n }\n\n //\n // used during `.encode()`\n //\n getValue(index: number, isEncodeAll: boolean = false) {\n //\n // `isEncodeAll` param is only used by ArraySchema\n //\n return this.ref[$getByIndex](index, isEncodeAll);\n }\n\n delete(index: number, operation?: OPERATION, allChangesIndex = index) {\n if (index === undefined) {\n try {\n throw new Error(`@colyseus/schema ${this.ref.constructor.name}: trying to delete non-existing index '${index}'`);\n } catch (e) {\n console.warn(e);\n }\n return;\n }\n\n const changeSet = (this.filteredChanges !== undefined)\n ? this.filteredChanges\n : this.changes;\n\n this.indexedOperations[index] = operation ?? OPERATION.DELETE;\n setOperationAtIndex(changeSet, index);\n deleteOperationAtIndex(this.allChanges, allChangesIndex);\n\n const previousValue = this.getValue(index);\n\n // remove `root` reference\n if (previousValue && previousValue[$changes]) {\n //\n // FIXME: this.root is \"undefined\"\n //\n // This method is being called at decoding time when a DELETE operation is found.\n //\n // - This is due to using the concrete Schema class at decoding time.\n // - \"Reflected\" structures do not have this problem.\n //\n // (The property descriptors should NOT be used at decoding time. only at encoding time.)\n //\n this.root?.remove(previousValue[$changes]);\n }\n\n //\n // FIXME: this is looking a ugly and repeated\n //\n if (this.filteredChanges !== undefined) {\n deleteOperationAtIndex(this.allFilteredChanges, allChangesIndex);\n enqueueChangeTree(this.root, this, 'filteredChanges');\n\n } else {\n enqueueChangeTree(this.root, this, 'changes');\n }\n\n return previousValue;\n }\n\n endEncode(changeSetName: ChangeSetName) {\n this.indexedOperations = {};\n\n // clear changeset\n this[changeSetName] = createChangeSet();\n\n // ArraySchema and MapSchema have a custom \"encode end\" method\n this.ref[$onEncodeEnd]?.();\n\n // Not a new instance anymore\n this.isNew = false;\n }\n\n discard(discardAll: boolean = false) {\n //\n // > MapSchema:\n // Remove cached key to ensure ADD operations is unsed instead of\n // REPLACE in case same key is used on next patches.\n //\n this.ref[$onEncodeEnd]?.();\n\n this.indexedOperations = {};\n this.changes = createChangeSet();\n\n if (this.filteredChanges !== undefined) {\n this.filteredChanges = createChangeSet();\n }\n\n if (discardAll) {\n this.allChanges = createChangeSet();\n\n if (this.allFilteredChanges !== undefined) {\n this.allFilteredChanges = createChangeSet();\n }\n }\n }\n\n /**\n * Recursively discard all changes from this, and child structures.\n * (Used in tests only)\n */\n discardAll() {\n const keys = Object.keys(this.indexedOperations);\n for (let i = 0, len = keys.length; i < len; i++) {\n const value = this.getValue(Number(keys[i]));\n\n if (value && value[$changes]) {\n value[$changes].discardAll();\n }\n }\n\n this.discard();\n }\n\n get changed() {\n return (Object.entries(this.indexedOperations).length > 0);\n }\n\n protected checkIsFiltered(parent: Ref, parentIndex: number, isNewChangeTree: boolean) {\n if (this.root.types.hasFilters) {\n //\n // At Schema initialization, the \"root\" structure might not be available\n // yet, as it only does once the \"Encoder\" has been set up.\n //\n // So the \"parent\" may be already set without a \"root\".\n //\n this._checkFilteredByParent(parent, parentIndex);\n\n if (this.filteredChanges !== undefined) {\n enqueueChangeTree(this.root, this, 'filteredChanges');\n if (isNewChangeTree) {\n enqueueChangeTree(this.root, this, 'allFilteredChanges');\n }\n }\n }\n\n if (!this.isFiltered) {\n enqueueChangeTree(this.root, this, 'changes');\n if (isNewChangeTree) {\n enqueueChangeTree(this.root, this, 'allChanges');\n }\n }\n }\n\n protected _checkFilteredByParent(parent: Ref, parentIndex: number) {\n // skip if parent is not set\n if (!parent) { return; }\n\n //\n // ArraySchema | MapSchema - get the child type\n // (if refType is typeof string, the parentFiltered[key] below will always be invalid)\n //\n const refType = Metadata.isValidInstance(this.ref)\n ? this.ref.constructor\n : this.ref[$childType];\n\n let parentChangeTree: ChangeTree;\n\n let parentIsCollection = !Metadata.isValidInstance(parent);\n if (parentIsCollection) {\n parentChangeTree = parent[$changes];\n parent = parentChangeTree.parent;\n parentIndex = parentChangeTree.parentIndex;\n\n } else {\n parentChangeTree = parent[$changes]\n }\n\n const parentConstructor = parent.constructor as typeof Schema;\n\n let key = `${this.root.types.getTypeId(refType as typeof Schema)}`;\n if (parentConstructor) {\n key += `-${this.root.types.schemas.get(parentConstructor)}`;\n }\n key += `-${parentIndex}`;\n\n const fieldHasViewTag = Metadata.hasViewTagAtIndex(parentConstructor?.[Symbol.metadata], parentIndex);\n\n this.isFiltered = parent[$changes].isFiltered // in case parent is already filtered\n || this.root.types.parentFiltered[key]\n || fieldHasViewTag;\n\n //\n // \"isFiltered\" may not be imedialely available during `change()` due to the instance not being attached to the root yet.\n // when it's available, we need to enqueue the \"changes\" changeset into the \"filteredChanges\" changeset.\n //\n if (this.isFiltered) {\n\n this.isVisibilitySharedWithParent = (\n parentChangeTree.isFiltered &&\n typeof (refType) !== \"string\" &&\n !fieldHasViewTag &&\n parentIsCollection\n );\n\n if (!this.filteredChanges) {\n this.filteredChanges = createChangeSet();\n this.allFilteredChanges = createChangeSet();\n }\n\n if (this.changes.operations.length > 0) {\n this.changes.operations.forEach((index) =>\n setOperationAtIndex(this.filteredChanges, index));\n\n this.allChanges.operations.forEach((index) =>\n setOperationAtIndex(this.allFilteredChanges, index));\n\n this.changes = createChangeSet();\n this.allChanges = createChangeSet();\n }\n }\n }\n\n /**\n * Get the immediate parent\n */\n get parent(): Ref | undefined {\n return this.parentChain?.ref;\n }\n\n /**\n * Get the immediate parent index\n */\n get parentIndex(): number | undefined {\n return this.parentChain?.index;\n }\n\n /**\n * Add a parent to the chain\n */\n addParent(parent: Ref, index: number) {\n // Check if this parent already exists in the chain\n if (this.hasParent((p, i) => p[$changes] === parent[$changes] && i === index)) {\n return;\n }\n\n this.parentChain = {\n ref: parent,\n index,\n next: this.parentChain\n };\n }\n\n /**\n * Remove a parent from the chain\n * @param parent - The parent to remove\n * @returns true if parent was removed\n */\n removeParent(parent: Ref = this.parent): boolean {\n let current = this.parentChain;\n let previous = null;\n while (current) {\n //\n // FIXME: it is required to check against `$changes` here because\n // ArraySchema is instance of Proxy\n //\n if (current.ref[$changes] === parent[$changes]) {\n if (previous) {\n previous.next = current.next;\n } else {\n this.parentChain = current.next;\n }\n return true;\n }\n previous = current;\n current = current.next;\n }\n return this.parentChain === undefined;\n }\n\n /**\n * Find a specific parent in the chain\n */\n findParent(predicate: (parent: Ref, index: number) => boolean): ParentChain | undefined {\n let current = this.parentChain;\n while (current) {\n if (predicate(current.ref, current.index)) {\n return current;\n }\n current = current.next;\n }\n return undefined;\n }\n\n /**\n * Check if this ChangeTree has a specific parent\n */\n hasParent(predicate: (parent: Ref, index: number) => boolean): boolean {\n return this.findParent(predicate) !== undefined;\n }\n\n /**\n * Get all parents as an array (for debugging/testing)\n */\n getAllParents(): Array<{ ref: Ref, index: number }> {\n const parents: Array<{ ref: Ref, index: number }> = [];\n let current = this.parentChain;\n while (current) {\n parents.push({ ref: current.ref, index: current.index });\n current = current.next;\n }\n return parents;\n }\n\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { TypeContext } from "../types/TypeContext";
2
- import { ChangeTree, ChangeTreeList, ChangeSetName } from "./ChangeTree";
2
+ import { ChangeTree, ChangeTreeList, ChangeSetName, Ref } from "./ChangeTree";
3
3
  export declare class Root {
4
4
  types: TypeContext;
5
5
  protected nextUniqueId: number;
@@ -16,7 +16,7 @@ export declare class Root {
16
16
  constructor(types: TypeContext);
17
17
  getNextUniqueId(): number;
18
18
  add(changeTree: ChangeTree): boolean;
19
- remove(changeTree: ChangeTree): number;
19
+ remove(changeTree: ChangeTree, parent?: Ref): number;
20
20
  moveToEndOfChanges(changeTree: ChangeTree): void;
21
21
  moveToEndOfChangeTreeList(changeSetName: ChangeSetName, changeTree: ChangeTree): void;
22
22
  protected removeChangeFromChangeSet(changeSetName: ChangeSetName, changeTree: ChangeTree): boolean;
@@ -42,10 +42,12 @@ class Root {
42
42
  }
43
43
  }
44
44
  this.refCount[changeTree.refId] = (previousRefCount || 0) + 1;
45
+ // console.log("ADD", { refId: changeTree.refId, refCount: this.refCount[changeTree.refId] });
45
46
  return isNewChangeTree;
46
47
  }
47
- remove(changeTree) {
48
+ remove(changeTree, parent) {
48
49
  const refCount = (this.refCount[changeTree.refId]) - 1;
50
+ // console.log("REMOVE", { refId: changeTree.refId, refCount });
49
51
  if (refCount <= 0) {
50
52
  //
51
53
  // Only remove "root" reference if it's the last reference
@@ -64,7 +66,7 @@ class Root {
64
66
  if ((child.parentChain === undefined || // no parent, remove it
65
67
  (child.parentChain && this.refCount[child.refId] > 1) // parent is still in use, but has more than one reference, remove it
66
68
  )) {
67
- this.remove(child);
69
+ this.remove(child, changeTree.ref);
68
70
  }
69
71
  else if (child.parentChain) {
70
72
  // re-assigning a child of the same root, move it to the end
@@ -1 +1 @@
1
- {"version":3,"file":"Root.js","sourceRoot":"","sources":["../../src/encoder/Root.ts"],"names":[],"mappings":";;;AAAA,2CAA6C;AAE7C,6CAAoH;AAEpH,MAAa,IAAI;IAcb,YAAmB,KAAkB;QAAlB,UAAK,GAAL,KAAK,CAAa;QAb3B,iBAAY,GAAW,CAAC,CAAC;QAEnC,aAAQ,GAA2B,EAAE,CAAC;QACtC,gBAAW,GAAkC,EAAE,CAAC;QAEhD,cAAc;QACd,eAAU,GAAmB,IAAA,iCAAoB,GAAE,CAAC;QACpD,uBAAkB,GAAmB,IAAA,iCAAoB,GAAE,CAAC,CAAA,qDAAqD;QAEjH,gCAAgC;QAChC,YAAO,GAAmB,IAAA,iCAAoB,GAAE,CAAC;QACjD,oBAAe,GAAmB,IAAA,iCAAoB,GAAE,CAAC,CAAA,qDAAqD;IAErE,CAAC;IAE1C,eAAe;QACX,OAAO,IAAI,CAAC,YAAY,EAAE,CAAC;IAC/B,CAAC;IAED,GAAG,CAAC,UAAsB;QACtB,kEAAkE;QAClE,IAAI,UAAU,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YACjC,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC,eAAe,EAAE,CAAC;QAC9C,CAAC;QAED,MAAM,eAAe,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,CAAC;QAC3E,IAAI,eAAe,EAAE,CAAC;YAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,UAAU,CAAC;QAAC,CAAC;QAEzE,MAAM,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,IAAI,GAAG,IAAI,CAAC;IAC1B,CAAC;IAES,yBAAyB,CAAC,aAA4B,EAAE,UAAsB;QACpF,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC;QACtC,MAAM,IAAI,GAAG,UAAU,CAAC,aAAa,CAAC,CAAC,aAAa,CAAC;QAErD,IAAI,IAAI,IAAI,IAAI,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;YACzC,uCAAuC;YACvC,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;gBACZ,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACJ,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;YAED,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;gBACZ,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACJ,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;YAED,SAAS,CAAC,MAAM,EAAE,CAAC;YAEnB,6BAA6B;YAC7B,UAAU,CAAC,aAAa,CAAC,CAAC,aAAa,GAAG,SAAS,CAAC;YACpD,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ;AA3KD,oBA2KC","sourcesContent":["import { OPERATION } from \"../encoding/spec\";\nimport { TypeContext } from \"../types/TypeContext\";\nimport { ChangeTree, setOperationAtIndex, ChangeTreeList, createChangeTreeList, ChangeSetName } from \"./ChangeTree\";\n\nexport class Root {\n protected nextUniqueId: number = 0;\n\n refCount: {[id: number]: number} = {};\n changeTrees: {[refId: number]: ChangeTree} = {};\n\n // all changes\n allChanges: ChangeTreeList = createChangeTreeList();\n allFilteredChanges: ChangeTreeList = createChangeTreeList();// TODO: do not initialize it if filters are not used\n\n // pending changes to be encoded\n changes: ChangeTreeList = createChangeTreeList();\n filteredChanges: ChangeTreeList = createChangeTreeList();// TODO: do not initialize it if filters are not used\n\n constructor(public types: TypeContext) { }\n\n getNextUniqueId() {\n return this.nextUniqueId++;\n }\n\n add(changeTree: ChangeTree) {\n // Assign unique `refId` to changeTree if it doesn't have one yet.\n if (changeTree.refId === undefined) {\n changeTree.refId = this.getNextUniqueId();\n }\n\n const isNewChangeTree = (this.changeTrees[changeTree.refId] === undefined);\n if (isNewChangeTree) { this.changeTrees[changeTree.refId] = changeTree; }\n\n const previousRefCount = this.refCount[changeTree.refId];\n if (previousRefCount === 0) {\n //\n // When a ChangeTree is re-added, it means that it was previously removed.\n // We need to re-add all changes to the `changes` map.\n //\n const ops = changeTree.allChanges.operations;\n let len = ops.length;\n while (len--) {\n changeTree.indexedOperations[ops[len]] = OPERATION.ADD;\n setOperationAtIndex(changeTree.changes, len);\n }\n }\n\n this.refCount[changeTree.refId] = (previousRefCount || 0) + 1;\n\n return isNewChangeTree;\n }\n\n remove(changeTree: ChangeTree) {\n const refCount = (this.refCount[changeTree.refId]) - 1;\n\n if (refCount <= 0) {\n\n //\n // Only remove \"root\" reference if it's the last reference\n //\n changeTree.root = undefined;\n delete this.changeTrees[changeTree.refId];\n\n this.removeChangeFromChangeSet(\"allChanges\", changeTree);\n this.removeChangeFromChangeSet(\"changes\", changeTree);\n\n if (changeTree.filteredChanges) {\n this.removeChangeFromChangeSet(\"allFilteredChanges\", changeTree);\n this.removeChangeFromChangeSet(\"filteredChanges\", changeTree);\n }\n\n this.refCount[changeTree.refId] = 0;\n\n changeTree.forEachChild((child, _) => {\n if (child.removeParent(changeTree.ref)) {\n if ((\n child.parentChain === undefined || // no parent, remove it\n (child.parentChain && this.refCount[child.refId] > 1) // parent is still in use, but has more than one reference, remove it\n )) {\n this.remove(child);\n\n } else if (child.parentChain) {\n // re-assigning a child of the same root, move it to the end\n this.moveToEndOfChanges(child);\n }\n }\n });\n\n } else {\n this.refCount[changeTree.refId] = refCount;\n\n //\n // When losing a reference to an instance, it is best to move the\n // ChangeTree to the end of the encoding queue.\n //\n // This way, at decoding time, the instance that contains the\n // ChangeTree will be available before the ChangeTree itself. If the\n // containing instance is not available, the Decoder will throw\n // \"refId not found\" error.\n //\n this.moveToEndOfChanges(changeTree);\n changeTree.forEachChild((child, _) => this.moveToEndOfChanges(child));\n }\n\n return refCount;\n }\n\n moveToEndOfChanges(changeTree: ChangeTree) {\n if (changeTree.filteredChanges) {\n this.moveToEndOfChangeTreeList(\"filteredChanges\", changeTree);\n this.moveToEndOfChangeTreeList(\"allFilteredChanges\", changeTree);\n } else {\n this.moveToEndOfChangeTreeList(\"changes\", changeTree);\n this.moveToEndOfChangeTreeList(\"allChanges\", changeTree);\n }\n }\n\n moveToEndOfChangeTreeList(changeSetName: ChangeSetName, changeTree: ChangeTree): void {\n const changeSet = this[changeSetName];\n const node = changeTree[changeSetName].queueRootNode;\n if (!node || node === changeSet.tail) return;\n\n // Remove from current position\n if (node.prev) {\n node.prev.next = node.next;\n } else {\n changeSet.next = node.next;\n }\n\n if (node.next) {\n node.next.prev = node.prev;\n } else {\n changeSet.tail = node.prev;\n }\n\n // Add to end\n node.prev = changeSet.tail;\n node.next = undefined;\n\n if (changeSet.tail) {\n changeSet.tail.next = node;\n } else {\n changeSet.next = node;\n }\n\n changeSet.tail = node;\n }\n\n protected removeChangeFromChangeSet(changeSetName: ChangeSetName, changeTree: ChangeTree) {\n const changeSet = this[changeSetName];\n const node = changeTree[changeSetName].queueRootNode;\n\n if (node && node.changeTree === changeTree) {\n // Remove the node from the linked list\n if (node.prev) {\n node.prev.next = node.next;\n } else {\n changeSet.next = node.next;\n }\n\n if (node.next) {\n node.next.prev = node.prev;\n } else {\n changeSet.tail = node.prev;\n }\n\n changeSet.length--;\n\n // Clear ChangeTree reference\n changeTree[changeSetName].queueRootNode = undefined;\n return true;\n }\n\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"Root.js","sourceRoot":"","sources":["../../src/encoder/Root.ts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AC,UAAU,KAAK,UAAU,EAAE,CAAC;YACzC,uCAAuC;YACvC,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;gBACZ,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACJ,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;YAED,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;gBACZ,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACJ,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YAC/B,CAAC;YAED,SAAS,CAAC,MAAM,EAAE,CAAC;YAEnB,6BAA6B;YAC7B,UAAU,CAAC,aAAa,CAAC,CAAC,aAAa,GAAG,SAAS,CAAC;YACpD,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ;AA7KD,oBA6KC","sourcesContent":["import { OPERATION } from \"../encoding/spec\";\nimport { TypeContext } from \"../types/TypeContext\";\nimport { ChangeTree, setOperationAtIndex, ChangeTreeList, createChangeTreeList, ChangeSetName, Ref } from \"./ChangeTree\";\n\nexport class Root {\n protected nextUniqueId: number = 0;\n\n refCount: {[id: number]: number} = {};\n changeTrees: {[refId: number]: ChangeTree} = {};\n\n // all changes\n allChanges: ChangeTreeList = createChangeTreeList();\n allFilteredChanges: ChangeTreeList = createChangeTreeList();// TODO: do not initialize it if filters are not used\n\n // pending changes to be encoded\n changes: ChangeTreeList = createChangeTreeList();\n filteredChanges: ChangeTreeList = createChangeTreeList();// TODO: do not initialize it if filters are not used\n\n constructor(public types: TypeContext) { }\n\n getNextUniqueId() {\n return this.nextUniqueId++;\n }\n\n add(changeTree: ChangeTree) {\n // Assign unique `refId` to changeTree if it doesn't have one yet.\n if (changeTree.refId === undefined) {\n changeTree.refId = this.getNextUniqueId();\n }\n\n const isNewChangeTree = (this.changeTrees[changeTree.refId] === undefined);\n if (isNewChangeTree) { this.changeTrees[changeTree.refId] = changeTree; }\n\n const previousRefCount = this.refCount[changeTree.refId];\n if (previousRefCount === 0) {\n //\n // When a ChangeTree is re-added, it means that it was previously removed.\n // We need to re-add all changes to the `changes` map.\n //\n const ops = changeTree.allChanges.operations;\n let len = ops.length;\n while (len--) {\n changeTree.indexedOperations[ops[len]] = OPERATION.ADD;\n setOperationAtIndex(changeTree.changes, len);\n }\n }\n\n this.refCount[changeTree.refId] = (previousRefCount || 0) + 1;\n\n // console.log(\"ADD\", { refId: changeTree.refId, refCount: this.refCount[changeTree.refId] });\n\n return isNewChangeTree;\n }\n\n remove(changeTree: ChangeTree, parent?: Ref) {\n const refCount = (this.refCount[changeTree.refId]) - 1;\n // console.log(\"REMOVE\", { refId: changeTree.refId, refCount });\n\n if (refCount <= 0) {\n //\n // Only remove \"root\" reference if it's the last reference\n //\n changeTree.root = undefined;\n delete this.changeTrees[changeTree.refId];\n\n this.removeChangeFromChangeSet(\"allChanges\", changeTree);\n this.removeChangeFromChangeSet(\"changes\", changeTree);\n\n if (changeTree.filteredChanges) {\n this.removeChangeFromChangeSet(\"allFilteredChanges\", changeTree);\n this.removeChangeFromChangeSet(\"filteredChanges\", changeTree);\n }\n\n this.refCount[changeTree.refId] = 0;\n\n changeTree.forEachChild((child, _) => {\n if (child.removeParent(changeTree.ref)) {\n if ((\n child.parentChain === undefined || // no parent, remove it\n (child.parentChain && this.refCount[child.refId] > 1) // parent is still in use, but has more than one reference, remove it\n )) {\n this.remove(child, changeTree.ref);\n\n } else if (child.parentChain) {\n // re-assigning a child of the same root, move it to the end\n this.moveToEndOfChanges(child);\n }\n }\n });\n\n } else {\n this.refCount[changeTree.refId] = refCount;\n\n //\n // When losing a reference to an instance, it is best to move the\n // ChangeTree to the end of the encoding queue.\n //\n // This way, at decoding time, the instance that contains the\n // ChangeTree will be available before the ChangeTree itself. If the\n // containing instance is not available, the Decoder will throw\n // \"refId not found\" error.\n //\n this.moveToEndOfChanges(changeTree);\n changeTree.forEachChild((child, _) => this.moveToEndOfChanges(child));\n }\n\n return refCount;\n }\n\n moveToEndOfChanges(changeTree: ChangeTree) {\n if (changeTree.filteredChanges) {\n this.moveToEndOfChangeTreeList(\"filteredChanges\", changeTree);\n this.moveToEndOfChangeTreeList(\"allFilteredChanges\", changeTree);\n } else {\n this.moveToEndOfChangeTreeList(\"changes\", changeTree);\n this.moveToEndOfChangeTreeList(\"allChanges\", changeTree);\n }\n }\n\n moveToEndOfChangeTreeList(changeSetName: ChangeSetName, changeTree: ChangeTree): void {\n const changeSet = this[changeSetName];\n const node = changeTree[changeSetName].queueRootNode;\n if (!node || node === changeSet.tail) return;\n\n // Remove from current position\n if (node.prev) {\n node.prev.next = node.next;\n } else {\n changeSet.next = node.next;\n }\n\n if (node.next) {\n node.next.prev = node.prev;\n } else {\n changeSet.tail = node.prev;\n }\n\n // Add to end\n node.prev = changeSet.tail;\n node.next = undefined;\n\n if (changeSet.tail) {\n changeSet.tail.next = node;\n } else {\n changeSet.next = node;\n }\n\n changeSet.tail = node;\n }\n\n protected removeChangeFromChangeSet(changeSetName: ChangeSetName, changeTree: ChangeTree) {\n const changeSet = this[changeSetName];\n const node = changeTree[changeSetName].queueRootNode;\n\n if (node && node.changeTree === changeTree) {\n // Remove the node from the linked list\n if (node.prev) {\n node.prev.next = node.next;\n } else {\n changeSet.next = node.next;\n }\n\n if (node.next) {\n node.next.prev = node.prev;\n } else {\n changeSet.tail = node.prev;\n }\n\n changeSet.length--;\n\n // Clear ChangeTree reference\n changeTree[changeSetName].queueRootNode = undefined;\n return true;\n }\n\n return false;\n }\n}\n"]}
@@ -22,7 +22,7 @@ export declare class StateView {
22
22
  */
23
23
  changes: Map<number, IndexedOperations>;
24
24
  constructor(iterable?: boolean);
25
- add(obj: Ref, tag?: number, checkIncludeParent?: boolean): this;
25
+ add(obj: Ref, tag?: number, checkIncludeParent?: boolean): boolean;
26
26
  protected addParentOf(childChangeTree: ChangeTree, tag: number): void;
27
27
  remove(obj: Ref, tag?: number): this;
28
28
  remove(obj: Ref, tag?: number, _isClear?: boolean): this;
@@ -33,11 +33,12 @@ class StateView {
33
33
  // TODO: allow to set multiple tags at once
34
34
  add(obj, tag = annotations_1.DEFAULT_VIEW_TAG, checkIncludeParent = true) {
35
35
  const changeTree = obj?.[symbols_1.$changes];
36
+ const parentChangeTree = changeTree.parent;
36
37
  if (!changeTree) {
37
38
  console.warn("StateView#add(), invalid object:", obj);
38
- return this;
39
+ return false;
39
40
  }
40
- else if (!changeTree.parent &&
41
+ else if (!parentChangeTree &&
41
42
  changeTree.refId !== 0 // allow root object
42
43
  ) {
43
44
  /**
@@ -59,18 +60,31 @@ class StateView {
59
60
  // add parent ChangeTree's
60
61
  // - if it was invisible to this view
61
62
  // - if it were previously filtered out
62
- if (checkIncludeParent && changeTree.parent) {
63
+ if (checkIncludeParent && parentChangeTree) {
63
64
  this.addParentOf(changeTree, tag);
64
65
  }
65
- //
66
- // TODO: when adding an item of a MapSchema, the changes may not
67
- // be set (only the parent's changes are set)
68
- //
69
66
  let changes = this.changes.get(changeTree.refId);
70
67
  if (changes === undefined) {
71
68
  changes = {};
69
+ // FIXME / OPTIMIZE: do not add if no changes are needed
72
70
  this.changes.set(changeTree.refId, changes);
73
71
  }
72
+ let isChildAdded = false;
73
+ //
74
+ // Add children of this ChangeTree first.
75
+ // If successful, we must link the current ChangeTree to the child.
76
+ //
77
+ changeTree.forEachChild((change, index) => {
78
+ // Do not ADD children that don't have the same tag
79
+ if (metadata &&
80
+ metadata[index].tag !== undefined &&
81
+ metadata[index].tag !== tag) {
82
+ return;
83
+ }
84
+ if (this.add(change.ref, tag, false)) {
85
+ isChildAdded = true;
86
+ }
87
+ });
74
88
  // set tag
75
89
  if (tag !== annotations_1.DEFAULT_VIEW_TAG) {
76
90
  if (!this.tags) {
@@ -92,11 +106,12 @@ class StateView {
92
106
  }
93
107
  });
94
108
  }
95
- else {
96
- const isInvisible = this.invisible.has(changeTree);
109
+ else if (!changeTree.isNew || isChildAdded) {
110
+ // new structures will be added as part of .encode() call, no need to force it to .encodeView()
97
111
  const changeSet = (changeTree.filteredChanges !== undefined)
98
112
  ? changeTree.allFilteredChanges
99
113
  : changeTree.allChanges;
114
+ const isInvisible = this.invisible.has(changeTree);
100
115
  for (let i = 0, len = changeSet.operations.length; i < len; i++) {
101
116
  const index = changeSet.operations[i];
102
117
  if (index === undefined) {
@@ -104,27 +119,17 @@ class StateView {
104
119
  } // skip "undefined" indexes
105
120
  const op = changeTree.indexedOperations[index] ?? spec_1.OPERATION.ADD;
106
121
  const tagAtIndex = metadata?.[index].tag;
107
- if (!changeTree.isNew && // new structures will be added as part of .encode() call, no need to force it to .encodeView()
122
+ if (op !== spec_1.OPERATION.DELETE &&
108
123
  (isInvisible || // if "invisible", include all
109
124
  tagAtIndex === undefined || // "all change" with no tag
110
125
  tagAtIndex === tag // tagged property
111
- ) &&
112
- op !== spec_1.OPERATION.DELETE) {
126
+ )) {
113
127
  changes[index] = op;
128
+ isChildAdded = true; // FIXME: assign only once
114
129
  }
115
130
  }
116
131
  }
117
- // Add children of this ChangeTree to this view
118
- changeTree.forEachChild((change, index) => {
119
- // Do not ADD children that don't have the same tag
120
- if (metadata &&
121
- metadata[index].tag !== undefined &&
122
- metadata[index].tag !== tag) {
123
- return;
124
- }
125
- this.add(change.ref, tag, false);
126
- });
127
- return this;
132
+ return isChildAdded;
128
133
  }
129
134
  addParentOf(childChangeTree, tag) {
130
135
  const changeTree = childChangeTree.parent[symbols_1.$changes];
@@ -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,C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rC,IAAI,CAAC,iCAAiC,CAAC,eAAe,CAAC,CAAC;QAC5D,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AAnUD,8BAmUC","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 } else if (changes[changeTree.parentIndex] === OPERATION.ADD) {\n //\n // SAME PATCH ADD + REMOVE:\n // The 'changes' of deleted structure should be ignored.\n //\n this.changes.delete(changeTree.refId);\n }\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"]}
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;QAC/C,MAAM,gBAAgB,GAAG,UAAU,C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AC,KAAK,CAAC,CAAC,CAAC,EAAE,8BAAgB,EAAE,IAAI,CAAC,CAAC;QACvD,CAAC;QAED,oBAAoB;QACpB,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,mBAAmB,CAAC,UAAsB;QACtC,IAAI,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAE7C,EAAE;QACF,+EAA+E;QAC/E,gHAAgH;QAChH,EAAE;QACF,IAAI,CAAC,SAAS,IAAI,UAAU,CAAC,4BAA4B,EAAC,CAAC;YAEvD,2CAA2C;YAC3C,4CAA4C;YAC5C,+BAA+B;YAC/B,kDAAkD;YAClD,MAAM;YAEN,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,kBAAQ,CAAC,CAAC,EAAE,CAAC;gBAChD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;gBAC7B,SAAS,GAAG,IAAI,CAAC;YACrB,CAAC;QACL,CAAC;QAED,OAAO,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;AA3UD,8BA2UC","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 const parentChangeTree = changeTree.parent;\n\n if (!changeTree) {\n console.warn(\"StateView#add(), invalid object:\", obj);\n return false;\n\n } else if (\n !parentChangeTree &&\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 && parentChangeTree) {\n this.addParentOf(changeTree, tag);\n }\n\n let changes = this.changes.get(changeTree.refId);\n if (changes === undefined) {\n changes = {};\n // FIXME / OPTIMIZE: do not add if no changes are needed\n this.changes.set(changeTree.refId, changes);\n }\n\n let isChildAdded = false;\n\n //\n // Add children of this ChangeTree first.\n // If successful, we must link the current ChangeTree to the child.\n //\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\n if (this.add(change.ref, tag, false)) {\n isChildAdded = true;\n }\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 if (!changeTree.isNew || isChildAdded) {\n // new structures will be added as part of .encode() call, no need to force it to .encodeView()\n const changeSet = (changeTree.filteredChanges !== undefined)\n ? changeTree.allFilteredChanges\n : changeTree.allChanges;\n\n const isInvisible = this.invisible.has(changeTree);\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 op !== OPERATION.DELETE &&\n (\n isInvisible || // if \"invisible\", include all\n tagAtIndex === undefined || // \"all change\" with no tag\n tagAtIndex === tag // tagged property\n )\n ) {\n changes[index] = op;\n isChildAdded = true; // FIXME: assign only once\n }\n }\n }\n\n return isChildAdded;\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 } else if (changes[changeTree.parentIndex] === OPERATION.ADD) {\n //\n // SAME PATCH ADD + REMOVE:\n // The 'changes' of deleted structure should be ignored.\n //\n this.changes.delete(changeTree.refId);\n }\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"]}
@@ -102,7 +102,7 @@ class ArraySchema {
102
102
  }
103
103
  if (previousValue !== undefined) {
104
104
  // remove root reference from previous value
105
- previousValue[symbols_1.$changes].root?.remove(previousValue[symbols_1.$changes]);
105
+ previousValue[symbols_1.$changes].root?.remove(previousValue[symbols_1.$changes], obj);
106
106
  }
107
107
  }
108
108
  else {
@@ -259,7 +259,7 @@ class ArraySchema {
259
259
  const changeTree = this[symbols_1.$changes];
260
260
  // remove children references
261
261
  changeTree.forEachChild((childChangeTree, _) => {
262
- changeTree.root?.remove(childChangeTree);
262
+ changeTree.root?.remove(childChangeTree, this);
263
263
  });
264
264
  changeTree.discard(true);
265
265
  changeTree.operation(spec_1.OPERATION.CLEAR);