ui: add objectRegistry for memory management

This commit is contained in:
Johannes Kirschbauer
2025-07-29 10:00:12 +02:00
parent a6a25075f7
commit 686976a143

View File

@@ -0,0 +1,43 @@
import * as THREE from "three";
type ObjectEntry = {
object: THREE.Object3D;
type: string;
id: string;
dispose?: () => void;
};
export class ObjectRegistry {
#objects = new Map<string, ObjectEntry>();
add(entry: ObjectEntry) {
const key = `${entry.type}:${entry.id}`;
this.#objects.set(key, entry);
}
getById(type: string, id: string) {
return this.#objects.get(`${type}:${id}`);
}
getAllByType(type: string) {
return [...this.#objects.values()].filter((obj) => obj.type === type);
}
removeById(type: string, id: string, scene: THREE.Scene) {
const key = `${type}:${id}`;
const entry = this.#objects.get(key);
if (entry) {
scene.remove(entry.object);
entry.dispose?.();
this.#objects.delete(key);
}
}
disposeAll(scene: THREE.Scene) {
for (const entry of this.#objects.values()) {
scene.remove(entry.object);
entry.dispose?.();
}
this.#objects.clear();
}
}