Skip to content

AI-native design editor. Open-source Figma alternative.

fix(scene-graph): let generated IDs walk past large imported graphs (#915)

Danila Poyarkovb834c5323faf
diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.ts
index 085660878..6f2ce4d86 100644
--- a/packages/scene-graph/src/index.ts
+++ b/packages/scene-graph/src/index.ts
@@ -122,9 +122,6 @@ function stripUndefinedProps<T extends object>(obj: T): T {
export { captureGraphCheckpoint } from './checkpoint'
-/** How many taken IDs a generator may return in a row before it counts as exhausted. */
-const MAX_ID_ATTEMPTS = 1000
-
export class SceneGraph {
nodes = new Map<string, SceneNode>()
images = new Map<string, Uint8Array>()
@@ -368,15 +365,28 @@ export class SceneGraph {
height: node?.height ?? 0
}
}
- /** An ID no entity uses; `issued` also excludes IDs handed out earlier in the same creation. */
+ /**
+ * An ID no entity uses; `issued` also excludes IDs handed out earlier in the same creation.
+ * A generator that returns more taken IDs than the graph holds must be repeating itself, so
+ * it counts as exhausted then; a fixed cap would reject a sequence walking past a large import.
+ */
private generateEntityId(issued?: Set<string>): string {
- for (let attempt = 0; attempt < MAX_ID_ATTEMPTS; attempt++) {
+ let limit = Infinity
+ for (let attempt = 0; attempt < limit; attempt++) {
const id = this.idGenerator()
- if (this.isEntityIdTaken(id) || issued?.has(id)) continue
+ if (this.isEntityIdTaken(id) || issued?.has(id)) {
+ if (limit === Infinity) limit = this.entityIdCount() + (issued?.size ?? 0) + 1
+ continue
+ }
issued?.add(id)
return id
}
- throw new Error(`The ID generator returned ${MAX_ID_ATTEMPTS} IDs in a row that are in use`)
+ throw new Error('The ID generator only returned IDs that are in use')
+ }
+ private entityIdCount(): number {
+ let count = this.nodes.size + this.variables.size + this.variableCollections.size
+ for (const collection of this.variableCollections.values()) count += collection.modes.length
+ return count
}
private isEntityIdTaken(id: string): boolean {
if (this.nodes.has(id) || this.variables.has(id) || this.variableCollections.has(id))
diff --git a/packages/scene-graph/tests/basic/id-generator.test.ts b/packages/scene-graph/tests/basic/id-generator.test.ts
index 1bbe357e2..ee946e0c1 100644
--- a/packages/scene-graph/tests/basic/id-generator.test.ts
+++ b/packages/scene-graph/tests/basic/id-generator.test.ts
@@ -73,7 +73,15 @@ describe('SceneGraph ID generator', () => {
})
test('throws instead of hanging when the generator only returns IDs in use', () => {
- expect(() => new SceneGraph(() => 'same')).toThrow('IDs in a row that are in use')
+ expect(() => new SceneGraph(() => 'same')).toThrow('only returned IDs that are in use')
+ })
+
+ test('walks a sequence past more taken IDs than any fixed cap', () => {
+ const graph = new SceneGraph(sequence('9'))
+ const page = pageId(graph)
+ for (let i = 3; i <= 2002; i++) graph.createNodeWithId(`9:${i}`, 'RECTANGLE', page)
+
+ expect(graph.createNode('RECTANGLE', page).id).toBe('9:2003')
})
test('gives modes added to a collection IDs from the injected generator', () => {