UNPKG

@itwin/presentation-hierarchies

Version:

A package for creating hierarchies based on data in iTwin.js iModels.

84 lines 3.3 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.HierarchyCache = void 0; const core_bentley_1 = require("@itwin/core-bentley"); const HierarchyNodeKey_js_1 = require("../HierarchyNodeKey.js"); /** @internal */ class HierarchyCache { _map; _props; constructor(props) { this._props = props; this._map = new core_bentley_1.LRUDictionary(props.size, compareHierarchyNodeKeys); } createCacheKeys(props) { function createVariationKey() { const { instanceFilter, hierarchyLevelSizeLimit, filteredInstanceKeys } = props; if (instanceFilter === undefined && hierarchyLevelSizeLimit === undefined && filteredInstanceKeys === undefined) { return undefined; } return JSON.stringify({ instanceFilter, hierarchyLevelSizeLimit, filteredInstanceKeys }); } if (!props.parentNode) { return { primaryKey: [], variationKey: createVariationKey() }; } const parentKeys = props.parentNode.parentKeys; return { primaryKey: [...parentKeys, props.parentNode.key], variationKey: createVariationKey(), }; } getCacheAccessors(primaryKey, variationKey) { const getMapEntry = (create) => { let entry = this._map.get(primaryKey); if (!entry && create) { entry = { primary: undefined, variations: new core_bentley_1.LRUMap(this._props.variationsCount ?? 1) }; this._map.set(primaryKey, entry); } return entry; }; if (variationKey) { return { get: () => getMapEntry(false)?.variations.get(variationKey), set: (value) => { getMapEntry(true).variations.set(variationKey, value); }, }; } return { get: () => getMapEntry(false)?.primary, set: (value) => { getMapEntry(true).primary = value; }, }; } set(requestProps, value) { const { primaryKey, variationKey } = this.createCacheKeys(requestProps); this.getCacheAccessors(primaryKey, variationKey).set(value); } get(requestProps) { const { primaryKey, variationKey } = this.createCacheKeys(requestProps); return this.getCacheAccessors(primaryKey, variationKey).get(); } clear() { this._map.clear(); } } exports.HierarchyCache = HierarchyCache; function compareHierarchyNodeKeys(lhs, rhs) { if (lhs.length !== rhs.length) { return lhs.length - rhs.length; } for (let i = 0; i < lhs.length; ++i) { const keysCompareResult = HierarchyNodeKey_js_1.HierarchyNodeKey.compare(lhs[i], rhs[i]); if (keysCompareResult !== 0) { return keysCompareResult; } } return 0; } //# sourceMappingURL=HierarchyCache.js.map