From ec2a68a25ad30e273e806ca5d1e2393d2c3d6f7f Mon Sep 17 00:00:00 2001 From: Nils Petter Fremming <35219649+nilscognite@users.noreply.github.com> Date: Mon, 24 Feb 2025 06:23:12 +0100 Subject: [PATCH] refactor(react-components): Make ThreeViewFactory a little bit better --- .../base/views/ThreeViewFactory.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/react-components/src/architecture/base/views/ThreeViewFactory.ts b/react-components/src/architecture/base/views/ThreeViewFactory.ts index 808761bb41..44230f3ede 100644 --- a/react-components/src/architecture/base/views/ThreeViewFactory.ts +++ b/react-components/src/architecture/base/views/ThreeViewFactory.ts @@ -15,12 +15,10 @@ type ThreeViewCreator = () => ThreeView; * @returns `true` if a Three.js view can be created, otherwise `false`. */ export function canCreateThreeView(domainObject: DomainObject): boolean { - let domainObjectType: Class | undefined = getClassOf(domainObject); - while (domainObjectType !== undefined) { + for (const domainObjectType of getClassesOf(domainObject)) { if (_products.has(domainObjectType)) { return true; } - domainObjectType = getSuperClassOf(domainObjectType); } return false; } @@ -32,13 +30,11 @@ export function canCreateThreeView(domainObject: DomainObject): boolean { * @returns A ThreeView instance if creation is successful, otherwise undefined. */ export function createThreeView(domainObject: DomainObject): ThreeView | undefined { - let domainObjectType: Class | undefined = getClassOf(domainObject); - while (domainObjectType !== undefined) { + for (const domainObjectType of getClassesOf(domainObject)) { const product = _products.get(domainObjectType); if (product !== undefined) { return product(); } - domainObjectType = getSuperClassOf(domainObjectType); } return undefined; } @@ -71,10 +67,22 @@ export function installThreeViewCreator( const _products = new Map, ThreeViewCreator>(); -function getClassOf(domainObject: DomainObject): Class { - return domainObject.constructor as Class; -} +export function* getClassesOf(domainObject: DomainObject): Generator> { + let classType = getClassOf(domainObject); + while (true) { + yield classType; + const superClassType = getSuperClassOf(classType); + if (superClassType === undefined) { + return; + } + classType = superClassType; + } -function getSuperClassOf(domainObjectType: Class): Class | undefined { - return Object.getPrototypeOf(domainObjectType.prototype)?.constructor ?? undefined; + function getClassOf(domainObject: DomainObject): Class { + return domainObject.constructor as Class; + } + + function getSuperClassOf(domainObjectType: Class): Class | undefined { + return Object.getPrototypeOf(domainObjectType.prototype)?.constructor ?? undefined; + } }