reptree 0.5.0 → 0.6.0
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.
- package/dist/index.cjs +18 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -10
- package/dist/index.d.ts +1 -10
- package/dist/index.js +18 -57
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/OpId.ts","../src/operations.ts","../src/VertexState.ts","../src/TreeState.ts","../src/uuid.ts","../src/reactive.ts","../src/Vertex.ts","../src/StateVector.ts","../src/RepTree.ts"],"sourcesContent":["export interface OpId {\n readonly counter: number;\n readonly peerId: string;\n}\n\nexport function createOpId(counter: number, peerId: string): OpId {\n return { counter, peerId };\n}\n\nexport function compareOpId(opIdA: OpId | string, opIdB: OpId | string): number {\n if (typeof opIdA === 'string') {\n const parsedA = tryParseOpIdStr(opIdA);\n if (!parsedA) throw new Error(`Invalid OpId string: ${opIdA}`);\n opIdA = parsedA;\n }\n if (typeof opIdB === 'string') {\n const parsedB = tryParseOpIdStr(opIdB);\n if (!parsedB) throw new Error(`Invalid OpId string: ${opIdB}`);\n opIdB = parsedB;\n }\n\n const counterA = opIdA.counter;\n const counterB = opIdB.counter;\n\n if (counterA > counterB) {\n return 1;\n } else if (counterA < counterB) {\n return -1;\n } else {\n return opIdA.peerId.localeCompare(opIdB.peerId);\n }\n}\n\nexport function equalsOpId(opIdA: OpId | string | null, opIdB: OpId | string | null): boolean {\n if (opIdA === opIdB) {\n return true;\n } else if (!opIdA || !opIdB) {\n return false;\n }\n\n return compareOpId(opIdA, opIdB) === 0;\n}\n\nexport function tryParseOpIdStr(opIdStr: string): OpId {\n const parts = opIdStr.split('@');\n\n if (parts.length !== 2) {\n throw new Error(`Invalid OpId string: ${opIdStr}`);\n }\n\n return createOpId(parseInt(parts[0], 10), parts[1]);\n}\n\nexport function isOpIdGreaterThan(opIdA: OpId | string, opIdB: OpId | string): boolean {\n return compareOpId(opIdA, opIdB) === 1;\n}\n\nexport function opIdToString(opId: OpId): string {\n return `${opId.counter}@${opId.peerId}`;\n}\n","import { type OpId, createOpId } from \"./OpId\";\nimport { type VertexPropertyType } from \"./treeTypes\";\n\nexport interface MoveVertex {\n id: OpId;\n targetId: string;\n parentId: string | null;\n}\n\nexport interface SetVertexProperty {\n id: OpId;\n targetId: string;\n key: string;\n value: VertexPropertyType;\n transient: boolean;\n}\n\nexport type VertexOperation = MoveVertex | SetVertexProperty;\n\nexport function isMoveVertexOp(op: VertexOperation): op is MoveVertex {\n return 'parentId' in op;\n}\n\nexport function isAnyPropertyOp(op: VertexOperation): op is SetVertexProperty {\n return 'key' in op;\n}\n\nexport function newMoveVertexOp(clock: number, peerId: string, targetId: string, parentId: string | null): MoveVertex {\n return { id: createOpId(clock, peerId), targetId, parentId };\n}\n\nexport function newSetVertexPropertyOp(clock: number, peerId: string, targetId: string, key: string, value: VertexPropertyType): SetVertexProperty {\n return { id: createOpId(clock, peerId), targetId, key, value, transient: false };\n}\n\nexport function newSetTransientVertexPropertyOp(clock: number, peerId: string, targetId: string, key: string, value: VertexPropertyType): SetVertexProperty {\n return { id: createOpId(clock, peerId), targetId, key, value, transient: true };\n}","import type { TreeVertexId, TreeVertexProperty, VertexPropertyType } from \"./treeTypes\";\n\nexport class VertexState {\n readonly id: string;\n parentId: TreeVertexId | null;\n private properties: TreeVertexProperty[];\n private transientProperties: TreeVertexProperty[];\n children: string[];\n\n constructor(id: string, parentId: TreeVertexId | null) {\n this.id = id;\n this.parentId = parentId;\n this.properties = [];\n this.transientProperties = [];\n this.children = [];\n }\n\n setProperty(key: string, value: any): void {\n const existingPropIndex = this.properties.findIndex(p => p.key === key);\n if (existingPropIndex !== -1) {\n if (value !== undefined) {\n // Update existing property\n this.properties[existingPropIndex] = { key, value };\n } else {\n // Remove existing property\n this.removeProperty(key);\n }\n } else {\n if (value !== undefined) {\n // Add new property\n this.properties.push({ key, value });\n }\n }\n }\n\n setTransientProperty(key: string, value: any): void {\n const existingPropIndex = this.transientProperties.findIndex(p => p.key === key);\n if (existingPropIndex !== -1) {\n if (value !== undefined) {\n // Update existing property\n this.transientProperties[existingPropIndex] = { key, value };\n } else {\n // Remove existing property\n this.removeTransientProperty(key);\n }\n } else {\n if (value !== undefined) {\n // Add new property\n this.transientProperties.push({ key, value });\n }\n }\n }\n\n getProperty(key: string, includingTransient: boolean = true): VertexPropertyType | undefined {\n if (includingTransient) {\n const transientProp = this.transientProperties.find(p => p.key === key);\n if (transientProp) {\n return transientProp.value;\n }\n }\n\n return this.properties.find(p => p.key === key)?.value;\n }\n\n getAllProperties(includingTransient: boolean = true): ReadonlyArray<TreeVertexProperty> {\n if (!includingTransient) {\n return this.properties;\n }\n\n const result: TreeVertexProperty[] = [];\n const seenKeys = new Set<string>();\n\n // Add transient properties first\n for (const prop of this.transientProperties) {\n result.push(prop);\n seenKeys.add(prop.key);\n }\n\n // Add permanent properties that don't have a transient override\n for (const prop of this.properties) {\n if (!seenKeys.has(prop.key)) {\n result.push(prop);\n }\n }\n\n return result;\n }\n\n removeProperty(key: string): void {\n this.properties = this.properties.filter(p => p.key !== key);\n }\n\n removeTransientProperty(key: string): void {\n this.transientProperties = this.transientProperties.filter(p => p.key !== key);\n }\n\n getTransientProperties(): ReadonlyArray<TreeVertexProperty> {\n return this.transientProperties;\n }\n\n clearAllTransientProperties(): void {\n this.transientProperties = [];\n }\n}\n","import type { TreeVertexId, VertexChangeEvent, VertexPropertyChangeEvent, VertexChildrenChangeEvent, VertexMoveEvent } from \"./treeTypes\";\nimport { VertexState } from \"./VertexState\";\n\nexport class TreeState {\n private vertices: Map<TreeVertexId, VertexState>;\n private changeCallbacks: Map<TreeVertexId, Set<(events: VertexChangeEvent[]) => void>> = new Map();\n private globalChangeCallbacks: Set<(events: VertexChangeEvent[]) => void> = new Set();\n\n private batchTickInterval: NodeJS.Timeout;\n private batchedEvents: Map<TreeVertexId, VertexChangeEvent[]> = new Map();\n\n constructor() {\n this.vertices = new Map();\n\n this.batchTickInterval = setInterval(() => {\n this.processBatchedEvents();\n }, 33.3);\n }\n\n dispose() {\n clearInterval(this.batchTickInterval);\n }\n\n private processBatchedEvents() {\n for (const [vertexId, events] of this.batchedEvents) {\n // Get last property events per key and last move/children events\n let lastMoveEvent: VertexMoveEvent | null = null;\n let lastChildrenEvent: VertexChildrenChangeEvent | null = null;\n const propertyEventsByKey = new Map<string, VertexPropertyChangeEvent>();\n\n for (let i = events.length - 1; i >= 0; i--) {\n const event = events[i];\n if (!lastMoveEvent && event.type === 'move') lastMoveEvent = event as VertexMoveEvent;\n if (!lastChildrenEvent && event.type === 'children') lastChildrenEvent = event as VertexChildrenChangeEvent;\n if (event.type === 'property') {\n const propertyEvent = event as VertexPropertyChangeEvent;\n if (!propertyEventsByKey.has(propertyEvent.key)) {\n propertyEventsByKey.set(propertyEvent.key, propertyEvent);\n }\n }\n }\n\n // Combine all events with move and children events first\n const filteredEvents = [\n ...(lastMoveEvent ? [lastMoveEvent] : []),\n ...(lastChildrenEvent ? [lastChildrenEvent] : []),\n ...propertyEventsByKey.values()\n ];\n\n this.globalChangeCallbacks.forEach(listener => listener(filteredEvents));\n this.changeCallbacks.get(vertexId)?.forEach(listener => listener(filteredEvents));\n }\n\n this.batchedEvents.clear();\n }\n\n getAllVertices(): ReadonlyArray<VertexState> {\n return Array.from(this.vertices.values());\n }\n\n getVertex(id: string): VertexState | undefined {\n return this.vertices.get(id);\n }\n\n getChildrenIds(vertexId: TreeVertexId): string[] {\n return this.getVertex(vertexId)?.children ?? [];\n }\n\n getChildren(vertexId: TreeVertexId): VertexState[] {\n return this.getChildrenIds(vertexId)\n .map(id => {\n const vertex = this.vertices.get(id);\n return vertex ? vertex : undefined;\n })\n .filter(vertex => vertex !== undefined)\n .sort((a, b) => {\n const aDate = a.getProperty('_c') as string;\n const bDate = b.getProperty('_c') as string;\n if (!aDate) return -1;\n if (!bDate) return 1;\n return new Date(aDate).getTime() - new Date(bDate).getTime();\n }) as VertexState[];\n }\n\n moveVertex(vertexId: TreeVertexId, newParentId: TreeVertexId | null): VertexState {\n let vertex = this.getVertex(vertexId);\n // Undefined if the vertex is new\n const prevParentId = vertex ? vertex.parentId : undefined;\n if (!vertex) {\n vertex = new VertexState(vertexId, newParentId);\n this.vertices.set(vertexId, vertex);\n }\n\n if (prevParentId === newParentId) {\n return vertex;\n }\n\n vertex.parentId = newParentId;\n\n let childrenInNewParent: string[] | null = null;\n let childrenInOldParent: string[] | null = null;\n\n // Update children arrays in vertices\n if (prevParentId) {\n const oldParentVertex = this.getVertex(prevParentId);\n if (oldParentVertex) {\n oldParentVertex.children = oldParentVertex.children.filter(child => child !== vertexId);\n childrenInOldParent = oldParentVertex.children;\n } else {\n console.error(`Old parent vertex not found for ${prevParentId}`);\n }\n }\n\n if (newParentId !== null) {\n const newParentVertex = this.vertices.get(newParentId);\n if (newParentVertex) {\n newParentVertex.children.push(vertexId);\n childrenInNewParent = newParentVertex.children;\n } else {\n console.error(`New parent vertex not found for ${newParentId}`);\n }\n }\n\n // We notify the listeners in the end so that they have the final state of the tree\n\n this.notifyChange({\n type: 'move',\n vertexId: vertexId,\n oldParentId: prevParentId,\n newParentId,\n } as VertexMoveEvent);\n\n if (childrenInNewParent !== null && newParentId !== null) {\n this.notifyChange({\n type: 'children',\n vertexId: newParentId,\n children: childrenInNewParent.map(id => this.vertices.get(id)!),\n } as VertexChildrenChangeEvent);\n }\n\n if (childrenInOldParent !== null && prevParentId) {\n this.notifyChange({\n type: 'children',\n vertexId: prevParentId,\n children: childrenInOldParent.map(id => this.vertices.get(id)!),\n } as VertexChildrenChangeEvent);\n }\n\n return vertex;\n }\n\n setProperty(vertexId: string, key: string, value: any) {\n const vertex = this.getVertex(vertexId);\n if (!vertex) {\n throw new Error(`Vertex ${vertexId} not found`);\n }\n\n vertex.setProperty(key, value);\n\n this.notifyChange({\n type: 'property',\n vertexId: vertexId,\n key,\n value,\n } as VertexPropertyChangeEvent);\n\n if (vertex.parentId !== null) {\n this.notifyChange({\n type: 'children',\n vertexId: vertex.parentId,\n children: [vertex], // @TODO: shoulld I set all children or rename this property?\n } as VertexChildrenChangeEvent);\n }\n }\n\n setTransientProperty(vertexId: string, key: string, value: any) {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n vertex.setTransientProperty(key, value);\n }\n\n // @TODO: add info that it's a transient property\n this.notifyChange({\n type: 'property',\n vertexId: vertexId,\n key,\n value,\n } as VertexPropertyChangeEvent);\n }\n\n addChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void) {\n if (!this.changeCallbacks.has(vertexId)) {\n this.changeCallbacks.set(vertexId, new Set());\n }\n this.changeCallbacks.get(vertexId)!.add(listener);\n }\n\n removeChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void) {\n this.changeCallbacks.get(vertexId)?.delete(listener);\n }\n\n addGlobalChangeCallback(listener: (events: VertexChangeEvent[]) => void) {\n this.globalChangeCallbacks.add(listener);\n }\n\n removeGlobalChangeCallback(listener: (events: VertexChangeEvent[]) => void) {\n this.globalChangeCallbacks.delete(listener);\n }\n\n private notifyChange(event: VertexChangeEvent) {\n let events = this.batchedEvents.get(event.vertexId);\n if (!events) {\n events = [];\n this.batchedEvents.set(event.vertexId, events);\n }\n\n events.push(event);\n\n // @TODO: have immediate events\n //this.globalChangeCallbacks.forEach(listener => listener(event));\n //this.changeCallbacks.get(event.vertexId)?.forEach(listener => listener(event));\n }\n\n printTree(vertexId: TreeVertexId, indent: string = \"\", isLast: boolean = true): string {\n const prefix = indent + (isLast ? \"└── \" : \"├── \");\n let result = prefix + vertexId + \"\\n\";\n\n let vertexName: string | null = null;\n\n if (vertexId !== null) {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n for (const prop of vertex.getAllProperties()) {\n if (prop.key === \"_n\") {\n vertexName = prop.value as string;\n //continue;\n }\n\n const propPrefix = indent + (isLast ? \" \" : \"│ \") + \"• \";\n result += `${propPrefix}${prop.key}: ${JSON.stringify(prop.value)}\\n`;\n }\n }\n }\n\n // Get children and sort them for deterministic output\n const children = this.getChildrenIds(vertexId);\n const sortedChildren = [...children].sort((a, b) => {\n // Sort by name if available\n const vertexA = this.getVertex(a);\n const vertexB = this.getVertex(b);\n \n const nameA = vertexA?.getProperty('_n') as string | undefined;\n const nameB = vertexB?.getProperty('_n') as string | undefined;\n \n // If both have names, compare them\n if (nameA && nameB) {\n return nameA.localeCompare(nameB);\n }\n \n // If only one has a name, prioritize the one with a name\n if (nameA) return -1;\n if (nameB) return 1;\n \n // Fall back to sorting by ID for consistent output\n return a.localeCompare(b);\n });\n \n for (let i = 0; i < sortedChildren.length; i++) {\n const childId = sortedChildren[i];\n const isLastChild = i === sortedChildren.length - 1;\n result += this.printTree(childId, indent + (isLast ? \" \" : \"│ \"), isLastChild);\n }\n\n return result;\n }\n}","const removeDashes = (guid: string) => guid.replace(/-/g, '');\n\nexport default function uuid(): string {\n return removeDashes(crypto.randomUUID());\n}","import type { RepTree } from './RepTree';\nimport type { Vertex } from './Vertex';\n\nexport type FieldSchemaLike = {\n safeParse?: (input: unknown) => { success: true; data: unknown } | { success: false };\n};\n\nexport type SchemaLike<T> = {\n safeParse?: (input: unknown) => { success: true; data: T } | { success: false };\n parse?: (input: unknown) => T;\n shape?: Record<string, FieldSchemaLike>;\n};\n\nexport type AliasRule = {\n publicKey: string;\n internalKey: string;\n toPublic?: (value: unknown) => unknown;\n toInternal?: (value: unknown) => unknown;\n};\n\nexport const defaultAliases: AliasRule[] = [\n { publicKey: 'name', internalKey: '_n' },\n {\n publicKey: 'createdAt',\n internalKey: '_c',\n toPublic: (v: unknown) => (typeof v === 'string' ? new Date(v) : v),\n toInternal: (v: unknown) => (v instanceof Date ? v.toISOString() : v),\n },\n];\n\nexport type BindOptions<T> = {\n schema?: SchemaLike<T>;\n aliases?: AliasRule[];\n includeInternalKeys?: boolean;\n};\n\n/**\n * A bound vertex object that forwards reads/writes to a vertex.\n * @param T - The type of the vertex.\n */\nexport type BindedVertex<T> = T & {\n\n $vertex: Vertex;\n $id: string;\n $parentId: string | null;\n $parent: Vertex | undefined;\n $children: Vertex[];\n $childrenIds: string[];\n\n /**\n * Apply transient edits that override reads but do not persist yet.\n * @param fn \n */\n $useTransients(fn: (t: T) => void): void;\n \n /**\n * Promote current transient overlays to persistent values.\n */\n $commitTransients(): void;\n \n /**\n * Move the vertex to a new parent.\n * @param parent - The new parent vertex or ID.\n */\n $moveTo(parent: Vertex | BindedVertex<any> | string): void;\n \n /**\n * Delete the vertex.\n */\n $delete(): void;\n \n /**\n * Observe changes to the vertex.\n * @param listener - The listener function to call when changes occur.\n */\n $observe(listener: (events: any[]) => void): () => void;\n \n /**\n * Observe changes to the children of the vertex.\n * @param listener - The listener function to call when children change.\n */\n $observeChildren(listener: (children: Vertex[]) => void): () => void;\n \n /**\n * Create a new child vertex.\n * @param props - The properties to set on the new child vertex.\n */\n \n $newChild(props?: Record<string, any> | object | null): Vertex;\n \n /**\n * Create a new named child vertex.\n * @param name - The name of the new child vertex.\n * @param props - The properties to set on the new child vertex.\n */\n $newNamedChild(name: string, props?: Record<string, any> | object | null): Vertex;\n};\n\nfunction buildAliasMaps(aliases: AliasRule[]) {\n const publicToInternal = new Map<string, AliasRule>();\n const internalToPublic = new Map<string, AliasRule>();\n for (const rule of aliases) {\n publicToInternal.set(rule.publicKey, rule);\n internalToPublic.set(rule.internalKey, rule);\n }\n return { publicToInternal, internalToPublic };\n}\n\n/**\n * Returns a live Proxy that forwards reads/writes to a vertex.\n * - Reads reflect the latest CRDT state (including transients by default)\n * - Writes persist to the CRDT with optional schema validation\n */\nexport function bindVertex<T extends Record<string, unknown>>(\n tree: RepTree,\n id: string,\n schemaOrOptions?: SchemaLike<T> | BindOptions<T>\n): BindedVertex<T> {\n const isOptions =\n typeof schemaOrOptions === 'object' && schemaOrOptions !== null && (\n Object.prototype.hasOwnProperty.call(schemaOrOptions as object, 'aliases') ||\n Object.prototype.hasOwnProperty.call(schemaOrOptions as object, 'includeInternalKeys') ||\n Object.prototype.hasOwnProperty.call(schemaOrOptions as object, 'schema')\n );\n\n const options = (isOptions\n ? (schemaOrOptions as BindOptions<T>)\n : { schema: schemaOrOptions as SchemaLike<T> }) as BindOptions<T>;\n\n const schema = options.schema;\n const aliases = options.aliases ?? defaultAliases;\n const { publicToInternal } = buildAliasMaps(aliases);\n\n const obj: any = {};\n\n Object.defineProperties(obj, {\n $vertex: { get: () => tree.getVertex(id)!, enumerable: false, configurable: true },\n $id: { get: () => id, enumerable: false, configurable: true },\n $parentId: { get: () => tree.getVertex(id)?.parentId ?? null, enumerable: false, configurable: true },\n $parent: { get: () => tree.getVertex(id)?.parent, enumerable: false, configurable: true },\n $children: { get: () => tree.getChildren(id), enumerable: false, configurable: true },\n $childrenIds: { get: () => tree.getChildrenIds(id), enumerable: false, configurable: true },\n $moveTo: {\n value: (parent: any) => {\n const parentId = typeof parent === 'object' && parent !== null ? (parent.id || parent.$id) : parent;\n tree.moveVertex(id, parentId);\n },\n enumerable: false,\n configurable: true,\n writable: false,\n },\n $delete: { value: () => tree.deleteVertex(id), enumerable: false, configurable: true, writable: false },\n $observe: { value: (listener: (events: any[]) => void) => tree.observe(id, listener), enumerable: false, configurable: true, writable: false },\n $observeChildren: {\n value: (listener: (children: Vertex[]) => void) =>\n tree.observe(id, (events: any[]) => {\n if (events.some((e: any) => e.type === 'children')) {\n listener(tree.getChildren(id));\n }\n }),\n enumerable: false,\n configurable: true,\n writable: false,\n },\n $newChild: { value: (props?: Record<string, any> | object | null) => tree.getVertex(id)!.newChild(props), enumerable: false, configurable: true, writable: false },\n $newNamedChild: { value: (name: string, props?: Record<string, any> | object | null) => tree.getVertex(id)!.newNamedChild(name, props), enumerable: false, configurable: true, writable: false },\n $useTransients: {\n value: function (fn: (t: any) => void) {\n const transientProxy = new Proxy({} as any, {\n set(_, prop: string | symbol, value: unknown) {\n if (typeof prop === 'string') {\n const rule = publicToInternal.get(prop);\n const internalKey = rule?.internalKey ?? prop;\n const internalValue = rule?.toInternal ? rule.toInternal(value) : value;\n tree.setTransientVertexProperty(id, internalKey, internalValue as any);\n }\n return true;\n },\n get(_, prop: string | symbol) {\n if (typeof prop !== 'string') return undefined;\n const rule = publicToInternal.get(prop);\n const internalKey = rule?.internalKey ?? prop;\n const rawValue = tree.getVertexProperty(id, internalKey, true);\n return rule?.toPublic ? rule.toPublic(rawValue as unknown) : rawValue;\n },\n });\n fn(transientProxy);\n },\n enumerable: false,\n configurable: true,\n writable: false,\n },\n $commitTransients: { value: () => tree.commitTransients(id), enumerable: false, configurable: true, writable: false },\n equals: {\n value: function (other: any) {\n if (other && typeof other === 'object' && '$id' in other) {\n return other.$id === id;\n }\n return false;\n },\n enumerable: false,\n configurable: true,\n writable: false,\n },\n });\n\n const proxy = new Proxy(obj, {\n get(target, prop: string | symbol, receiver) {\n if (typeof prop !== 'string') {\n return Reflect.get(target, prop, receiver);\n }\n if (prop in target) {\n return Reflect.get(target, prop, receiver);\n }\n\n const rule = publicToInternal.get(prop);\n const internalKey = rule?.internalKey ?? prop;\n const rawValue = tree.getVertexProperty(id, internalKey, true);\n return rule?.toPublic ? rule.toPublic(rawValue as unknown) : rawValue;\n },\n\n set(target, prop: string | symbol, value: unknown) {\n if (typeof prop !== 'string') {\n return Reflect.set(target, prop, value);\n }\n\n if (schema?.shape && schema.shape[prop]) {\n const field = schema.shape[prop]!;\n if (field.safeParse) {\n const res = field.safeParse(value);\n if (!res.success) throw new Error(`Invalid value for ${prop}`);\n value = (res as any).data;\n }\n }\n\n const rule = publicToInternal.get(prop);\n const internalKey = rule?.internalKey ?? prop;\n const internalValue = rule?.toInternal ? rule.toInternal(value) : value;\n tree.setVertexProperty(id, internalKey, internalValue as any);\n return true;\n },\n\n deleteProperty(_target, prop: string | symbol) {\n if (typeof prop !== 'string') {\n return true;\n }\n const rule = publicToInternal.get(prop);\n const internalKey = rule?.internalKey ?? prop;\n tree.setVertexProperty(id, internalKey, undefined as any);\n return true;\n },\n });\n\n return proxy as BindedVertex<T>;\n}\n","import type { VertexState } from \"./VertexState\";\nimport type { RepTree } from \"./RepTree\";\nimport { bindVertex, type SchemaLike, type BindOptions, type BindedVertex } from './reactive';\nimport type { VertexChangeEvent, VertexPropertyType } from \"./treeTypes\";\n\n/**\n * A wrapper class for VertexState that provides a more convenient API\n * for working with vertices in a RepTree.\n */\nexport class Vertex {\n constructor(\n private tree: RepTree,\n private state: VertexState\n ) { }\n\n get id(): string {\n return this.state.id;\n }\n\n get name(): string | undefined {\n return this.getProperty('_n') as string | undefined;\n }\n\n set name(name: string) {\n this.tree.setVertexProperty(this.id, '_n', name);\n }\n\n get createdAt(): Date {\n const createdAt = this.getProperty('_c') as string;\n if (!createdAt) {\n return new Date(0);\n }\n return new Date(createdAt);\n }\n\n get path(): string {\n //return this.tree.getVertexPath(this.id);\n throw new Error('Not implemented');\n }\n\n get parentId(): string | null {\n return this.state.parentId;\n }\n\n get parent(): Vertex | undefined {\n if (!this.parentId) {\n return undefined;\n }\n\n return this.tree.getVertex(this.parentId);\n }\n\n get children(): Vertex[] {\n return this.tree.getChildren(this.id);\n }\n\n get childrenIds(): string[] {\n return this.tree.getChildrenIds(this.id);\n }\n\n getAsTypedObject<T>(): T {\n return this.getProperties() as T;\n }\n\n getChildrenAsTypedArray<T>(): T[] {\n return this.children.map(v => v.getAsTypedObject<T>());\n }\n\n newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex {\n // Forbid nested children in props (not supported yet)\n if (props && typeof props === 'object' && 'children' in (props as any)) {\n throw new Error('Passing children inside props is not supported at the moment');\n }\n\n const normalized = Vertex.normalizePropsForCreation(props);\n return this.tree.newVertex(this.id, normalized);\n }\n\n newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex {\n // Forbid nested children in props (not supported yet)\n if (props && typeof props === 'object' && 'children' in (props as any)) {\n throw new Error('Passing children inside props is not supported at the moment');\n }\n\n const normalized = Vertex.normalizePropsForCreation(props, name);\n return this.tree.newNamedVertex(this.id, name, normalized);\n }\n\n setProperty(key: string, value: VertexPropertyType): void {\n // First check if the property is already set (not including transient properties)\n const existingValue = this.getProperty(key, false);\n if (existingValue === value) {\n return;\n }\n\n this.tree.setVertexProperty(this.id, key, value);\n }\n\n setTransientProperty(key: string, value: VertexPropertyType): void {\n // First check if the property is already set\n const existingValue = this.getProperty(key);\n if (existingValue === value) {\n return;\n }\n\n this.tree.setTransientVertexProperty(this.id, key, value);\n }\n\n commitTransients(): void {\n this.tree.commitTransients(this.id);\n }\n\n setProperties(props: Record<string, VertexPropertyType> | object): void {\n for (const [key, value] of Object.entries(props)) {\n this.setProperty(key, value);\n }\n }\n\n getProperty(key: string, includingTransient: boolean = true): VertexPropertyType | undefined {\n return this.tree.getVertexProperty(this.id, key, includingTransient);\n }\n\n getProperties(): Record<string, VertexPropertyType> {\n const props: Record<string, VertexPropertyType> = {};\n this.tree.getVertexProperties(this.id).forEach(p => {\n props[p.key] = p.value;\n });\n return props;\n }\n\n findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[] {\n return this.children.filter(c => c.getProperty(key) === value);\n }\n\n findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined {\n return this.children.find(c => c.getProperty(key) === value);\n }\n\n findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined {\n return this.findFirstChildVertexWithProperty(key, value)?.getAsTypedObject<T>();\n }\n\n findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[] {\n return this.findAllChildrenWithProperty(key, value).map(c => c.getAsTypedObject<T>());\n }\n\n observe(listener: (events: VertexChangeEvent[]) => void): () => void {\n const unobserve = this.tree.observe(this.id, listener);\n return () => unobserve();\n }\n\n observeChildren(listener: (children: Vertex[]) => void): () => void {\n const unobserve = this.tree.observe(this.id, (events: VertexChangeEvent[]) => {\n if (events.some(e => e.type === 'children')) {\n listener(this.children);\n }\n });\n return () => unobserve();\n }\n\n observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void {\n return this.observeChildren((children) => {\n listener(children.map(c => c.getProperties() as unknown as T));\n });\n }\n\n delete(): void {\n this.tree.deleteVertex(this.id);\n }\n\n moveTo(parent: Vertex): void {\n this.tree.moveVertex(this.id, parent.id);\n }\n\n /** Returns a live reactive object bound to this vertex. Accepts schema or options. */\n bind<T extends Record<string, unknown>>(schemaOrOptions?: SchemaLike<T> | BindOptions<T>): BindedVertex<T> {\n return bindVertex<T>(this.tree, this.id, schemaOrOptions as any);\n }\n\n /**\n * Normalizes an input props object for vertex creation:\n * - Aliases name -> _n, createdAt -> _c (Date -> ISO string)\n * - Filters unsupported field types with a console warning\n * - When a name param is provided to newNamedChild, ignores conflicting name in props\n */\n private static normalizePropsForCreation(props?: Record<string, VertexPropertyType> | object | null, explicitName?: string): Record<string, VertexPropertyType> | null {\n if (!props) return null;\n const input = props as Record<string, any>;\n const out: Record<string, VertexPropertyType> = {};\n const skipped: string[] = [];\n\n for (const [rawKey, rawValue] of Object.entries(input)) {\n if (rawValue === undefined) {\n // Skip undefined to avoid writing explicit undefineds on creation\n continue;\n }\n\n // Disallow nested children handled earlier; skip here defensively\n if (rawKey === 'children') continue;\n\n // Alias mapping\n let key = rawKey;\n if (rawKey === 'name') {\n if (explicitName !== undefined) {\n // Explicit argument takes precedence\n console.warn('newNamedChild: \"name\" in props is ignored because a name argument was provided');\n continue;\n }\n key = '_n';\n } else if (rawKey === 'createdAt') {\n key = '_c';\n }\n\n // Value normalization\n let value: any = rawValue;\n if (key === '_c') {\n if (value instanceof Date) {\n value = value.toISOString();\n } else if (typeof value === 'string') {\n // leave as is (assumed ISO)\n } else {\n skipped.push(rawKey);\n continue;\n }\n }\n\n const isPrimitive = (v: any) => typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean';\n\n if (Array.isArray(value)) {\n // Ensure array of primitives\n if (!value.every(isPrimitive)) {\n skipped.push(rawKey);\n continue;\n }\n } else if (typeof value === 'object' && value !== null) {\n // Unsupported nested object\n skipped.push(rawKey);\n continue;\n } else if (!isPrimitive(value)) {\n skipped.push(rawKey);\n continue;\n }\n\n out[key] = value as VertexPropertyType;\n }\n\n if (skipped.length > 0) {\n console.warn(`Some fields were skipped due to unsupported types: ${skipped.join(', ')}`);\n }\n\n return Object.keys(out).length > 0 ? out : null;\n }\n} ","import { OpId } from \"./OpId\";\nimport type { OpIdRange } from \"./treeTypes\";\nimport type { VertexOperation } from \"./operations\";\n\n/**\n * Helper function to subtract one set of ranges from another.\n * Returns the ranges in A that are not in B.\n * Assumes ranges in both A and B are sorted and non-overlapping.\n */\nexport function subtractRanges(rangesA: number[][], rangesB: number[][]): number[][] {\n if (rangesB.length === 0) return rangesA.map(r => [...r]); // Return a copy\n if (rangesA.length === 0) return []; // If A is empty, nothing to subtract\n\n const result: number[][] = [];\n let indexB = 0;\n\n for (const rangeA of rangesA) {\n let currentStart = rangeA[0];\n const endA = rangeA[1];\n\n // Iterate through ranges in B that could potentially overlap with rangeA\n while (indexB < rangesB.length && rangesB[indexB][1] < currentStart) {\n // Skip ranges in B that are entirely before the current start\n indexB++;\n }\n\n while (indexB < rangesB.length && rangesB[indexB][0] <= endA) {\n const startB = rangesB[indexB][0];\n const endB = rangesB[indexB][1];\n\n // If there's a gap before this range in B starts\n if (currentStart < startB) {\n // Add the portion of rangeA before the overlap\n result.push([currentStart, Math.min(endA, startB - 1)]);\n }\n\n // Move current start past this range in B\n currentStart = Math.max(currentStart, endB + 1);\n\n // If we've gone past the end of rangeA, break inner loop\n if (currentStart > endA) break;\n\n // If the current rangeB ends after rangeA, we don't need to check further ranges in B for this rangeA\n if (endB >= endA) break;\n\n // Only advance indexB if the current rangeB is fully processed relative to currentStart\n if (endB < currentStart) {\n indexB++;\n } else if (startB >= currentStart) {\n indexB++;\n }\n }\n\n // If there's a remaining part of rangeA after processing overlaps with B\n if (currentStart <= endA) {\n result.push([currentStart, endA]);\n }\n }\n\n return result;\n}\n\n/**\n * StateVector tracks operations that have been applied using a range-based representation.\n * It's used for synchronization between peers to determine which operations need to be sent.\n */\nexport class StateVector {\n private ranges: Record<string, number[][]> = {};\n\n /**\n * Creates a new StateVector.\n * @param initialState Optional initial state to copy from\n */\n constructor(initialState: Record<string, number[][]> = {}) {\n // Create a deep copy of the initial state\n for (const [peerId, peerRanges] of Object.entries(initialState)) {\n this.ranges[peerId] = peerRanges.map(range => [...range]);\n }\n }\n\n /**\n * Updates the state vector with a newly applied operation.\n * Assumes ranges are sorted and non-overlapping.\n * \n * @param peerId The peer ID of the operation\n * @param counter The counter value of the operation\n */\n update(peerId: string, counter: number): void {\n // Initialize ranges array for this peer if it doesn't exist\n if (!this.ranges[peerId]) {\n this.ranges[peerId] = [];\n }\n\n const ranges = this.ranges[peerId];\n\n // Case 1: No ranges yet\n if (ranges.length === 0) {\n ranges.push([counter, counter]);\n return;\n }\n\n let rangeExtendedOrMerged = false;\n let insertIndex = -1;\n\n for (let i = 0; i < ranges.length; i++) {\n const range = ranges[i];\n\n // If counter is already in a range, do nothing\n if (counter >= range[0] && counter <= range[1]) {\n rangeExtendedOrMerged = true;\n break;\n }\n\n // If counter is one less than range start, extend range start\n if (counter === range[0] - 1) {\n range[0] = counter;\n rangeExtendedOrMerged = true;\n // Check if this range now merges with the previous range\n if (i > 0 && range[0] === ranges[i - 1][1] + 1) {\n ranges[i - 1][1] = range[1]; // Merge into previous\n ranges.splice(i, 1); // Remove current\n }\n break;\n }\n\n // If counter is one more than range end, extend range end\n if (counter === range[1] + 1) {\n range[1] = counter;\n rangeExtendedOrMerged = true;\n // Check if this range now merges with the next range\n if (i < ranges.length - 1 && range[1] + 1 === ranges[i + 1][0]) {\n range[1] = ranges[i + 1][1]; // Merge next into current\n ranges.splice(i + 1, 1); // Remove next\n }\n break;\n }\n\n // Keep track of where to insert if no extension/merge happens\n if (counter < range[0] && insertIndex === -1) {\n insertIndex = i;\n }\n }\n\n // If we couldn't extend or merge any range, add a new one\n if (!rangeExtendedOrMerged) {\n if (insertIndex === -1) {\n // If counter is greater than all existing ranges, add to the end\n insertIndex = ranges.length;\n }\n ranges.splice(insertIndex, 0, [counter, counter]);\n // After inserting, check if the new range merges with neighbors\n // Merge with previous range if possible\n if (insertIndex > 0 && ranges[insertIndex][0] === ranges[insertIndex - 1][1] + 1) {\n ranges[insertIndex - 1][1] = ranges[insertIndex][1];\n ranges.splice(insertIndex, 1);\n insertIndex--; // Adjust index after merging\n }\n // Merge with next range if possible (use adjusted insertIndex)\n if (insertIndex < ranges.length - 1 && ranges[insertIndex][1] + 1 === ranges[insertIndex + 1][0]) {\n ranges[insertIndex][1] = ranges[insertIndex + 1][1];\n ranges.splice(insertIndex + 1, 1);\n }\n }\n }\n\n /**\n * Updates the state vector with a newly applied operation.\n * \n * @param op The operation that was just applied\n */\n updateFromOp(op: VertexOperation): void {\n this.update(op.id.peerId, op.id.counter);\n }\n\n /**\n * Returns the current state vector.\n * Returns a readonly reference to the internal state.\n */\n getState(): Readonly<Record<string, number[][]>> {\n return this.ranges;\n }\n\n /**\n * Calculates which operation ranges we have that the other state vector is missing\n * by comparing state vectors.\n * \n * @param other The other state vector to compare against\n * @returns Array of operation ID ranges that we have but they don't\n */\n diff(other: StateVector): OpIdRange[] {\n const missingRanges: OpIdRange[] = [];\n const theirState = other.getState();\n\n // Check what we have that they don't have\n for (const [peerId, ourRanges] of Object.entries(this.ranges)) {\n const theirRanges = theirState[peerId] || [];\n\n // Calculate ranges we have that they don't\n const missing = subtractRanges(ourRanges, theirRanges);\n\n // Convert to OpIdRange format\n for (const [start, end] of missing) {\n // Ensure the range is valid (start <= end)\n if (start <= end) {\n missingRanges.push({\n peerId,\n start,\n end\n });\n }\n }\n }\n\n return missingRanges;\n }\n\n /**\n * Checks if the state vector contains the given operation ID\n * \n * @param opId The operation ID to check\n * @returns true if the operation is in the state vector, false otherwise\n */\n contains(opId: OpId): boolean {\n const peerId = opId.peerId;\n const counter = opId.counter;\n\n if (!this.ranges[peerId]) {\n return false;\n }\n\n for (const [start, end] of this.ranges[peerId]) {\n if (counter >= start && counter <= end) {\n return true;\n }\n }\n\n return false;\n }\n\n /**\n * Creates a copy of this state vector\n */\n clone(): StateVector {\n return new StateVector(this.ranges);\n }\n\n /**\n * Builds a state vector from an array of operations\n * @param operations The operations to build the state vector from\n * @returns A new StateVector instance\n */\n static fromOperations(operations: ReadonlyArray<VertexOperation>): StateVector {\n const stateVector = new StateVector();\n for (const op of operations) {\n stateVector.updateFromOp(op);\n }\n return stateVector;\n }\n} ","import {\n newMoveVertexOp,\n type MoveVertex,\n type SetVertexProperty,\n isMoveVertexOp,\n type VertexOperation,\n newSetVertexPropertyOp,\n newSetTransientVertexPropertyOp,\n isAnyPropertyOp\n} from \"./operations\";\nimport type { VertexPropertyType, TreeVertexProperty, VertexChangeEvent, TreeVertexId, VertexMoveEvent } from \"./treeTypes\";\nimport { VertexState } from \"./VertexState\";\nimport { TreeState } from \"./TreeState\";\nimport { type OpId, compareOpId, equalsOpId, isOpIdGreaterThan, opIdToString } from \"./OpId\";\nimport uuid from \"./uuid\";\nimport { Vertex } from './Vertex';\nimport { StateVector } from './StateVector';\n\ntype PropertyKeyAtVertexId = `${string}@${TreeVertexId}`;\n\n/**\n * RepTree is a tree data structure for storing vertices with properties.\n * It uses 2 conflict-free replicated data types (CRDTs) to manage seamless replication between peers.\n * A move tree CRDT is used for the tree structure (https://martin.kleppmann.com/papers/move-op.pdf).\n * A last writer wins (LWW) CRDT is used for properties.\n */\nexport class RepTree {\n private static NULL_VERTEX_ID = '0';\n\n readonly peerId: string;\n private rootVertexId: string | undefined;\n\n private lamportClock = 0;\n private state: TreeState;\n private moveOps: MoveVertex[] = [];\n private setPropertyOps: SetVertexProperty[] = [];\n private propertiesAndTheirOpIds: Map<PropertyKeyAtVertexId, OpId> = new Map();\n private transientPropertiesAndTheirOpIds: Map<PropertyKeyAtVertexId, OpId> = new Map();\n // Observers for non-structural properties are not used\n private localOps: VertexOperation[] = [];\n private pendingMovesWithMissingParent: Map<string, MoveVertex[]> = new Map();\n private pendingPropertiesWithMissingVertex: Map<string, SetVertexProperty[]> = new Map();\n private knownOps: Set<string> = new Set();\n private parentIdBeforeMove: Map<OpId, string | null | undefined> = new Map();\n private opAppliedCallbacks: ((op: VertexOperation) => void)[] = [];\n\n // State vector tracking operations from each peer\n private stateVector: StateVector;\n private _stateVectorEnabled: boolean = true;\n\n /**\n * @param peerId - The peer ID of the current client. Should be unique across all peers.\n * @param ops - The operations to replicate an existing tree, if not provided - an empty tree will be created without a root vertex\n */\n constructor(peerId: string, ops?: ReadonlyArray<VertexOperation>) {\n this.peerId = peerId;\n this.state = new TreeState();\n\n // Initialize state vector (enabled by default)\n this.stateVector = new StateVector();\n\n if (ops && ops.length > 0) {\n this.applyOps(ops);\n\n const root = this.root;\n if (!root) {\n throw new Error('There has to be a root vertex in the operations');\n }\n\n // @TODO: validate the tree structure, throw an exception if it's invalid\n }\n // @TODO: remove it? It creates an extra null vertex op in every new empty tree. Or is it ok?\n else {\n this.ensureNullVertex();\n }\n }\n\n get root(): Vertex | undefined {\n if (!this.rootVertexId) {\n const vertices = this.state.getAllVertices();\n for (const vertex of vertices) {\n if (vertex.parentId === null && vertex.id !== RepTree.NULL_VERTEX_ID) {\n this.rootVertexId = vertex.id;\n return new Vertex(this, vertex);\n }\n }\n\n return undefined;\n }\n\n const rootVertex = this.state.getVertex(this.rootVertexId);\n if (!rootVertex) {\n throw new Error(\"Root vertex not found\");\n }\n\n return new Vertex(this, rootVertex);\n }\n\n replicate(newPeerId: string): RepTree {\n return new RepTree(newPeerId, this.getAllOps());\n }\n\n getMoveOps(): ReadonlyArray<MoveVertex> {\n return this.moveOps;\n }\n\n getAllOps(): ReadonlyArray<VertexOperation> {\n return [...this.moveOps, ...this.setPropertyOps];\n }\n\n getVertex(vertexId: string): Vertex | undefined {\n const vertex = this.state.getVertex(vertexId);\n return vertex ? new Vertex(this, vertex) : undefined;\n }\n\n getAllVertices(): ReadonlyArray<Vertex> {\n return this.state.getAllVertices().map(v => new Vertex(this, v));\n }\n\n getParent(vertexId: string): Vertex | undefined {\n const parentId = this.state.getVertex(vertexId)?.parentId;\n const parent = parentId ? this.state.getVertex(parentId) : undefined;\n return parent ? new Vertex(this, parent) : undefined;\n }\n\n getChildren(vertexId: string): Vertex[] {\n return this.state.getChildren(vertexId).map(v => new Vertex(this, v));\n }\n\n getChildrenIds(vertexId: string): string[] {\n return this.state.getChildrenIds(vertexId);\n }\n\n getAncestors(vertexId: string): Vertex[] {\n const ancestors: Vertex[] = [];\n let currentVertex = this.state.getVertex(vertexId);\n\n while (currentVertex && currentVertex.parentId) {\n const parentVertex = this.state.getVertex(currentVertex.parentId);\n if (parentVertex) {\n ancestors.push(new Vertex(this, parentVertex));\n currentVertex = parentVertex;\n } else {\n break;\n }\n }\n\n return ancestors;\n }\n\n getVertexProperty(vertexId: string, key: string, includingTransient: boolean = true): VertexPropertyType | undefined {\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n return undefined;\n }\n\n return vertex.getProperty(key, includingTransient);\n }\n\n getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]> {\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n return [];\n }\n\n return vertex.getAllProperties();\n }\n\n /**\n * Returns all local operations and clears the local operations list.\n * Can be used to get all operations that were generated from this peer and need to be sent to other peers.\n */\n popLocalOps(): VertexOperation[] {\n const ops = this.localOps;\n this.localOps = [];\n return ops;\n }\n\n /**\n * This is the first vertex that will contain all other vertices.\n * If you plan to replicate a tree then don't use this method and instead merge\n * in the ops from another tree (that will also contain the root vertex).\n * @returns The root vertex\n */\n createRoot(): Vertex {\n if (this.rootVertexId) {\n throw new Error('Root vertex already exists');\n }\n\n this.rootVertexId = this.newVertexInternalWithUUID(null);\n\n const rootVertex = this.state.getVertex(this.rootVertexId);\n if (!rootVertex) {\n throw new Error(\"Root vertex not found\");\n }\n\n return new Vertex(this, rootVertex);\n }\n\n newVertex(parentId: string, props: Record<string, VertexPropertyType> | object | null = null): Vertex {\n const typedProps = props as Record<string, VertexPropertyType> | null;\n const vertexId = this.newVertexInternalWithUUID(parentId);\n if (typedProps) {\n this.setVertexProperties(vertexId, typedProps);\n }\n\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n throw new Error('Failed to create vertex');\n }\n return new Vertex(this, vertex);\n }\n\n newNamedVertex(parentId: string, name: string, props: Record<string, VertexPropertyType> | object | null = null): Vertex {\n const typedProps = props as Record<string, VertexPropertyType> | null;\n const vertexId = this.newVertexInternalWithUUID(parentId);\n if (typedProps) {\n this.setVertexProperties(vertexId, typedProps);\n }\n this.setVertexProperty(vertexId, '_n', name);\n\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n throw new Error('Failed to create named vertex');\n }\n return new Vertex(this, vertex);\n }\n\n moveVertex(vertexId: string, parentId: string) {\n this.lamportClock++;\n const op = newMoveVertexOp(this.lamportClock, this.peerId, vertexId, parentId);\n this.localOps.push(op);\n this.applyMove(op);\n }\n\n deleteVertex(vertexId: string) {\n this.moveVertex(vertexId, RepTree.NULL_VERTEX_ID);\n }\n\n setTransientVertexProperty(vertexId: string, key: string, value: VertexPropertyType) {\n this.lamportClock++;\n const op = newSetTransientVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value as VertexPropertyType);\n this.localOps.push(op);\n this.applyProperty(op);\n }\n\n /**\n * Promotes all transient (temporary) properties to persistent properties.\n * @param vertexId - The ID of the vertex to commit transients for.\n * @returns \n */\n commitTransients(vertexId: string) {\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n return;\n }\n\n const transientProps = vertex.getTransientProperties();\n \n // Promote each transient property to persistent\n for (const prop of transientProps) {\n this.setVertexProperty(vertexId, prop.key, prop.value);\n }\n\n // Clear transient OpIds tracking\n for (const prop of transientProps) {\n this.transientPropertiesAndTheirOpIds.delete(`${prop.key}@${vertexId}`);\n }\n\n // Clear all transient properties from the vertex\n vertex.clearAllTransientProperties();\n }\n\n setVertexProperty(vertexId: string, key: string, value: VertexPropertyType) {\n this.lamportClock++;\n const op = newSetVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value as VertexPropertyType);\n this.localOps.push(op);\n this.applyProperty(op);\n }\n\n setVertexProperties(vertexId: string, props: Record<string, VertexPropertyType> | object) {\n const typedProps = props as Record<string, VertexPropertyType>;\n for (const [key, value] of Object.entries(typedProps)) {\n this.setVertexProperty(vertexId, key, value);\n }\n }\n\n getVertexByPath(path: string): Vertex | undefined {\n // Let's remove '/' at the start and at the end of the path\n path = path.replace(/^\\/+/, '');\n path = path.replace(/\\/+$/, '');\n\n const pathParts = path.split('/');\n\n if (!this.rootVertexId) {\n return undefined;\n }\n\n const root = this.state.getVertex(this.rootVertexId);\n if (!root) {\n throw new Error('The root vertex is not found');\n }\n\n const vertex = this.getVertexByPathArray(new Vertex(this, root), pathParts);\n return vertex;\n }\n\n private getVertexByPathArray(vertex: Vertex, path: string[]): Vertex | undefined {\n if (path.length === 0) {\n return vertex ?? undefined;\n }\n\n const targetName = path[0];\n // Now, search recursively by name '_n' in children until the path is empty or not found.\n const children = this.getChildren(vertex.id);\n for (const child of children) {\n if (child.getProperty('_n') === targetName) {\n return this.getVertexByPathArray(child, path.slice(1));\n }\n }\n\n return undefined;\n }\n\n printTree() {\n if (!this.rootVertexId) {\n return '';\n }\n\n return this.state.printTree(this.rootVertexId);\n }\n\n merge(ops: ReadonlyArray<VertexOperation>) {\n /*\n if (ops.length > 100) {\n this.applyOpsOptimizedForLotsOfMoves(ops);\n } else {\n this.applyOps(ops);\n }\n */\n\n this.applyOps(ops);\n }\n\n private applyOps(ops: ReadonlyArray<VertexOperation>) {\n for (const op of ops) {\n // We skip the operation if we already know about it.\n // This is to avoid processing the same operation multiple times.\n if (this.knownOps.has(opIdToString(op.id))) {\n continue;\n }\n\n this.applyOperation(op);\n }\n }\n\n /** Applies operations in an optimized way, sorting move ops by OpId to avoid undo-do-redo cycles */\n private applyOpsOptimizedForLotsOfMoves(ops: ReadonlyArray<VertexOperation>) {\n const newMoveOps = ops.filter(op => isMoveVertexOp(op) && !this.knownOps.has(opIdToString(op.id)));\n if (newMoveOps.length > 0) {\n // Get an array of all move ops (without already applied ones)\n const allMoveOps = [...this.moveOps, ...newMoveOps] as MoveVertex[];\n // The main point of this optimization is to apply the moves without undo-do-redo cycles (the conflict resolution algorithm).\n // That is why we sort by OpId.\n allMoveOps.sort((a, b) => compareOpId(a.id, b.id));\n for (let i = 0, len = allMoveOps.length; i < len; i++) {\n const op = allMoveOps[i];\n this.applyMove(op);\n }\n }\n\n // Get an array of all property ops (without already applied ones)\n const propertyOps = ops.filter(op => isAnyPropertyOp(op) && !this.knownOps.has(opIdToString(op.id)));\n for (let i = 0, len = propertyOps.length; i < len; i++) {\n const op = propertyOps[i];\n this.applyProperty(op as SetVertexProperty);\n }\n }\n\n compareStructure(other: RepTree): boolean {\n if (this.root?.id !== other.root?.id) {\n return false;\n }\n\n if (!this.rootVertexId) {\n return true;\n }\n\n return RepTree.compareVertices(this.rootVertexId, this, other);\n }\n\n compareMoveOps(other: RepTree): boolean {\n const movesA = this.moveOps;\n const movesB = other.getMoveOps();\n\n if (movesA.length !== movesB.length) {\n return false;\n }\n\n for (let i = 0; i < movesA.length; i++) {\n if (!equalsOpId(movesA[i].id, movesB[i].id)) {\n return false;\n }\n }\n\n return true;\n }\n\n /** Checks if the given `ancestorId` is an ancestor of `childId` in the tree */\n isAncestor(childId: string, ancestorId: string | null): boolean {\n let targetId = childId;\n let vertex: VertexState | undefined;\n \n // Set to track visited vertices and detect cycles\n const visitedVertices = new Set<string>();\n\n while (vertex = this.state.getVertex(targetId)) {\n if (vertex.parentId === ancestorId) return true;\n if (!vertex.parentId) return false;\n \n // If we've already visited this vertex, we have a cycle\n if (visitedVertices.has(targetId)) {\n console.error(`isAncestor: cycle detected in the tree structure.`);\n // In the context of tryToMove, we should return false here to prevent the move\n // since the target ancestor isn't actually in the path (we're in a cycle)\n return false;\n }\n \n // Mark this vertex as visited\n visitedVertices.add(targetId);\n \n targetId = vertex.parentId;\n }\n\n return false;\n }\n\n observeVertex(vertexId: string, callback: (updatedVertex: Vertex) => void): () => void {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n callback(vertex);\n }\n\n const unsubscribe = this.observe(vertexId, (_) => {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n callback(vertex);\n }\n });\n\n return () => {\n unsubscribe();\n };\n }\n\n observeVertexMove(callback: (movedVertex: Vertex, isNew: boolean) => void): () => void {\n const listener = (events: VertexChangeEvent[]) => {\n const moveEvent = events.find(e => e.type === 'move') as VertexMoveEvent | undefined;\n if (moveEvent) {\n const vertex = this.getVertex(moveEvent.vertexId);\n if (vertex) {\n callback(vertex, moveEvent.oldParentId === undefined);\n }\n }\n };\n\n this.state.addGlobalChangeCallback(listener);\n\n return () => this.state.removeGlobalChangeCallback(listener);\n }\n\n observe(vertexId: string, callback: (events: VertexChangeEvent[]) => void): () => void {\n this.state.addChangeCallback(vertexId, callback);\n return () => this.state.removeChangeCallback(vertexId, callback);\n }\n\n observeOpApplied(callback: (op: VertexOperation) => void): () => void {\n this.opAppliedCallbacks.push(callback);\n return () => this.opAppliedCallbacks = this.opAppliedCallbacks.filter(l => l !== callback);\n }\n\n static compareVertices(vertexId: string, treeA: RepTree, treeB: RepTree): boolean {\n const childrenA = treeA.state.getChildrenIds(vertexId);\n const childrenB = treeB.state.getChildrenIds(vertexId);\n\n if (childrenA.length !== childrenB.length) {\n return false;\n }\n\n // Compare properties of the current vertex\n if (vertexId !== null) {\n const propertiesA = treeA.getVertexProperties(vertexId);\n const propertiesB = treeB.getVertexProperties(vertexId);\n\n if (propertiesA.length !== propertiesB.length) {\n return false;\n }\n\n for (const propA of propertiesA) {\n const propB = propertiesB.find(p => p.key === propA.key);\n if (!propB) {\n return false;\n }\n \n if (propA.value !== propB.value) {\n return false;\n }\n }\n }\n\n // Compare children and their properties recursively\n for (const childId of childrenA) {\n if (!childrenB.includes(childId)) {\n return false;\n }\n\n if (!RepTree.compareVertices(childId, treeA, treeB)) {\n return false;\n }\n }\n\n return true;\n }\n\n private newVertexInternal(vertexId: string, parentId: string | null): string {\n this.lamportClock++;\n // To create a vertex - we move a vertex with a fresh id under the parent.\n // No need to have a separate \"create vertex\" operation.\n const op = newMoveVertexOp(this.lamportClock, this.peerId, vertexId, parentId);\n this.localOps.push(op);\n this.applyMove(op);\n\n // Set the creation date\n this.setVertexProperty(vertexId, '_c', new Date().toISOString());\n\n return vertexId;\n }\n\n private newVertexInternalWithUUID(parentId: string | null): string {\n const vertexId = uuid();\n return this.newVertexInternal(vertexId, parentId);\n }\n\n private ensureNullVertex() {\n const vertexId = RepTree.NULL_VERTEX_ID;\n\n // Check if the null vertex already exists\n if (this.state.getVertex(vertexId)) {\n return;\n }\n\n this.newVertexInternal(vertexId, null);\n }\n\n /** Updates the lamport clock with the counter value of the operation */\n private updateLamportClock(operation: VertexOperation): void {\n // This is how Lamport clock updates with a foreign operation that has a greater counter value.\n if (operation.id.counter > this.lamportClock) {\n this.lamportClock = operation.id.counter;\n }\n }\n\n private applyPendingMovesForParent(parentId: string) {\n // If a parent doesn't exist, we can't apply pending moves yet.\n if (!this.state.getVertex(parentId)) {\n return;\n }\n\n const pendingMoves = this.pendingMovesWithMissingParent.get(parentId);\n if (!pendingMoves) {\n return;\n }\n\n this.pendingMovesWithMissingParent.delete(parentId);\n\n for (const pendingOp of pendingMoves) {\n this.applyMove(pendingOp);\n }\n }\n\n private applyMove(op: MoveVertex) {\n // Check if a parent (unless we're dealing with the root vertex) exists for the move operation.\n // If it doesn't exist, stash the move op for later\n if (op.parentId !== null && !this.state.getVertex(op.parentId)) {\n if (!this.pendingMovesWithMissingParent.has(op.parentId)) {\n this.pendingMovesWithMissingParent.set(op.parentId, []);\n }\n this.pendingMovesWithMissingParent.get(op.parentId)!.push(op);\n return;\n }\n\n this.updateLamportClock(op);\n\n const lastOp = this.moveOps.length > 0 ? this.moveOps[this.moveOps.length - 1] : null;\n\n // If it's the most recent move operation - just try to move it. No conflict resolution is needed.\n if (lastOp === null || isOpIdGreaterThan(op.id, lastOp.id)) {\n this.moveOps.push(op);\n this.reportOpAsApplied(op);\n this.tryToMove(op);\n }\n\n // Here comes the core of the 'THE REPLICATED TREE ALGORITHM'.\n // From https://martin.kleppmann.com/papers/move-op.pdf\n // We undo all moves that are newer (based on the Lamport clock) than the target move, do the move, and then redo the moves we just undid.\n // The algorithm ensures that all replicas converge to the same tree after applying all operations.\n // The replicas are basically forced to apply the moves in the same order (by undo-do-redo).\n // So if a conflict or a cycle is introduced by some of the peers - the algorithm will resolve it.\n // tryToMove function has the logic to detect cycles and will ignore the move if it creates a cycle. \n else {\n let targetIndex = this.moveOps.length;\n for (let i = this.moveOps.length - 1; i >= 0; i--) {\n const moveOp = this.moveOps[i];\n targetIndex = i;\n if (isOpIdGreaterThan(op.id, moveOp.id)) {\n break;\n }\n else {\n this.undoMove(moveOp);\n }\n }\n\n // Insert the op at the correct position\n this.moveOps.splice(targetIndex + 1, 0, op);\n this.reportOpAsApplied(op);\n this.tryToMove(op);\n\n // Redo all of the operations after the operation that we applied\n for (let i = targetIndex + 2; i < this.moveOps.length; i++) {\n this.tryToMove(this.moveOps[i]);\n }\n }\n\n // After applying the move, check if it unblocks any pending moves\n // We use targetId here because this vertex might now be a parent for pending operations\n this.applyPendingMovesForParent(op.targetId);\n }\n\n private setLLWPropertyAndItsOpId(op: SetVertexProperty) {\n this.propertiesAndTheirOpIds.set(`${op.key}@${op.targetId}`, op.id);\n this.state.setProperty(op.targetId, op.key, op.value);\n this.reportOpAsApplied(op);\n }\n\n private setTransientPropertyAndItsOpId(op: SetVertexProperty) {\n this.transientPropertiesAndTheirOpIds.set(`${op.key}@${op.targetId}`, op.id);\n this.state.setTransientProperty(op.targetId, op.key, op.value);\n this.reportOpAsApplied(op);\n }\n\n private applyProperty(op: SetVertexProperty) {\n const targetVertex = this.state.getVertex(op.targetId);\n if (!targetVertex) {\n // No need to handle transient properties if the vertex doesn't exist\n if (op.transient) {\n return;\n }\n\n // If the vertex doesn't exist, we will wait for the move operation to appear that will create the vertex\n // so we can apply the property then.\n if (!this.pendingPropertiesWithMissingVertex.has(op.targetId)) {\n this.pendingPropertiesWithMissingVertex.set(op.targetId, []);\n }\n this.pendingPropertiesWithMissingVertex.get(op.targetId)!.push(op);\n return;\n }\n\n this.updateLamportClock(op);\n\n this.applyLLWProperty(op, targetVertex);\n }\n\n private applyLLWProperty(op: SetVertexProperty, targetVertex: VertexState) {\n const prevTransientOpId = this.transientPropertiesAndTheirOpIds.get(`${op.key}@${op.targetId}`);\n const prevOpId = this.propertiesAndTheirOpIds.get(`${op.key}@${op.targetId}`);\n\n if (!op.transient) {\n this.setPropertyOps.push(op);\n\n // Apply the property if it's not already applied or if the current op is newer\n // This is the last writer wins approach that ensures the same state between replicas.\n if (!prevOpId || isOpIdGreaterThan(op.id, prevOpId)) {\n this.setLLWPropertyAndItsOpId(op);\n } else {\n // We add it to set of known ops to avoid adding them to `setPropertyOps` multiple times \n // if we ever receive the same op from another peer.\n this.knownOps.add(opIdToString(op.id));\n }\n\n // Remove the transient property if the current op is greater\n if (prevTransientOpId && isOpIdGreaterThan(op.id, prevTransientOpId)) {\n this.transientPropertiesAndTheirOpIds.delete(`${op.key}@${op.targetId}`);\n targetVertex.removeTransientProperty(op.key);\n }\n\n } else {\n // Handle transient properties\n if (!prevTransientOpId || isOpIdGreaterThan(op.id, prevTransientOpId)) {\n this.setTransientPropertyAndItsOpId(op);\n }\n }\n }\n\n // Non-LWW modify-property flow removed\n\n private applyOperation(op: VertexOperation) {\n if (isMoveVertexOp(op)) {\n this.applyMove(op);\n } else if (isAnyPropertyOp(op)) {\n this.applyProperty(op);\n }\n }\n\n private reportOpAsApplied(op: VertexOperation) {\n this.knownOps.add(opIdToString(op.id));\n\n if (this._stateVectorEnabled) {\n this.stateVector.updateFromOp(op);\n }\n\n for (const callback of this.opAppliedCallbacks) {\n callback(op);\n }\n }\n\n private tryToMove(op: MoveVertex) {\n let targetVertex = this.state.getVertex(op.targetId);\n\n if (targetVertex) {\n // We cache the parentId before the move operation.\n // We will use it to undo the move according to the move op algorithm.\n this.parentIdBeforeMove.set(op.id, targetVertex.parentId);\n }\n\n // If trying to move the target vertex under itself - do nothing\n if (op.targetId === op.parentId) return;\n\n // If we try to move the vertex (op.targetId) under one of its descendants (op.parentId) - do nothing\n if (op.parentId && this.isAncestor(op.parentId, op.targetId)) return;\n\n this.state.moveVertex(op.targetId, op.parentId);\n\n // If the vertex didn't exist before the move - see if it has pending properties\n // and apply them.\n if (!targetVertex) {\n const pendingProperties = this.pendingPropertiesWithMissingVertex.get(op.targetId) || [];\n this.pendingPropertiesWithMissingVertex.delete(op.targetId);\n for (const prop of pendingProperties) {\n this.applyProperty(prop);\n }\n }\n }\n\n private undoMove(op: MoveVertex) {\n const targetVertex = this.state.getVertex(op.targetId);\n if (!targetVertex) {\n console.error(`An attempt to undo move operation ${opIdToString(op.id)} failed because the target vertex ${op.targetId} not found`);\n return;\n }\n\n const prevParentId = this.parentIdBeforeMove.get(op.id);\n if (prevParentId === undefined) {\n return;\n }\n\n this.state.moveVertex(op.targetId, prevParentId);\n }\n\n // --- Range-Based State Vector Methods --- \n\n /**\n * Returns the current state vector.\n * Returns a readonly reference to the internal state vector.\n */\n getStateVector(): Readonly<Record<string, number[][]>> | null {\n if (!this._stateVectorEnabled) {\n return null;\n }\n return this.stateVector.getState();\n }\n\n /**\n * Determines which operations are needed to synchronize \n * with the provided state vector.\n * \n * @param theirStateVector The state vector from another peer\n * @returns Operations that should be sent to the other peer, sorted by OpId.\n */\n getMissingOps(theirStateVector: Record<string, number[][]>): VertexOperation[] {\n // If state vector is disabled, fallback to sending all ops\n if (!this._stateVectorEnabled) {\n return [...this.moveOps, ...this.setPropertyOps];\n }\n\n // Create a StateVector instance from their state vector\n const otherStateVector = new StateVector(theirStateVector);\n\n // Get the missing ranges\n const missingRanges = this.stateVector.diff(otherStateVector);\n\n // Then, retrieve only the operations that fall within those ranges\n const missingOps: VertexOperation[] = [];\n // Combine moveOps and setPropertyOps for checking\n const allOps = [...this.moveOps, ...this.setPropertyOps];\n\n // Only check operations that might be in the missing ranges\n for (const op of allOps) {\n for (const range of missingRanges) {\n if (op.id.peerId === range.peerId &&\n op.id.counter >= range.start &&\n op.id.counter <= range.end) {\n missingOps.push(op);\n break; // Move to the next op once found in a missing range\n }\n }\n }\n\n // Sort the missing ops by OpId before returning, ensuring causal order\n missingOps.sort((a, b) => compareOpId(a.id, b.id));\n\n return missingOps;\n }\n\n /**\n * Gets or sets whether state vector tracking is enabled\n */\n get stateVectorEnabled(): boolean {\n return this._stateVectorEnabled;\n }\n\n /**\n * Sets the state vector enabled status\n * When enabled, rebuilds the state vector from existing operations if needed\n */\n set stateVectorEnabled(value: boolean) {\n if (value === this._stateVectorEnabled) return;\n\n if (value) {\n // Enable state vector and rebuild from existing operations\n this._stateVectorEnabled = true;\n this.stateVector = StateVector.fromOperations([...this.moveOps, ...this.setPropertyOps]);\n } else {\n // Disable state vector and clear it to save memory\n this._stateVectorEnabled = false;\n this.stateVector = new StateVector();\n }\n }\n\n /**\n * Parses the vertex properties with a provided schema that has a `parse` method (e.g., Zod schema)\n */\n parseVertex<T>(vertexId: string, schema: { parse: (data: unknown) => T }): T {\n const propsArray = this.getVertexProperties(vertexId);\n const propsObject: Record<string, unknown> = {};\n for (const { key, value } of propsArray) {\n propsObject[key] = value as unknown;\n }\n return schema.parse(propsObject);\n }\n}"],"mappings":";AAKO,SAAS,WAAW,SAAiB,QAAsB;AAChE,SAAO,EAAE,SAAS,OAAO;AAC3B;AAEO,SAAS,YAAY,OAAsB,OAA8B;AAC9E,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,UAAU,gBAAgB,KAAK;AACrC,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,wBAAwB,KAAK,EAAE;AAC7D,YAAQ;AAAA,EACV;AACA,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,UAAU,gBAAgB,KAAK;AACrC,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,wBAAwB,KAA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AAI,EAAE,EAAE,CAAC;AAEjD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,qBAA8B;AAChC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,mBAAmB,OAAgB;AACrC,QAAI,UAAU,KAAK,oBAAqB;AAExC,QAAI,OAAO;AAET,WAAK,sBAAsB;AAC3B,WAAK,cAAc,YAAY,eAAe,CAAC,GAAG,KAAK,SAAS,GAAG,KAAK,cAAc,CAAC;AAAA,IACzF,OAAO;AAEL,WAAK,sBAAsB;AAC3B,WAAK,cAAc,IAAI,YAAY;AAAA,IACrC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,YAAe,UAAkB,QAA4C;AAC3E,UAAM,aAAa,KAAK,oBAAoB,QAAQ;AACpD,UAAM,cAAuC,CAAC;AAC9C,eAAW,EAAE,KAAK,MAAM,KAAK,YAAY;AACvC,kBAAY,GAAG,IAAI;AAAA,IACrB;AACA,WAAO,OAAO,MAAM,WAAW;AAAA,EACjC;AACF;AAj0Ba,SACI,iBAAiB;AAD3B,IAAM,UAAN;","names":["vertex"]}
|
|
1
|
+
{"version":3,"sources":["../src/OpId.ts","../src/operations.ts","../src/VertexState.ts","../src/TreeState.ts","../src/uuid.ts","../src/reactive.ts","../src/Vertex.ts","../src/StateVector.ts","../src/RepTree.ts"],"sourcesContent":["export interface OpId {\n readonly counter: number;\n readonly peerId: string;\n}\n\nexport function createOpId(counter: number, peerId: string): OpId {\n return { counter, peerId };\n}\n\nexport function compareOpId(opIdA: OpId | string, opIdB: OpId | string): number {\n if (typeof opIdA === 'string') {\n const parsedA = tryParseOpIdStr(opIdA);\n if (!parsedA) throw new Error(`Invalid OpId string: ${opIdA}`);\n opIdA = parsedA;\n }\n if (typeof opIdB === 'string') {\n const parsedB = tryParseOpIdStr(opIdB);\n if (!parsedB) throw new Error(`Invalid OpId string: ${opIdB}`);\n opIdB = parsedB;\n }\n\n const counterA = opIdA.counter;\n const counterB = opIdB.counter;\n\n if (counterA > counterB) {\n return 1;\n } else if (counterA < counterB) {\n return -1;\n } else {\n return opIdA.peerId.localeCompare(opIdB.peerId);\n }\n}\n\nexport function equalsOpId(opIdA: OpId | string | null, opIdB: OpId | string | null): boolean {\n if (opIdA === opIdB) {\n return true;\n } else if (!opIdA || !opIdB) {\n return false;\n }\n\n return compareOpId(opIdA, opIdB) === 0;\n}\n\nexport function tryParseOpIdStr(opIdStr: string): OpId {\n const parts = opIdStr.split('@');\n\n if (parts.length !== 2) {\n throw new Error(`Invalid OpId string: ${opIdStr}`);\n }\n\n return createOpId(parseInt(parts[0], 10), parts[1]);\n}\n\nexport function isOpIdGreaterThan(opIdA: OpId | string, opIdB: OpId | string): boolean {\n return compareOpId(opIdA, opIdB) === 1;\n}\n\nexport function opIdToString(opId: OpId): string {\n return `${opId.counter}@${opId.peerId}`;\n}\n","import { type OpId, createOpId } from \"./OpId\";\nimport { type VertexPropertyType } from \"./treeTypes\";\n\nexport interface MoveVertex {\n id: OpId;\n targetId: string;\n parentId: string | null;\n}\n\nexport interface SetVertexProperty {\n id: OpId;\n targetId: string;\n key: string;\n value: VertexPropertyType;\n transient: boolean;\n}\n\nexport type VertexOperation = MoveVertex | SetVertexProperty;\n\nexport function isMoveVertexOp(op: VertexOperation): op is MoveVertex {\n return 'parentId' in op;\n}\n\nexport function isAnyPropertyOp(op: VertexOperation): op is SetVertexProperty {\n return 'key' in op;\n}\n\nexport function newMoveVertexOp(clock: number, peerId: string, targetId: string, parentId: string | null): MoveVertex {\n return { id: createOpId(clock, peerId), targetId, parentId };\n}\n\nexport function newSetVertexPropertyOp(clock: number, peerId: string, targetId: string, key: string, value: VertexPropertyType): SetVertexProperty {\n return { id: createOpId(clock, peerId), targetId, key, value, transient: false };\n}\n\nexport function newSetTransientVertexPropertyOp(clock: number, peerId: string, targetId: string, key: string, value: VertexPropertyType): SetVertexProperty {\n return { id: createOpId(clock, peerId), targetId, key, value, transient: true };\n}","import type { TreeVertexId, TreeVertexProperty, VertexPropertyType } from \"./treeTypes\";\n\nexport class VertexState {\n readonly id: string;\n parentId: TreeVertexId | null;\n private properties: TreeVertexProperty[];\n private transientProperties: TreeVertexProperty[];\n children: string[];\n\n constructor(id: string, parentId: TreeVertexId | null) {\n this.id = id;\n this.parentId = parentId;\n this.properties = [];\n this.transientProperties = [];\n this.children = [];\n }\n\n setProperty(key: string, value: any): void {\n const existingPropIndex = this.properties.findIndex(p => p.key === key);\n if (existingPropIndex !== -1) {\n if (value !== undefined) {\n // Update existing property\n this.properties[existingPropIndex] = { key, value };\n } else {\n // Remove existing property\n this.removeProperty(key);\n }\n } else {\n if (value !== undefined) {\n // Add new property\n this.properties.push({ key, value });\n }\n }\n }\n\n setTransientProperty(key: string, value: any): void {\n const existingPropIndex = this.transientProperties.findIndex(p => p.key === key);\n if (existingPropIndex !== -1) {\n if (value !== undefined) {\n // Update existing property\n this.transientProperties[existingPropIndex] = { key, value };\n } else {\n // Remove existing property\n this.removeTransientProperty(key);\n }\n } else {\n if (value !== undefined) {\n // Add new property\n this.transientProperties.push({ key, value });\n }\n }\n }\n\n getProperty(key: string, includingTransient: boolean = true): VertexPropertyType | undefined {\n if (includingTransient) {\n const transientProp = this.transientProperties.find(p => p.key === key);\n if (transientProp) {\n return transientProp.value;\n }\n }\n\n return this.properties.find(p => p.key === key)?.value;\n }\n\n getAllProperties(includingTransient: boolean = true): ReadonlyArray<TreeVertexProperty> {\n if (!includingTransient) {\n return this.properties;\n }\n\n const result: TreeVertexProperty[] = [];\n const seenKeys = new Set<string>();\n\n // Add transient properties first\n for (const prop of this.transientProperties) {\n result.push(prop);\n seenKeys.add(prop.key);\n }\n\n // Add permanent properties that don't have a transient override\n for (const prop of this.properties) {\n if (!seenKeys.has(prop.key)) {\n result.push(prop);\n }\n }\n\n return result;\n }\n\n removeProperty(key: string): void {\n this.properties = this.properties.filter(p => p.key !== key);\n }\n\n removeTransientProperty(key: string): void {\n this.transientProperties = this.transientProperties.filter(p => p.key !== key);\n }\n\n getTransientProperties(): ReadonlyArray<TreeVertexProperty> {\n return this.transientProperties;\n }\n\n clearAllTransientProperties(): void {\n this.transientProperties = [];\n }\n}\n","import type { TreeVertexId, VertexChangeEvent, VertexPropertyChangeEvent, VertexChildrenChangeEvent, VertexMoveEvent } from \"./treeTypes\";\nimport { VertexState } from \"./VertexState\";\n\nexport class TreeState {\n private vertices: Map<TreeVertexId, VertexState>;\n private changeCallbacks: Map<TreeVertexId, Set<(events: VertexChangeEvent[]) => void>> = new Map();\n private globalChangeCallbacks: Set<(events: VertexChangeEvent[]) => void> = new Set();\n\n private batchTickInterval: NodeJS.Timeout;\n private batchedEvents: Map<TreeVertexId, VertexChangeEvent[]> = new Map();\n\n constructor() {\n this.vertices = new Map();\n\n this.batchTickInterval = setInterval(() => {\n this.processBatchedEvents();\n }, 33.3);\n }\n\n dispose() {\n clearInterval(this.batchTickInterval);\n }\n\n private processBatchedEvents() {\n for (const [vertexId, events] of this.batchedEvents) {\n // Get last property events per key and last move/children events\n let lastMoveEvent: VertexMoveEvent | null = null;\n let lastChildrenEvent: VertexChildrenChangeEvent | null = null;\n const propertyEventsByKey = new Map<string, VertexPropertyChangeEvent>();\n\n for (let i = events.length - 1; i >= 0; i--) {\n const event = events[i];\n if (!lastMoveEvent && event.type === 'move') lastMoveEvent = event as VertexMoveEvent;\n if (!lastChildrenEvent && event.type === 'children') lastChildrenEvent = event as VertexChildrenChangeEvent;\n if (event.type === 'property') {\n const propertyEvent = event as VertexPropertyChangeEvent;\n if (!propertyEventsByKey.has(propertyEvent.key)) {\n propertyEventsByKey.set(propertyEvent.key, propertyEvent);\n }\n }\n }\n\n // Combine all events with move and children events first\n const filteredEvents = [\n ...(lastMoveEvent ? [lastMoveEvent] : []),\n ...(lastChildrenEvent ? [lastChildrenEvent] : []),\n ...propertyEventsByKey.values()\n ];\n\n this.globalChangeCallbacks.forEach(listener => listener(filteredEvents));\n this.changeCallbacks.get(vertexId)?.forEach(listener => listener(filteredEvents));\n }\n\n this.batchedEvents.clear();\n }\n\n getAllVertices(): ReadonlyArray<VertexState> {\n return Array.from(this.vertices.values());\n }\n\n getVertex(id: string): VertexState | undefined {\n return this.vertices.get(id);\n }\n\n getChildrenIds(vertexId: TreeVertexId): string[] {\n return this.getVertex(vertexId)?.children ?? [];\n }\n\n getChildren(vertexId: TreeVertexId): VertexState[] {\n return this.getChildrenIds(vertexId)\n .map(id => {\n const vertex = this.vertices.get(id);\n return vertex ? vertex : undefined;\n })\n .filter(vertex => vertex !== undefined)\n .sort((a, b) => {\n const aDate = a.getProperty('_c') as string;\n const bDate = b.getProperty('_c') as string;\n if (!aDate) return -1;\n if (!bDate) return 1;\n return new Date(aDate).getTime() - new Date(bDate).getTime();\n }) as VertexState[];\n }\n\n moveVertex(vertexId: TreeVertexId, newParentId: TreeVertexId | null): VertexState {\n let vertex = this.getVertex(vertexId);\n // Undefined if the vertex is new\n const prevParentId = vertex ? vertex.parentId : undefined;\n if (!vertex) {\n vertex = new VertexState(vertexId, newParentId);\n this.vertices.set(vertexId, vertex);\n }\n\n if (prevParentId === newParentId) {\n return vertex;\n }\n\n vertex.parentId = newParentId;\n\n let childrenInNewParent: string[] | null = null;\n let childrenInOldParent: string[] | null = null;\n\n // Update children arrays in vertices\n if (prevParentId) {\n const oldParentVertex = this.getVertex(prevParentId);\n if (oldParentVertex) {\n oldParentVertex.children = oldParentVertex.children.filter(child => child !== vertexId);\n childrenInOldParent = oldParentVertex.children;\n } else {\n console.error(`Old parent vertex not found for ${prevParentId}`);\n }\n }\n\n if (newParentId !== null) {\n const newParentVertex = this.vertices.get(newParentId);\n if (newParentVertex) {\n newParentVertex.children.push(vertexId);\n childrenInNewParent = newParentVertex.children;\n } else {\n console.error(`New parent vertex not found for ${newParentId}`);\n }\n }\n\n // We notify the listeners in the end so that they have the final state of the tree\n\n this.notifyChange({\n type: 'move',\n vertexId: vertexId,\n oldParentId: prevParentId,\n newParentId,\n } as VertexMoveEvent);\n\n if (childrenInNewParent !== null && newParentId !== null) {\n this.notifyChange({\n type: 'children',\n vertexId: newParentId,\n children: childrenInNewParent.map(id => this.vertices.get(id)!),\n } as VertexChildrenChangeEvent);\n }\n\n if (childrenInOldParent !== null && prevParentId) {\n this.notifyChange({\n type: 'children',\n vertexId: prevParentId,\n children: childrenInOldParent.map(id => this.vertices.get(id)!),\n } as VertexChildrenChangeEvent);\n }\n\n return vertex;\n }\n\n setProperty(vertexId: string, key: string, value: any) {\n const vertex = this.getVertex(vertexId);\n if (!vertex) {\n throw new Error(`Vertex ${vertexId} not found`);\n }\n\n vertex.setProperty(key, value);\n\n this.notifyChange({\n type: 'property',\n vertexId: vertexId,\n key,\n value,\n } as VertexPropertyChangeEvent);\n\n if (vertex.parentId !== null) {\n this.notifyChange({\n type: 'children',\n vertexId: vertex.parentId,\n children: [vertex], // @TODO: shoulld I set all children or rename this property?\n } as VertexChildrenChangeEvent);\n }\n }\n\n setTransientProperty(vertexId: string, key: string, value: any) {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n vertex.setTransientProperty(key, value);\n }\n\n // @TODO: add info that it's a transient property\n this.notifyChange({\n type: 'property',\n vertexId: vertexId,\n key,\n value,\n } as VertexPropertyChangeEvent);\n }\n\n addChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void) {\n if (!this.changeCallbacks.has(vertexId)) {\n this.changeCallbacks.set(vertexId, new Set());\n }\n this.changeCallbacks.get(vertexId)!.add(listener);\n }\n\n removeChangeCallback(vertexId: TreeVertexId, listener: (events: VertexChangeEvent[]) => void) {\n this.changeCallbacks.get(vertexId)?.delete(listener);\n }\n\n addGlobalChangeCallback(listener: (events: VertexChangeEvent[]) => void) {\n this.globalChangeCallbacks.add(listener);\n }\n\n removeGlobalChangeCallback(listener: (events: VertexChangeEvent[]) => void) {\n this.globalChangeCallbacks.delete(listener);\n }\n\n private notifyChange(event: VertexChangeEvent) {\n let events = this.batchedEvents.get(event.vertexId);\n if (!events) {\n events = [];\n this.batchedEvents.set(event.vertexId, events);\n }\n\n events.push(event);\n\n // @TODO: have immediate events\n //this.globalChangeCallbacks.forEach(listener => listener(event));\n //this.changeCallbacks.get(event.vertexId)?.forEach(listener => listener(event));\n }\n\n printTree(vertexId: TreeVertexId, indent: string = \"\", isLast: boolean = true): string {\n const prefix = indent + (isLast ? \"└── \" : \"├── \");\n let result = prefix + vertexId + \"\\n\";\n\n let vertexName: string | null = null;\n\n if (vertexId !== null) {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n for (const prop of vertex.getAllProperties()) {\n if (prop.key === \"name\") {\n vertexName = prop.value as string;\n //continue;\n }\n\n const propPrefix = indent + (isLast ? \" \" : \"│ \") + \"• \";\n result += `${propPrefix}${prop.key}: ${JSON.stringify(prop.value)}\\n`;\n }\n }\n }\n\n // Get children and sort them for deterministic output\n const children = this.getChildrenIds(vertexId);\n const sortedChildren = [...children].sort((a, b) => {\n // Sort by name if available\n const vertexA = this.getVertex(a);\n const vertexB = this.getVertex(b);\n \n const nameA = vertexA?.getProperty('name') as string | undefined;\n const nameB = vertexB?.getProperty('name') as string | undefined;\n \n // If both have names, compare them\n if (nameA && nameB) {\n return nameA.localeCompare(nameB);\n }\n \n // If only one has a name, prioritize the one with a name\n if (nameA) return -1;\n if (nameB) return 1;\n \n // Fall back to sorting by ID for consistent output\n return a.localeCompare(b);\n });\n \n for (let i = 0; i < sortedChildren.length; i++) {\n const childId = sortedChildren[i];\n const isLastChild = i === sortedChildren.length - 1;\n result += this.printTree(childId, indent + (isLast ? \" \" : \"│ \"), isLastChild);\n }\n\n return result;\n }\n}","const removeDashes = (guid: string) => guid.replace(/-/g, '');\n\nexport default function uuid(): string {\n return removeDashes(crypto.randomUUID());\n}","import type { RepTree } from './RepTree';\nimport type { Vertex } from './Vertex';\n\nexport type FieldSchemaLike = {\n safeParse?: (input: unknown) => { success: true; data: unknown } | { success: false };\n};\n\nexport type SchemaLike<T> = {\n safeParse?: (input: unknown) => { success: true; data: T } | { success: false };\n parse?: (input: unknown) => T;\n shape?: Record<string, FieldSchemaLike>;\n};\n\nexport type BindOptions<T> = {\n schema?: SchemaLike<T>;\n includeInternalKeys?: boolean;\n};\n\n/**\n * A bound vertex object that forwards reads/writes to a vertex.\n * @param T - The type of the vertex.\n */\nexport type BindedVertex<T> = T & {\n\n $vertex: Vertex;\n $id: string;\n $parentId: string | null;\n $parent: Vertex | undefined;\n $children: Vertex[];\n $childrenIds: string[];\n\n /**\n * Apply transient edits that override reads but do not persist yet.\n * @param fn \n */\n $useTransients(fn: (t: T) => void): void;\n \n /**\n * Promote current transient overlays to persistent values.\n */\n $commitTransients(): void;\n \n /**\n * Move the vertex to a new parent.\n * @param parent - The new parent vertex or ID.\n */\n $moveTo(parent: Vertex | BindedVertex<any> | string): void;\n \n /**\n * Delete the vertex.\n */\n $delete(): void;\n \n /**\n * Observe changes to the vertex.\n * @param listener - The listener function to call when changes occur.\n */\n $observe(listener: (events: any[]) => void): () => void;\n \n /**\n * Observe changes to the children of the vertex.\n * @param listener - The listener function to call when children change.\n */\n $observeChildren(listener: (children: Vertex[]) => void): () => void;\n \n /**\n * Create a new child vertex.\n * @param props - The properties to set on the new child vertex.\n */\n \n $newChild(props?: Record<string, any> | object | null): Vertex;\n \n /**\n * Create a new named child vertex.\n * @param name - The name of the new child vertex.\n * @param props - The properties to set on the new child vertex.\n */\n $newNamedChild(name: string, props?: Record<string, any> | object | null): Vertex;\n};\n\n/**\n * Returns a live Proxy that forwards reads/writes to a vertex.\n * - Reads reflect the latest CRDT state (including transients by default)\n * - Writes persist to the CRDT with optional schema validation\n */\nexport function bindVertex<T extends Record<string, unknown>>(\n tree: RepTree,\n id: string,\n schemaOrOptions?: SchemaLike<T> | BindOptions<T>\n): BindedVertex<T> {\n const isOptions =\n typeof schemaOrOptions === 'object' && schemaOrOptions !== null && (\n Object.prototype.hasOwnProperty.call(schemaOrOptions as object, 'includeInternalKeys') ||\n Object.prototype.hasOwnProperty.call(schemaOrOptions as object, 'schema')\n );\n\n const options = (isOptions\n ? (schemaOrOptions as BindOptions<T>)\n : { schema: schemaOrOptions as SchemaLike<T> }) as BindOptions<T>;\n\n const schema = options.schema;\n\n const obj: any = {};\n\n Object.defineProperties(obj, {\n $vertex: { get: () => tree.getVertex(id)!, enumerable: false, configurable: true },\n $id: { get: () => id, enumerable: false, configurable: true },\n $parentId: { get: () => tree.getVertex(id)?.parentId ?? null, enumerable: false, configurable: true },\n $parent: { get: () => tree.getVertex(id)?.parent, enumerable: false, configurable: true },\n $children: { get: () => tree.getChildren(id), enumerable: false, configurable: true },\n $childrenIds: { get: () => tree.getChildrenIds(id), enumerable: false, configurable: true },\n $moveTo: {\n value: (parent: any) => {\n const parentId = typeof parent === 'object' && parent !== null ? (parent.id || parent.$id) : parent;\n tree.moveVertex(id, parentId);\n },\n enumerable: false,\n configurable: true,\n writable: false,\n },\n $delete: { value: () => tree.deleteVertex(id), enumerable: false, configurable: true, writable: false },\n $observe: { value: (listener: (events: any[]) => void) => tree.observe(id, listener), enumerable: false, configurable: true, writable: false },\n $observeChildren: {\n value: (listener: (children: Vertex[]) => void) =>\n tree.observe(id, (events: any[]) => {\n if (events.some((e: any) => e.type === 'children')) {\n listener(tree.getChildren(id));\n }\n }),\n enumerable: false,\n configurable: true,\n writable: false,\n },\n $newChild: { value: (props?: Record<string, any> | object | null) => tree.getVertex(id)!.newChild(props), enumerable: false, configurable: true, writable: false },\n $newNamedChild: { value: (name: string, props?: Record<string, any> | object | null) => tree.getVertex(id)!.newNamedChild(name, props), enumerable: false, configurable: true, writable: false },\n $useTransients: {\n value: function (fn: (t: any) => void) {\n const transientProxy = new Proxy({} as any, {\n set(_, prop: string | symbol, value: unknown) {\n if (typeof prop === 'string') {\n tree.setTransientVertexProperty(id, prop, value as any);\n }\n return true;\n },\n get(_, prop: string | symbol) {\n if (typeof prop !== 'string') return undefined;\n const rawValue = tree.getVertexProperty(id, prop, true);\n return rawValue as unknown;\n },\n });\n fn(transientProxy);\n },\n enumerable: false,\n configurable: true,\n writable: false,\n },\n $commitTransients: { value: () => tree.commitTransients(id), enumerable: false, configurable: true, writable: false },\n equals: {\n value: function (other: any) {\n if (other && typeof other === 'object' && '$id' in other) {\n return other.$id === id;\n }\n return false;\n },\n enumerable: false,\n configurable: true,\n writable: false,\n },\n });\n\n const proxy = new Proxy(obj, {\n get(target, prop: string | symbol, receiver) {\n if (typeof prop !== 'string') {\n return Reflect.get(target, prop, receiver);\n }\n if (prop in target) {\n return Reflect.get(target, prop, receiver);\n }\n const rawValue = tree.getVertexProperty(id, prop, true);\n return rawValue;\n },\n\n set(target, prop: string | symbol, value: unknown) {\n if (typeof prop !== 'string') {\n return Reflect.set(target, prop, value);\n }\n\n if (schema?.shape && schema.shape[prop]) {\n const field = schema.shape[prop]!;\n if (field.safeParse) {\n const res = field.safeParse(value);\n if (!res.success) throw new Error(`Invalid value for ${prop}`);\n value = (res as any).data;\n }\n }\n\n tree.setVertexProperty(id, prop, value as any);\n return true;\n },\n\n deleteProperty(_target, prop: string | symbol) {\n if (typeof prop !== 'string') {\n return true;\n }\n tree.setVertexProperty(id, prop, undefined as any);\n return true;\n },\n });\n\n return proxy as BindedVertex<T>;\n}\n","import type { VertexState } from \"./VertexState\";\nimport type { RepTree } from \"./RepTree\";\nimport { bindVertex, type SchemaLike, type BindOptions, type BindedVertex } from './reactive';\nimport type { VertexChangeEvent, VertexPropertyType } from \"./treeTypes\";\n\n/**\n * A wrapper class for VertexState that provides a more convenient API\n * for working with vertices in a RepTree.\n */\nexport class Vertex {\n constructor(\n private tree: RepTree,\n private state: VertexState\n ) { }\n\n get id(): string {\n return this.state.id;\n }\n\n get name(): string | undefined {\n return this.getProperty('name') as string | undefined;\n }\n\n set name(name: string) {\n this.tree.setVertexProperty(this.id, 'name', name);\n }\n\n get createdAt(): Date {\n const createdAt = this.getProperty('_c') as string;\n if (!createdAt) {\n return new Date(0);\n }\n return new Date(createdAt);\n }\n\n get path(): string {\n //return this.tree.getVertexPath(this.id);\n throw new Error('Not implemented');\n }\n\n get parentId(): string | null {\n return this.state.parentId;\n }\n\n get parent(): Vertex | undefined {\n if (!this.parentId) {\n return undefined;\n }\n\n return this.tree.getVertex(this.parentId);\n }\n\n get children(): Vertex[] {\n return this.tree.getChildren(this.id);\n }\n\n get childrenIds(): string[] {\n return this.tree.getChildrenIds(this.id);\n }\n\n getAsTypedObject<T>(): T {\n return this.getProperties() as T;\n }\n\n getChildrenAsTypedArray<T>(): T[] {\n return this.children.map(v => v.getAsTypedObject<T>());\n }\n\n newChild(props?: Record<string, VertexPropertyType> | object | null): Vertex {\n // Forbid nested children in props (not supported yet)\n if (props && typeof props === 'object' && 'children' in (props as any)) {\n throw new Error('Passing children inside props is not supported at the moment');\n }\n\n const normalized = Vertex.normalizePropsForCreation(props);\n return this.tree.newVertex(this.id, normalized);\n }\n\n newNamedChild(name: string, props?: Record<string, VertexPropertyType> | object | null): Vertex {\n // Forbid nested children in props (not supported yet)\n if (props && typeof props === 'object' && 'children' in (props as any)) {\n throw new Error('Passing children inside props is not supported at the moment');\n }\n\n const normalized = Vertex.normalizePropsForCreation(props, name);\n return this.tree.newNamedVertex(this.id, name, normalized);\n }\n\n setProperty(key: string, value: VertexPropertyType): void {\n // First check if the property is already set (not including transient properties)\n const existingValue = this.getProperty(key, false);\n if (existingValue === value) {\n return;\n }\n\n this.tree.setVertexProperty(this.id, key, value);\n }\n\n setTransientProperty(key: string, value: VertexPropertyType): void {\n // First check if the property is already set\n const existingValue = this.getProperty(key);\n if (existingValue === value) {\n return;\n }\n\n this.tree.setTransientVertexProperty(this.id, key, value);\n }\n\n commitTransients(): void {\n this.tree.commitTransients(this.id);\n }\n\n setProperties(props: Record<string, VertexPropertyType> | object): void {\n for (const [key, value] of Object.entries(props)) {\n this.setProperty(key, value);\n }\n }\n\n getProperty(key: string, includingTransient: boolean = true): VertexPropertyType | undefined {\n return this.tree.getVertexProperty(this.id, key, includingTransient);\n }\n\n getProperties(): Record<string, VertexPropertyType> {\n const props: Record<string, VertexPropertyType> = {};\n this.tree.getVertexProperties(this.id).forEach(p => {\n props[p.key] = p.value;\n });\n return props;\n }\n\n findAllChildrenWithProperty(key: string, value: VertexPropertyType): Vertex[] {\n return this.children.filter(c => c.getProperty(key) === value);\n }\n\n findFirstChildVertexWithProperty(key: string, value: VertexPropertyType): Vertex | undefined {\n return this.children.find(c => c.getProperty(key) === value);\n }\n\n findFirstTypedChildWithProperty<T>(key: string, value: VertexPropertyType): T | undefined {\n return this.findFirstChildVertexWithProperty(key, value)?.getAsTypedObject<T>();\n }\n\n findAllTypedChildrenWithProperty<T>(key: string, value: VertexPropertyType): T[] {\n return this.findAllChildrenWithProperty(key, value).map(c => c.getAsTypedObject<T>());\n }\n\n observe(listener: (events: VertexChangeEvent[]) => void): () => void {\n const unobserve = this.tree.observe(this.id, listener);\n return () => unobserve();\n }\n\n observeChildren(listener: (children: Vertex[]) => void): () => void {\n const unobserve = this.tree.observe(this.id, (events: VertexChangeEvent[]) => {\n if (events.some(e => e.type === 'children')) {\n listener(this.children);\n }\n });\n return () => unobserve();\n }\n\n observeChildrenAsTypedArray<T>(listener: (children: T[]) => void): () => void {\n return this.observeChildren((children) => {\n listener(children.map(c => c.getProperties() as unknown as T));\n });\n }\n\n delete(): void {\n this.tree.deleteVertex(this.id);\n }\n\n moveTo(parent: Vertex): void {\n this.tree.moveVertex(this.id, parent.id);\n }\n\n /** Returns a live reactive object bound to this vertex. Accepts schema or options. */\n bind<T extends Record<string, unknown>>(schemaOrOptions?: SchemaLike<T> | BindOptions<T>): BindedVertex<T> {\n return bindVertex<T>(this.tree, this.id, schemaOrOptions as any);\n }\n\n /**\n * Normalizes an input props object for vertex creation:\n * - Filters unsupported field types with a console warning\n * - When a name param is provided to newNamedChild, ignores conflicting name in props\n */\n private static normalizePropsForCreation(props?: Record<string, VertexPropertyType> | object | null, explicitName?: string): Record<string, VertexPropertyType> | null {\n if (!props) return null;\n const input = props as Record<string, any>;\n const out: Record<string, VertexPropertyType> = {};\n const skipped: string[] = [];\n\n for (const [rawKey, rawValue] of Object.entries(input)) {\n if (rawValue === undefined) {\n // Skip undefined to avoid writing explicit undefineds on creation\n continue;\n }\n\n // Disallow nested children handled earlier; skip here defensively\n if (rawKey === 'children') continue;\n\n // Use keys as-is\n let key = rawKey;\n if (rawKey === 'name' && explicitName !== undefined) {\n // Explicit argument takes precedence\n console.warn('newNamedChild: \"name\" in props is ignored because a name argument was provided');\n continue;\n }\n\n // Value normalization\n let value: any = rawValue;\n if (key === '_c') {\n if (value instanceof Date) {\n value = value.toISOString();\n } else if (typeof value === 'string') {\n // leave as is (assumed ISO)\n } else {\n skipped.push(rawKey);\n continue;\n }\n }\n\n const isPrimitive = (v: any) => typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean';\n\n if (Array.isArray(value)) {\n // Ensure array of primitives\n if (!value.every(isPrimitive)) {\n skipped.push(rawKey);\n continue;\n }\n } else if (typeof value === 'object' && value !== null) {\n // Unsupported nested object\n skipped.push(rawKey);\n continue;\n } else if (!isPrimitive(value)) {\n skipped.push(rawKey);\n continue;\n }\n\n out[key] = value as VertexPropertyType;\n }\n\n if (skipped.length > 0) {\n console.warn(`Some fields were skipped due to unsupported types: ${skipped.join(', ')}`);\n }\n\n return Object.keys(out).length > 0 ? out : null;\n }\n} ","import { OpId } from \"./OpId\";\nimport type { OpIdRange } from \"./treeTypes\";\nimport type { VertexOperation } from \"./operations\";\n\n/**\n * Helper function to subtract one set of ranges from another.\n * Returns the ranges in A that are not in B.\n * Assumes ranges in both A and B are sorted and non-overlapping.\n */\nexport function subtractRanges(rangesA: number[][], rangesB: number[][]): number[][] {\n if (rangesB.length === 0) return rangesA.map(r => [...r]); // Return a copy\n if (rangesA.length === 0) return []; // If A is empty, nothing to subtract\n\n const result: number[][] = [];\n let indexB = 0;\n\n for (const rangeA of rangesA) {\n let currentStart = rangeA[0];\n const endA = rangeA[1];\n\n // Iterate through ranges in B that could potentially overlap with rangeA\n while (indexB < rangesB.length && rangesB[indexB][1] < currentStart) {\n // Skip ranges in B that are entirely before the current start\n indexB++;\n }\n\n while (indexB < rangesB.length && rangesB[indexB][0] <= endA) {\n const startB = rangesB[indexB][0];\n const endB = rangesB[indexB][1];\n\n // If there's a gap before this range in B starts\n if (currentStart < startB) {\n // Add the portion of rangeA before the overlap\n result.push([currentStart, Math.min(endA, startB - 1)]);\n }\n\n // Move current start past this range in B\n currentStart = Math.max(currentStart, endB + 1);\n\n // If we've gone past the end of rangeA, break inner loop\n if (currentStart > endA) break;\n\n // If the current rangeB ends after rangeA, we don't need to check further ranges in B for this rangeA\n if (endB >= endA) break;\n\n // Only advance indexB if the current rangeB is fully processed relative to currentStart\n if (endB < currentStart) {\n indexB++;\n } else if (startB >= currentStart) {\n indexB++;\n }\n }\n\n // If there's a remaining part of rangeA after processing overlaps with B\n if (currentStart <= endA) {\n result.push([currentStart, endA]);\n }\n }\n\n return result;\n}\n\n/**\n * StateVector tracks operations that have been applied using a range-based representation.\n * It's used for synchronization between peers to determine which operations need to be sent.\n */\nexport class StateVector {\n private ranges: Record<string, number[][]> = {};\n\n /**\n * Creates a new StateVector.\n * @param initialState Optional initial state to copy from\n */\n constructor(initialState: Record<string, number[][]> = {}) {\n // Create a deep copy of the initial state\n for (const [peerId, peerRanges] of Object.entries(initialState)) {\n this.ranges[peerId] = peerRanges.map(range => [...range]);\n }\n }\n\n /**\n * Updates the state vector with a newly applied operation.\n * Assumes ranges are sorted and non-overlapping.\n * \n * @param peerId The peer ID of the operation\n * @param counter The counter value of the operation\n */\n update(peerId: string, counter: number): void {\n // Initialize ranges array for this peer if it doesn't exist\n if (!this.ranges[peerId]) {\n this.ranges[peerId] = [];\n }\n\n const ranges = this.ranges[peerId];\n\n // Case 1: No ranges yet\n if (ranges.length === 0) {\n ranges.push([counter, counter]);\n return;\n }\n\n let rangeExtendedOrMerged = false;\n let insertIndex = -1;\n\n for (let i = 0; i < ranges.length; i++) {\n const range = ranges[i];\n\n // If counter is already in a range, do nothing\n if (counter >= range[0] && counter <= range[1]) {\n rangeExtendedOrMerged = true;\n break;\n }\n\n // If counter is one less than range start, extend range start\n if (counter === range[0] - 1) {\n range[0] = counter;\n rangeExtendedOrMerged = true;\n // Check if this range now merges with the previous range\n if (i > 0 && range[0] === ranges[i - 1][1] + 1) {\n ranges[i - 1][1] = range[1]; // Merge into previous\n ranges.splice(i, 1); // Remove current\n }\n break;\n }\n\n // If counter is one more than range end, extend range end\n if (counter === range[1] + 1) {\n range[1] = counter;\n rangeExtendedOrMerged = true;\n // Check if this range now merges with the next range\n if (i < ranges.length - 1 && range[1] + 1 === ranges[i + 1][0]) {\n range[1] = ranges[i + 1][1]; // Merge next into current\n ranges.splice(i + 1, 1); // Remove next\n }\n break;\n }\n\n // Keep track of where to insert if no extension/merge happens\n if (counter < range[0] && insertIndex === -1) {\n insertIndex = i;\n }\n }\n\n // If we couldn't extend or merge any range, add a new one\n if (!rangeExtendedOrMerged) {\n if (insertIndex === -1) {\n // If counter is greater than all existing ranges, add to the end\n insertIndex = ranges.length;\n }\n ranges.splice(insertIndex, 0, [counter, counter]);\n // After inserting, check if the new range merges with neighbors\n // Merge with previous range if possible\n if (insertIndex > 0 && ranges[insertIndex][0] === ranges[insertIndex - 1][1] + 1) {\n ranges[insertIndex - 1][1] = ranges[insertIndex][1];\n ranges.splice(insertIndex, 1);\n insertIndex--; // Adjust index after merging\n }\n // Merge with next range if possible (use adjusted insertIndex)\n if (insertIndex < ranges.length - 1 && ranges[insertIndex][1] + 1 === ranges[insertIndex + 1][0]) {\n ranges[insertIndex][1] = ranges[insertIndex + 1][1];\n ranges.splice(insertIndex + 1, 1);\n }\n }\n }\n\n /**\n * Updates the state vector with a newly applied operation.\n * \n * @param op The operation that was just applied\n */\n updateFromOp(op: VertexOperation): void {\n this.update(op.id.peerId, op.id.counter);\n }\n\n /**\n * Returns the current state vector.\n * Returns a readonly reference to the internal state.\n */\n getState(): Readonly<Record<string, number[][]>> {\n return this.ranges;\n }\n\n /**\n * Calculates which operation ranges we have that the other state vector is missing\n * by comparing state vectors.\n * \n * @param other The other state vector to compare against\n * @returns Array of operation ID ranges that we have but they don't\n */\n diff(other: StateVector): OpIdRange[] {\n const missingRanges: OpIdRange[] = [];\n const theirState = other.getState();\n\n // Check what we have that they don't have\n for (const [peerId, ourRanges] of Object.entries(this.ranges)) {\n const theirRanges = theirState[peerId] || [];\n\n // Calculate ranges we have that they don't\n const missing = subtractRanges(ourRanges, theirRanges);\n\n // Convert to OpIdRange format\n for (const [start, end] of missing) {\n // Ensure the range is valid (start <= end)\n if (start <= end) {\n missingRanges.push({\n peerId,\n start,\n end\n });\n }\n }\n }\n\n return missingRanges;\n }\n\n /**\n * Checks if the state vector contains the given operation ID\n * \n * @param opId The operation ID to check\n * @returns true if the operation is in the state vector, false otherwise\n */\n contains(opId: OpId): boolean {\n const peerId = opId.peerId;\n const counter = opId.counter;\n\n if (!this.ranges[peerId]) {\n return false;\n }\n\n for (const [start, end] of this.ranges[peerId]) {\n if (counter >= start && counter <= end) {\n return true;\n }\n }\n\n return false;\n }\n\n /**\n * Creates a copy of this state vector\n */\n clone(): StateVector {\n return new StateVector(this.ranges);\n }\n\n /**\n * Builds a state vector from an array of operations\n * @param operations The operations to build the state vector from\n * @returns A new StateVector instance\n */\n static fromOperations(operations: ReadonlyArray<VertexOperation>): StateVector {\n const stateVector = new StateVector();\n for (const op of operations) {\n stateVector.updateFromOp(op);\n }\n return stateVector;\n }\n} ","import {\n newMoveVertexOp,\n type MoveVertex,\n type SetVertexProperty,\n isMoveVertexOp,\n type VertexOperation,\n newSetVertexPropertyOp,\n newSetTransientVertexPropertyOp,\n isAnyPropertyOp\n} from \"./operations\";\nimport type { VertexPropertyType, TreeVertexProperty, VertexChangeEvent, TreeVertexId, VertexMoveEvent } from \"./treeTypes\";\nimport { VertexState } from \"./VertexState\";\nimport { TreeState } from \"./TreeState\";\nimport { type OpId, compareOpId, equalsOpId, isOpIdGreaterThan, opIdToString } from \"./OpId\";\nimport uuid from \"./uuid\";\nimport { Vertex } from './Vertex';\nimport { StateVector } from './StateVector';\n\ntype PropertyKeyAtVertexId = `${string}@${TreeVertexId}`;\n\n/**\n * RepTree is a tree data structure for storing vertices with properties.\n * It uses 2 conflict-free replicated data types (CRDTs) to manage seamless replication between peers.\n * A move tree CRDT is used for the tree structure (https://martin.kleppmann.com/papers/move-op.pdf).\n * A last writer wins (LWW) CRDT is used for properties.\n */\nexport class RepTree {\n private static NULL_VERTEX_ID = '0';\n\n readonly peerId: string;\n private rootVertexId: string | undefined;\n\n private lamportClock = 0;\n private state: TreeState;\n private moveOps: MoveVertex[] = [];\n private setPropertyOps: SetVertexProperty[] = [];\n private propertiesAndTheirOpIds: Map<PropertyKeyAtVertexId, OpId> = new Map();\n private transientPropertiesAndTheirOpIds: Map<PropertyKeyAtVertexId, OpId> = new Map();\n // Observers for non-structural properties are not used\n private localOps: VertexOperation[] = [];\n private pendingMovesWithMissingParent: Map<string, MoveVertex[]> = new Map();\n private pendingPropertiesWithMissingVertex: Map<string, SetVertexProperty[]> = new Map();\n private knownOps: Set<string> = new Set();\n private parentIdBeforeMove: Map<OpId, string | null | undefined> = new Map();\n private opAppliedCallbacks: ((op: VertexOperation) => void)[] = [];\n\n // State vector tracking operations from each peer\n private stateVector: StateVector;\n private _stateVectorEnabled: boolean = true;\n\n /**\n * @param peerId - The peer ID of the current client. Should be unique across all peers.\n * @param ops - The operations to replicate an existing tree, if not provided - an empty tree will be created without a root vertex\n */\n constructor(peerId: string, ops?: ReadonlyArray<VertexOperation>) {\n this.peerId = peerId;\n this.state = new TreeState();\n\n // Initialize state vector (enabled by default)\n this.stateVector = new StateVector();\n\n if (ops && ops.length > 0) {\n this.applyOps(ops);\n\n const root = this.root;\n if (!root) {\n throw new Error('There has to be a root vertex in the operations');\n }\n\n // @TODO: validate the tree structure, throw an exception if it's invalid\n }\n // @TODO: remove it? It creates an extra null vertex op in every new empty tree. Or is it ok?\n else {\n this.ensureNullVertex();\n }\n }\n\n get root(): Vertex | undefined {\n if (!this.rootVertexId) {\n const vertices = this.state.getAllVertices();\n for (const vertex of vertices) {\n if (vertex.parentId === null && vertex.id !== RepTree.NULL_VERTEX_ID) {\n this.rootVertexId = vertex.id;\n return new Vertex(this, vertex);\n }\n }\n\n return undefined;\n }\n\n const rootVertex = this.state.getVertex(this.rootVertexId);\n if (!rootVertex) {\n throw new Error(\"Root vertex not found\");\n }\n\n return new Vertex(this, rootVertex);\n }\n\n replicate(newPeerId: string): RepTree {\n return new RepTree(newPeerId, this.getAllOps());\n }\n\n getMoveOps(): ReadonlyArray<MoveVertex> {\n return this.moveOps;\n }\n\n getAllOps(): ReadonlyArray<VertexOperation> {\n return [...this.moveOps, ...this.setPropertyOps];\n }\n\n getVertex(vertexId: string): Vertex | undefined {\n const vertex = this.state.getVertex(vertexId);\n return vertex ? new Vertex(this, vertex) : undefined;\n }\n\n getAllVertices(): ReadonlyArray<Vertex> {\n return this.state.getAllVertices().map(v => new Vertex(this, v));\n }\n\n getParent(vertexId: string): Vertex | undefined {\n const parentId = this.state.getVertex(vertexId)?.parentId;\n const parent = parentId ? this.state.getVertex(parentId) : undefined;\n return parent ? new Vertex(this, parent) : undefined;\n }\n\n getChildren(vertexId: string): Vertex[] {\n return this.state.getChildren(vertexId).map(v => new Vertex(this, v));\n }\n\n getChildrenIds(vertexId: string): string[] {\n return this.state.getChildrenIds(vertexId);\n }\n\n getAncestors(vertexId: string): Vertex[] {\n const ancestors: Vertex[] = [];\n let currentVertex = this.state.getVertex(vertexId);\n\n while (currentVertex && currentVertex.parentId) {\n const parentVertex = this.state.getVertex(currentVertex.parentId);\n if (parentVertex) {\n ancestors.push(new Vertex(this, parentVertex));\n currentVertex = parentVertex;\n } else {\n break;\n }\n }\n\n return ancestors;\n }\n\n getVertexProperty(vertexId: string, key: string, includingTransient: boolean = true): VertexPropertyType | undefined {\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n return undefined;\n }\n\n return vertex.getProperty(key, includingTransient);\n }\n\n getVertexProperties(vertexId: string): Readonly<TreeVertexProperty[]> {\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n return [];\n }\n\n return vertex.getAllProperties();\n }\n\n /**\n * Returns all local operations and clears the local operations list.\n * Can be used to get all operations that were generated from this peer and need to be sent to other peers.\n */\n popLocalOps(): VertexOperation[] {\n const ops = this.localOps;\n this.localOps = [];\n return ops;\n }\n\n /**\n * This is the first vertex that will contain all other vertices.\n * If you plan to replicate a tree then don't use this method and instead merge\n * in the ops from another tree (that will also contain the root vertex).\n * @returns The root vertex\n */\n createRoot(): Vertex {\n if (this.rootVertexId) {\n throw new Error('Root vertex already exists');\n }\n\n this.rootVertexId = this.newVertexInternalWithUUID(null);\n\n const rootVertex = this.state.getVertex(this.rootVertexId);\n if (!rootVertex) {\n throw new Error(\"Root vertex not found\");\n }\n\n return new Vertex(this, rootVertex);\n }\n\n newVertex(parentId: string, props: Record<string, VertexPropertyType> | object | null = null): Vertex {\n const typedProps = props as Record<string, VertexPropertyType> | null;\n const vertexId = this.newVertexInternalWithUUID(parentId);\n if (typedProps) {\n this.setVertexProperties(vertexId, typedProps);\n }\n\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n throw new Error('Failed to create vertex');\n }\n return new Vertex(this, vertex);\n }\n\n newNamedVertex(parentId: string, name: string, props: Record<string, VertexPropertyType> | object | null = null): Vertex {\n const typedProps = props as Record<string, VertexPropertyType> | null;\n const vertexId = this.newVertexInternalWithUUID(parentId);\n if (typedProps) {\n this.setVertexProperties(vertexId, typedProps);\n }\n this.setVertexProperty(vertexId, 'name', name);\n\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n throw new Error('Failed to create named vertex');\n }\n return new Vertex(this, vertex);\n }\n\n moveVertex(vertexId: string, parentId: string) {\n this.lamportClock++;\n const op = newMoveVertexOp(this.lamportClock, this.peerId, vertexId, parentId);\n this.localOps.push(op);\n this.applyMove(op);\n }\n\n deleteVertex(vertexId: string) {\n this.moveVertex(vertexId, RepTree.NULL_VERTEX_ID);\n }\n\n setTransientVertexProperty(vertexId: string, key: string, value: VertexPropertyType) {\n this.lamportClock++;\n const op = newSetTransientVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value as VertexPropertyType);\n this.localOps.push(op);\n this.applyProperty(op);\n }\n\n /**\n * Promotes all transient (temporary) properties to persistent properties.\n * @param vertexId - The ID of the vertex to commit transients for.\n * @returns \n */\n commitTransients(vertexId: string) {\n const vertex = this.state.getVertex(vertexId);\n if (!vertex) {\n return;\n }\n\n const transientProps = vertex.getTransientProperties();\n \n // Promote each transient property to persistent\n for (const prop of transientProps) {\n this.setVertexProperty(vertexId, prop.key, prop.value);\n }\n\n // Clear transient OpIds tracking\n for (const prop of transientProps) {\n this.transientPropertiesAndTheirOpIds.delete(`${prop.key}@${vertexId}`);\n }\n\n // Clear all transient properties from the vertex\n vertex.clearAllTransientProperties();\n }\n\n setVertexProperty(vertexId: string, key: string, value: VertexPropertyType) {\n this.lamportClock++;\n const op = newSetVertexPropertyOp(this.lamportClock, this.peerId, vertexId, key, value as VertexPropertyType);\n this.localOps.push(op);\n this.applyProperty(op);\n }\n\n setVertexProperties(vertexId: string, props: Record<string, VertexPropertyType> | object) {\n const typedProps = props as Record<string, VertexPropertyType>;\n for (const [key, value] of Object.entries(typedProps)) {\n this.setVertexProperty(vertexId, key, value);\n }\n }\n\n getVertexByPath(path: string): Vertex | undefined {\n // Let's remove '/' at the start and at the end of the path\n path = path.replace(/^\\/+/, '');\n path = path.replace(/\\/+$/, '');\n\n const pathParts = path.split('/');\n\n if (!this.rootVertexId) {\n return undefined;\n }\n\n const root = this.state.getVertex(this.rootVertexId);\n if (!root) {\n throw new Error('The root vertex is not found');\n }\n\n const vertex = this.getVertexByPathArray(new Vertex(this, root), pathParts);\n return vertex;\n }\n\n private getVertexByPathArray(vertex: Vertex, path: string[]): Vertex | undefined {\n if (path.length === 0) {\n return vertex ?? undefined;\n }\n\n const targetName = path[0];\n // Now, search recursively by name 'name' in children until the path is empty or not found.\n const children = this.getChildren(vertex.id);\n for (const child of children) {\n if (child.getProperty('name') === targetName) {\n return this.getVertexByPathArray(child, path.slice(1));\n }\n }\n\n return undefined;\n }\n\n printTree() {\n if (!this.rootVertexId) {\n return '';\n }\n\n return this.state.printTree(this.rootVertexId);\n }\n\n merge(ops: ReadonlyArray<VertexOperation>) {\n /*\n if (ops.length > 100) {\n this.applyOpsOptimizedForLotsOfMoves(ops);\n } else {\n this.applyOps(ops);\n }\n */\n\n this.applyOps(ops);\n }\n\n private applyOps(ops: ReadonlyArray<VertexOperation>) {\n for (const op of ops) {\n // We skip the operation if we already know about it.\n // This is to avoid processing the same operation multiple times.\n if (this.knownOps.has(opIdToString(op.id))) {\n continue;\n }\n\n this.applyOperation(op);\n }\n }\n\n /** Applies operations in an optimized way, sorting move ops by OpId to avoid undo-do-redo cycles */\n private applyOpsOptimizedForLotsOfMoves(ops: ReadonlyArray<VertexOperation>) {\n const newMoveOps = ops.filter(op => isMoveVertexOp(op) && !this.knownOps.has(opIdToString(op.id)));\n if (newMoveOps.length > 0) {\n // Get an array of all move ops (without already applied ones)\n const allMoveOps = [...this.moveOps, ...newMoveOps] as MoveVertex[];\n // The main point of this optimization is to apply the moves without undo-do-redo cycles (the conflict resolution algorithm).\n // That is why we sort by OpId.\n allMoveOps.sort((a, b) => compareOpId(a.id, b.id));\n for (let i = 0, len = allMoveOps.length; i < len; i++) {\n const op = allMoveOps[i];\n this.applyMove(op);\n }\n }\n\n // Get an array of all property ops (without already applied ones)\n const propertyOps = ops.filter(op => isAnyPropertyOp(op) && !this.knownOps.has(opIdToString(op.id)));\n for (let i = 0, len = propertyOps.length; i < len; i++) {\n const op = propertyOps[i];\n this.applyProperty(op as SetVertexProperty);\n }\n }\n\n compareStructure(other: RepTree): boolean {\n if (this.root?.id !== other.root?.id) {\n return false;\n }\n\n if (!this.rootVertexId) {\n return true;\n }\n\n return RepTree.compareVertices(this.rootVertexId, this, other);\n }\n\n compareMoveOps(other: RepTree): boolean {\n const movesA = this.moveOps;\n const movesB = other.getMoveOps();\n\n if (movesA.length !== movesB.length) {\n return false;\n }\n\n for (let i = 0; i < movesA.length; i++) {\n if (!equalsOpId(movesA[i].id, movesB[i].id)) {\n return false;\n }\n }\n\n return true;\n }\n\n /** Checks if the given `ancestorId` is an ancestor of `childId` in the tree */\n isAncestor(childId: string, ancestorId: string | null): boolean {\n let targetId = childId;\n let vertex: VertexState | undefined;\n \n // Set to track visited vertices and detect cycles\n const visitedVertices = new Set<string>();\n\n while (vertex = this.state.getVertex(targetId)) {\n if (vertex.parentId === ancestorId) return true;\n if (!vertex.parentId) return false;\n \n // If we've already visited this vertex, we have a cycle\n if (visitedVertices.has(targetId)) {\n console.error(`isAncestor: cycle detected in the tree structure.`);\n // In the context of tryToMove, we should return false here to prevent the move\n // since the target ancestor isn't actually in the path (we're in a cycle)\n return false;\n }\n \n // Mark this vertex as visited\n visitedVertices.add(targetId);\n \n targetId = vertex.parentId;\n }\n\n return false;\n }\n\n observeVertex(vertexId: string, callback: (updatedVertex: Vertex) => void): () => void {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n callback(vertex);\n }\n\n const unsubscribe = this.observe(vertexId, (_) => {\n const vertex = this.getVertex(vertexId);\n if (vertex) {\n callback(vertex);\n }\n });\n\n return () => {\n unsubscribe();\n };\n }\n\n observeVertexMove(callback: (movedVertex: Vertex, isNew: boolean) => void): () => void {\n const listener = (events: VertexChangeEvent[]) => {\n const moveEvent = events.find(e => e.type === 'move') as VertexMoveEvent | undefined;\n if (moveEvent) {\n const vertex = this.getVertex(moveEvent.vertexId);\n if (vertex) {\n callback(vertex, moveEvent.oldParentId === undefined);\n }\n }\n };\n\n this.state.addGlobalChangeCallback(listener);\n\n return () => this.state.removeGlobalChangeCallback(listener);\n }\n\n observe(vertexId: string, callback: (events: VertexChangeEvent[]) => void): () => void {\n this.state.addChangeCallback(vertexId, callback);\n return () => this.state.removeChangeCallback(vertexId, callback);\n }\n\n observeOpApplied(callback: (op: VertexOperation) => void): () => void {\n this.opAppliedCallbacks.push(callback);\n return () => this.opAppliedCallbacks = this.opAppliedCallbacks.filter(l => l !== callback);\n }\n\n static compareVertices(vertexId: string, treeA: RepTree, treeB: RepTree): boolean {\n const childrenA = treeA.state.getChildrenIds(vertexId);\n const childrenB = treeB.state.getChildrenIds(vertexId);\n\n if (childrenA.length !== childrenB.length) {\n return false;\n }\n\n // Compare properties of the current vertex\n if (vertexId !== null) {\n const propertiesA = treeA.getVertexProperties(vertexId);\n const propertiesB = treeB.getVertexProperties(vertexId);\n\n if (propertiesA.length !== propertiesB.length) {\n return false;\n }\n\n for (const propA of propertiesA) {\n const propB = propertiesB.find(p => p.key === propA.key);\n if (!propB) {\n return false;\n }\n \n if (propA.value !== propB.value) {\n return false;\n }\n }\n }\n\n // Compare children and their properties recursively\n for (const childId of childrenA) {\n if (!childrenB.includes(childId)) {\n return false;\n }\n\n if (!RepTree.compareVertices(childId, treeA, treeB)) {\n return false;\n }\n }\n\n return true;\n }\n\n private newVertexInternal(vertexId: string, parentId: string | null): string {\n this.lamportClock++;\n // To create a vertex - we move a vertex with a fresh id under the parent.\n // No need to have a separate \"create vertex\" operation.\n const op = newMoveVertexOp(this.lamportClock, this.peerId, vertexId, parentId);\n this.localOps.push(op);\n this.applyMove(op);\n\n // Set the creation date\n this.setVertexProperty(vertexId, '_c', new Date().toISOString());\n\n return vertexId;\n }\n\n private newVertexInternalWithUUID(parentId: string | null): string {\n const vertexId = uuid();\n return this.newVertexInternal(vertexId, parentId);\n }\n\n private ensureNullVertex() {\n const vertexId = RepTree.NULL_VERTEX_ID;\n\n // Check if the null vertex already exists\n if (this.state.getVertex(vertexId)) {\n return;\n }\n\n this.newVertexInternal(vertexId, null);\n }\n\n /** Updates the lamport clock with the counter value of the operation */\n private updateLamportClock(operation: VertexOperation): void {\n // This is how Lamport clock updates with a foreign operation that has a greater counter value.\n if (operation.id.counter > this.lamportClock) {\n this.lamportClock = operation.id.counter;\n }\n }\n\n private applyPendingMovesForParent(parentId: string) {\n // If a parent doesn't exist, we can't apply pending moves yet.\n if (!this.state.getVertex(parentId)) {\n return;\n }\n\n const pendingMoves = this.pendingMovesWithMissingParent.get(parentId);\n if (!pendingMoves) {\n return;\n }\n\n this.pendingMovesWithMissingParent.delete(parentId);\n\n for (const pendingOp of pendingMoves) {\n this.applyMove(pendingOp);\n }\n }\n\n private applyMove(op: MoveVertex) {\n // Check if a parent (unless we're dealing with the root vertex) exists for the move operation.\n // If it doesn't exist, stash the move op for later\n if (op.parentId !== null && !this.state.getVertex(op.parentId)) {\n if (!this.pendingMovesWithMissingParent.has(op.parentId)) {\n this.pendingMovesWithMissingParent.set(op.parentId, []);\n }\n this.pendingMovesWithMissingParent.get(op.parentId)!.push(op);\n return;\n }\n\n this.updateLamportClock(op);\n\n const lastOp = this.moveOps.length > 0 ? this.moveOps[this.moveOps.length - 1] : null;\n\n // If it's the most recent move operation - just try to move it. No conflict resolution is needed.\n if (lastOp === null || isOpIdGreaterThan(op.id, lastOp.id)) {\n this.moveOps.push(op);\n this.reportOpAsApplied(op);\n this.tryToMove(op);\n }\n\n // Here comes the core of the 'THE REPLICATED TREE ALGORITHM'.\n // From https://martin.kleppmann.com/papers/move-op.pdf\n // We undo all moves that are newer (based on the Lamport clock) than the target move, do the move, and then redo the moves we just undid.\n // The algorithm ensures that all replicas converge to the same tree after applying all operations.\n // The replicas are basically forced to apply the moves in the same order (by undo-do-redo).\n // So if a conflict or a cycle is introduced by some of the peers - the algorithm will resolve it.\n // tryToMove function has the logic to detect cycles and will ignore the move if it creates a cycle. \n else {\n let targetIndex = this.moveOps.length;\n for (let i = this.moveOps.length - 1; i >= 0; i--) {\n const moveOp = this.moveOps[i];\n targetIndex = i;\n if (isOpIdGreaterThan(op.id, moveOp.id)) {\n break;\n }\n else {\n this.undoMove(moveOp);\n }\n }\n\n // Insert the op at the correct position\n this.moveOps.splice(targetIndex + 1, 0, op);\n this.reportOpAsApplied(op);\n this.tryToMove(op);\n\n // Redo all of the operations after the operation that we applied\n for (let i = targetIndex + 2; i < this.moveOps.length; i++) {\n this.tryToMove(this.moveOps[i]);\n }\n }\n\n // After applying the move, check if it unblocks any pending moves\n // We use targetId here because this vertex might now be a parent for pending operations\n this.applyPendingMovesForParent(op.targetId);\n }\n\n private setLLWPropertyAndItsOpId(op: SetVertexProperty) {\n this.propertiesAndTheirOpIds.set(`${op.key}@${op.targetId}`, op.id);\n this.state.setProperty(op.targetId, op.key, op.value);\n this.reportOpAsApplied(op);\n }\n\n private setTransientPropertyAndItsOpId(op: SetVertexProperty) {\n this.transientPropertiesAndTheirOpIds.set(`${op.key}@${op.targetId}`, op.id);\n this.state.setTransientProperty(op.targetId, op.key, op.value);\n this.reportOpAsApplied(op);\n }\n\n private applyProperty(op: SetVertexProperty) {\n const targetVertex = this.state.getVertex(op.targetId);\n if (!targetVertex) {\n // No need to handle transient properties if the vertex doesn't exist\n if (op.transient) {\n return;\n }\n\n // If the vertex doesn't exist, we will wait for the move operation to appear that will create the vertex\n // so we can apply the property then.\n if (!this.pendingPropertiesWithMissingVertex.has(op.targetId)) {\n this.pendingPropertiesWithMissingVertex.set(op.targetId, []);\n }\n this.pendingPropertiesWithMissingVertex.get(op.targetId)!.push(op);\n return;\n }\n\n this.updateLamportClock(op);\n\n this.applyLLWProperty(op, targetVertex);\n }\n\n private applyLLWProperty(op: SetVertexProperty, targetVertex: VertexState) {\n const prevTransientOpId = this.transientPropertiesAndTheirOpIds.get(`${op.key}@${op.targetId}`);\n const prevOpId = this.propertiesAndTheirOpIds.get(`${op.key}@${op.targetId}`);\n\n if (!op.transient) {\n this.setPropertyOps.push(op);\n\n // Apply the property if it's not already applied or if the current op is newer\n // This is the last writer wins approach that ensures the same state between replicas.\n if (!prevOpId || isOpIdGreaterThan(op.id, prevOpId)) {\n this.setLLWPropertyAndItsOpId(op);\n } else {\n // We add it to set of known ops to avoid adding them to `setPropertyOps` multiple times \n // if we ever receive the same op from another peer.\n this.knownOps.add(opIdToString(op.id));\n }\n\n // Remove the transient property if the current op is greater\n if (prevTransientOpId && isOpIdGreaterThan(op.id, prevTransientOpId)) {\n this.transientPropertiesAndTheirOpIds.delete(`${op.key}@${op.targetId}`);\n targetVertex.removeTransientProperty(op.key);\n }\n\n } else {\n // Handle transient properties\n if (!prevTransientOpId || isOpIdGreaterThan(op.id, prevTransientOpId)) {\n this.setTransientPropertyAndItsOpId(op);\n }\n }\n }\n\n // Non-LWW modify-property flow removed\n\n private applyOperation(op: VertexOperation) {\n if (isMoveVertexOp(op)) {\n this.applyMove(op);\n } else if (isAnyPropertyOp(op)) {\n this.applyProperty(op);\n }\n }\n\n private reportOpAsApplied(op: VertexOperation) {\n this.knownOps.add(opIdToString(op.id));\n\n if (this._stateVectorEnabled) {\n this.stateVector.updateFromOp(op);\n }\n\n for (const callback of this.opAppliedCallbacks) {\n callback(op);\n }\n }\n\n private tryToMove(op: MoveVertex) {\n let targetVertex = this.state.getVertex(op.targetId);\n\n if (targetVertex) {\n // We cache the parentId before the move operation.\n // We will use it to undo the move according to the move op algorithm.\n this.parentIdBeforeMove.set(op.id, targetVertex.parentId);\n }\n\n // If trying to move the target vertex under itself - do nothing\n if (op.targetId === op.parentId) return;\n\n // If we try to move the vertex (op.targetId) under one of its descendants (op.parentId) - do nothing\n if (op.parentId && this.isAncestor(op.parentId, op.targetId)) return;\n\n this.state.moveVertex(op.targetId, op.parentId);\n\n // If the vertex didn't exist before the move - see if it has pending properties\n // and apply them.\n if (!targetVertex) {\n const pendingProperties = this.pendingPropertiesWithMissingVertex.get(op.targetId) || [];\n this.pendingPropertiesWithMissingVertex.delete(op.targetId);\n for (const prop of pendingProperties) {\n this.applyProperty(prop);\n }\n }\n }\n\n private undoMove(op: MoveVertex) {\n const targetVertex = this.state.getVertex(op.targetId);\n if (!targetVertex) {\n console.error(`An attempt to undo move operation ${opIdToString(op.id)} failed because the target vertex ${op.targetId} not found`);\n return;\n }\n\n const prevParentId = this.parentIdBeforeMove.get(op.id);\n if (prevParentId === undefined) {\n return;\n }\n\n this.state.moveVertex(op.targetId, prevParentId);\n }\n\n // --- Range-Based State Vector Methods --- \n\n /**\n * Returns the current state vector.\n * Returns a readonly reference to the internal state vector.\n */\n getStateVector(): Readonly<Record<string, number[][]>> | null {\n if (!this._stateVectorEnabled) {\n return null;\n }\n return this.stateVector.getState();\n }\n\n /**\n * Determines which operations are needed to synchronize \n * with the provided state vector.\n * \n * @param theirStateVector The state vector from another peer\n * @returns Operations that should be sent to the other peer, sorted by OpId.\n */\n getMissingOps(theirStateVector: Record<string, number[][]>): VertexOperation[] {\n // If state vector is disabled, fallback to sending all ops\n if (!this._stateVectorEnabled) {\n return [...this.moveOps, ...this.setPropertyOps];\n }\n\n // Create a StateVector instance from their state vector\n const otherStateVector = new StateVector(theirStateVector);\n\n // Get the missing ranges\n const missingRanges = this.stateVector.diff(otherStateVector);\n\n // Then, retrieve only the operations that fall within those ranges\n const missingOps: VertexOperation[] = [];\n // Combine moveOps and setPropertyOps for checking\n const allOps = [...this.moveOps, ...this.setPropertyOps];\n\n // Only check operations that might be in the missing ranges\n for (const op of allOps) {\n for (const range of missingRanges) {\n if (op.id.peerId === range.peerId &&\n op.id.counter >= range.start &&\n op.id.counter <= range.end) {\n missingOps.push(op);\n break; // Move to the next op once found in a missing range\n }\n }\n }\n\n // Sort the missing ops by OpId before returning, ensuring causal order\n missingOps.sort((a, b) => compareOpId(a.id, b.id));\n\n return missingOps;\n }\n\n /**\n * Gets or sets whether state vector tracking is enabled\n */\n get stateVectorEnabled(): boolean {\n return this._stateVectorEnabled;\n }\n\n /**\n * Sets the state vector enabled status\n * When enabled, rebuilds the state vector from existing operations if needed\n */\n set stateVectorEnabled(value: boolean) {\n if (value === this._stateVectorEnabled) return;\n\n if (value) {\n // Enable state vector and rebuild from existing operations\n this._stateVectorEnabled = true;\n this.stateVector = StateVector.fromOperations([...this.moveOps, ...this.setPropertyOps]);\n } else {\n // Disable state vector and clear it to save memory\n this._stateVectorEnabled = false;\n this.stateVector = new StateVector();\n }\n }\n\n /**\n * Parses the vertex properties with a provided schema that has a `parse` method (e.g., Zod schema)\n */\n parseVertex<T>(vertexId: string, schema: { parse: (data: unknown) => T }): T {\n const propsArray = this.getVertexProperties(vertexId);\n const propsObject: Record<string, unknown> = {};\n for (const { key, value } of propsArray) {\n propsObject[key] = value as unknown;\n }\n return schema.parse(propsObject);\n }\n}"],"mappings":";AAKO,SAAS,WAAW,SAAiB,QAAsB;AAChE,SAAO,EAAE,SAAS,OAAO;AAC3B;AAEO,SAAS,YAAY,OAAsB,OAA8B;AAC9E,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,UAAU,gBAAgB,KAAK;AACrC,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,wBAAwB,KAAK,EAAE;AAC7D,YAAQ;AAAA,EACV;AACA,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,UAAU,gBAAgB,KAAK;AACrC,QAAI,CAAC,QAAS,OAAM,IAAI,MAAM,wBAAwB,KAAK,EAAE;AAC7D,YAAQ;AAAA,EACV;AAEA,QAAM,WAAW,MAAM;AACvB,QAAM,WAAW,MAAM;AAEvB,MAAI,WAAW,UAAU;AACvB,WAAO;AAAA,EACT,WAAW,WAAW,UAAU;AAC9B,WAAO;AAAA,EACT,OAAO;AACL,WAAO,MAAM,OAAO,cAAc,MAAM,MAAM;AAAA,EAChD;AACF;AAEO,SAAS,WAAW,OAA6B,OAAsC;AAC5F,MAAI,UAAU,OAAO;AACnB,WAAO;AAAA,EACT,WAAW,CAAC,SAAS,CAAC,OAAO;AAC3B,WAAO;AAAA,EACT;AAEA,SAAO,YAAY,OAAO,KAAK,MAAM;AACvC;AAEO,SAAS,gBAAgB,SAAuB;AACrD,QAAM,QAAQ,QAAQ,MAAM,GAAG;AAE/B,MAAI,MAAM,WAAW,GAAG;AACtB,UAAM,IAAI,MAAM,wBAAwB,OAAO,EAAE;AAAA,EACnD;AAEA,SAAO,WAAW,SAAS,MAAM,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,CAAC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