@itwin/presentation-shared
Version:
The package contains types and utilities used across different iTwin.js Presentation packages.
49 lines • 2.23 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { LRUMap } from "@itwin/core-bentley";
import { parseFullClassName } from "./Utils.js";
/**
* Creates a new `ECClassHierarchyInspector` that caches results of `derivesFrom` calls.
* @public
*/
export function createCachingECClassHierarchyInspector(props) {
const map = new LRUMap(props.cacheSize ?? 0);
function createCacheKey(derivedClassName, baseClassName) {
return `${derivedClassName}/${baseClassName}`;
}
return {
classDerivesFrom(derivedClassFullName, candidateBaseClassFullName) {
const cacheKey = createCacheKey(derivedClassFullName, candidateBaseClassFullName);
let result = map.get(cacheKey);
if (result === undefined) {
result = Promise.all([getClass(props.schemaProvider, derivedClassFullName), getClass(props.schemaProvider, candidateBaseClassFullName)]).then(async ([derivedClass, baseClass]) => {
const resolvedResult = await derivedClass.is(baseClass);
map.set(cacheKey, resolvedResult);
return resolvedResult;
});
map.set(cacheKey, result);
}
return result;
},
};
}
/**
* Finds a class with the specified full class name using the given `ECSchemaProvider`.
* @throws Error if the schema or class is not found.
* @public
*/
export async function getClass(schemaProvider, fullClassName) {
const { schemaName, className } = parseFullClassName(fullClassName);
const schema = await schemaProvider.getSchema(schemaName);
if (!schema) {
throw new Error(`Schema "${schemaName}" not found.`);
}
const lookupClass = await schema.getClass(className);
if (!lookupClass) {
throw new Error(`Class "${className}" not found in schema "${schemaName}".`);
}
return lookupClass;
}
//# sourceMappingURL=Metadata.js.map