UNPKG

@itwin/presentation-shared

Version:

The package contains types and utilities used across different iTwin.js Presentation packages.

53 lines 2.45 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.createCachingECClassHierarchyInspector = createCachingECClassHierarchyInspector; exports.getClass = getClass; const core_bentley_1 = require("@itwin/core-bentley"); const Utils_js_1 = require("./Utils.js"); /** * Creates a new `ECClassHierarchyInspector` that caches results of `derivesFrom` calls. * @public */ function createCachingECClassHierarchyInspector(props) { const map = new core_bentley_1.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 */ async function getClass(schemaProvider, fullClassName) { const { schemaName, className } = (0, Utils_js_1.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