@itwin/measure-tools-react
Version:
Frontend framework and tools for measurements
104 lines • 4.11 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 { IModelConnection } from "@itwin/core-frontend";
import { IModelApp } from "@itwin/core-frontend";
import { SheetMeasurementHelper } from "./SheetMeasurementHelper.js";
export class DrawingDataCache {
constructor() {
this._drawingDataCache = new Map();
this._drawingTypeDataCache = new Map();
this._spatialDataCache = new Map();
this._viewportModelChangedListeners = new Map();
this.setupEvents();
// Populate initial viewports
for (const vp of IModelApp.viewManager)
this.addViewport(vp);
}
static getInstance() {
if (DrawingDataCache._instance === undefined) {
DrawingDataCache._instance = new DrawingDataCache();
}
return DrawingDataCache._instance;
}
setupEvents() {
// If an imodel closes, clear the cache for it
IModelConnection.onClose.addListener((imodel) => {
this._drawingDataCache.delete(imodel);
this._drawingTypeDataCache.delete(imodel);
this._spatialDataCache.delete(imodel);
});
// Listen for new viewports opening
IModelApp.viewManager.onViewOpen.addListener((vp) => {
this.addViewport(vp);
});
// Listen for viewports closing, this also is called when IModelApp shuts down
IModelApp.viewManager.onViewClose.addListener((vp) => {
this.dropViewport(vp);
});
}
addViewport(vp) {
vp.onViewedModelsChanged.addListener((viewport) => {
if (!viewport.view.isSheetView())
return;
void this.querySheetDrawingData(viewport.iModel, viewport.view.id);
});
}
dropViewport(vp) {
const listener = this._viewportModelChangedListeners.get(vp);
if (listener) {
listener();
this._viewportModelChangedListeners.delete(vp);
}
}
getSheetDrawingDataForViewport(vp) {
if (!vp.view.isSheetView())
return [];
const cache = this._drawingDataCache.get(vp.iModel);
if (cache)
return cache.get(vp.view.id) ?? [];
return [];
}
async queryDrawingType(imodel, drawingId) {
let cache = this._drawingTypeDataCache.get(imodel);
if (!cache) {
cache = new Map();
this._drawingTypeDataCache.set(imodel, cache);
}
let drawingType = cache?.get(drawingId);
if (!drawingType) {
this._drawingTypeDataCache.set(imodel, await SheetMeasurementHelper.getDrawingsTypes(imodel));
drawingType = this._drawingTypeDataCache.get(imodel)?.get(drawingId);
}
return drawingType;
}
async querySheetDrawingData(imodel, viewedModelID) {
let cache = this._drawingDataCache.get(imodel);
if (!cache) {
cache = new Map();
this._drawingDataCache.set(imodel, cache);
}
let sheetData = cache.get(viewedModelID);
if (!sheetData) {
sheetData = await SheetMeasurementHelper.getDrawingInfo(imodel, viewedModelID);
cache.set(viewedModelID, sheetData);
}
return sheetData;
}
async querySpatialInfo(imodel, drawing) {
let cache = this._spatialDataCache.get(imodel);
if (!cache) {
cache = new Map();
this._spatialDataCache.set(imodel, cache);
}
let transformProps = cache.get(drawing.id);
if (!transformProps) {
transformProps = await SheetMeasurementHelper.getSpatialInfo(imodel, drawing);
if (transformProps)
cache.set(drawing.id, transformProps);
}
return transformProps;
}
}
//# sourceMappingURL=DrawingTypeDataCache.js.map