@itwin/presentation-hierarchies
Version:
A package for creating hierarchies based on data in iTwin.js iModels.
77 lines • 3.07 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 { LRUDictionary, LRUMap } from "@itwin/core-bentley";
import { HierarchyNodeKey } from "../HierarchyNodeKey.js";
/** @internal */
export class HierarchyCache {
_map;
_props;
constructor(props) {
this._props = props;
this._map = new 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 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();
}
}
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.compare(lhs[i], rhs[i]);
if (keysCompareResult !== 0) {
return keysCompareResult;
}
}
return 0;
}
//# sourceMappingURL=HierarchyCache.js.map