Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measure-tools-react: drawingTypeCache refactor #992

Merged
merged 22 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "drawingTypeCache refactor",
"packageName": "@itwin/measure-tools-react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
93 changes: 73 additions & 20 deletions packages/itwin/measure-tools/src/api/DrawingTypeDataCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,95 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import type { IModelConnection } from "@itwin/core-frontend";
import type { Viewport } from "@itwin/core-frontend";
import { IModelConnection } from "@itwin/core-frontend";
import { IModelApp } from "@itwin/core-frontend";
import { SheetMeasurementsHelper } from "./SheetMeasurementHelper";
import type { Id64String } from "@itwin/core-bentley";

export class DrawingDataCache {
bsy-nicholasw marked this conversation as resolved.
Show resolved Hide resolved

private _drawingTypeCache: SheetMeasurementsHelper.DrawingTypeData[];
private _sheetChangeListener: VoidFunction[] = [];
private _drawingTypeCache: Map<IModelConnection, Map<Id64String, SheetMeasurementsHelper.DrawingTypeData[]>>;
private _viewportModelChangedListeners: Map<Viewport, () => void>;
private static _instance: DrawingDataCache | undefined;

public get drawingtypes(): ReadonlyArray<SheetMeasurementsHelper.DrawingTypeData> {
return this._drawingTypeCache;
private constructor() {
this._drawingTypeCache = new Map<IModelConnection, Map<Id64String, SheetMeasurementsHelper.DrawingTypeData[]>>();
this._viewportModelChangedListeners = new Map<Viewport, () => void>();

this.setupEvents();

// Populate initial viewports
for (const vp of IModelApp.viewManager)
this.addViewport(vp);
}

public constructor() {
this._drawingTypeCache = [];
public static getInstance(): DrawingDataCache {
if (DrawingDataCache._instance === undefined) {
DrawingDataCache._instance = new DrawingDataCache();
}
return DrawingDataCache._instance;
}

public async updateDrawingTypeCache(iModel: IModelConnection) {
this._sheetChangeListener.forEach((func) => {
func();
private setupEvents() {
// If an imodel closes, clear the cache for it
IModelConnection.onClose.addListener((imodel) => {
this._drawingTypeCache.delete(imodel);
});

// Listen for new viewports opening
IModelApp.viewManager.onViewOpen.addListener((vp) => {
this.addViewport(vp);
});
this._sheetChangeListener = [];
this._drawingTypeCache = [];

const sheetIds = new Set<string>();
// Listen for viewports closing, this also is called when IModelApp shuts down
IModelApp.viewManager.onViewClose.addListener((vp) => {
this.dropViewport(vp);
});
}

private addViewport(vp: Viewport) {
vp.onViewedModelsChanged.addListener((viewport) =>{
if (!viewport.view.isSheetView())
return;

for (const viewport of IModelApp.viewManager) {
if (viewport.view.isSheetView()) {
this._sheetChangeListener.push(viewport.onViewedModelsChanged.addListener(async () => this.updateDrawingTypeCache(iModel)));
sheetIds.add(viewport.view.id);
}
void this.querySheetDrawingData(viewport.iModel, viewport.view.id);
});
}

private dropViewport(vp: Viewport) {
const listener = this._viewportModelChangedListeners.get(vp);
if (listener) {
listener();
this._viewportModelChangedListeners.delete(vp);
}
}

public getSheetDrawingDataForViewport(vp: Viewport): ReadonlyArray<SheetMeasurementsHelper.DrawingTypeData> {
if (!vp.view.isSheetView())
return [];

for (const id of sheetIds) {
this._drawingTypeCache = this._drawingTypeCache.concat(await SheetMeasurementsHelper.getSheetTypes(iModel, id));
const cache = this._drawingTypeCache.get(vp.iModel);
if (cache)
return cache.get(vp.view.id) ?? [];

return [];
}

public async querySheetDrawingData(imodel: IModelConnection, viewedModelID: string): Promise<ReadonlyArray<SheetMeasurementsHelper.DrawingTypeData>> {
let cache = this._drawingTypeCache.get(imodel);
if (!cache) {
cache = new Map<Id64String, SheetMeasurementsHelper.DrawingTypeData[]>();
this._drawingTypeCache.set(imodel, cache);
}

let sheetData = cache.get(viewedModelID);
if (!sheetData) {
sheetData = await SheetMeasurementsHelper.getSheetTypes(imodel, viewedModelID);
cache.set(viewedModelID, sheetData);
}

return sheetData;
}

}
13 changes: 3 additions & 10 deletions packages/itwin/measure-tools/src/tools/MeasureAreaTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ MeasureAreaToolModel
public static override toolId = "MeasureTools.MeasureArea";
public static override iconSpec = "icon-measure-2d";
private _enableSheetMeasurements: boolean;
private _drawingTypeCache?: DrawingDataCache;

public static override get flyover() {
return MeasureTools.localization.getLocalizedString(
Expand Down Expand Up @@ -72,10 +71,6 @@ MeasureAreaToolModel

public override async onPostInstall(): Promise<void> {
await super.onPostInstall();
if (this._enableSheetMeasurements) {
this._drawingTypeCache = new DrawingDataCache();
await this._drawingTypeCache.updateDrawingTypeCache(this.iModel);
}
}

public override async onReinitialize(): Promise<void> {
Expand Down Expand Up @@ -155,12 +150,10 @@ MeasureAreaToolModel
return true;
}

if (this._drawingTypeCache) {
for (const drawing of this._drawingTypeCache.drawingtypes) {
for (const drawing of DrawingDataCache.getInstance().getSheetDrawingDataForViewport(ev.viewport)) {
if (drawing.type !== SheetMeasurementsHelper.DrawingType.CrossSection && drawing.type !== SheetMeasurementsHelper.DrawingType.Plan) {
if (SheetMeasurementsHelper.checkIfInDrawing(ev.point, drawing.origin, drawing.extents)) {
if (drawing.type !== SheetMeasurementsHelper.DrawingType.CrossSection && drawing.type !== SheetMeasurementsHelper.DrawingType.Plan) {
return false;
}
return false;
}
}
}
Expand Down
13 changes: 3 additions & 10 deletions packages/itwin/measure-tools/src/tools/MeasureDistanceTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ MeasureDistanceToolModel
public static override toolId = "MeasureTools.MeasureDistance";
public static override iconSpec = "icon-measure-distance";
private _enableSheetMeasurements: boolean;
private _drawingTypeCache?: DrawingDataCache;

public static override get flyover() {
return MeasureTools.localization.getLocalizedString(
Expand Down Expand Up @@ -64,10 +63,6 @@ MeasureDistanceToolModel

public override async onPostInstall(): Promise<void> {
await super.onPostInstall();
if (this._enableSheetMeasurements) {
this._drawingTypeCache = new DrawingDataCache();
await this._drawingTypeCache.updateDrawingTypeCache(this.iModel);
}
}

public async onRestartTool(): Promise<void> {
Expand Down Expand Up @@ -133,12 +128,10 @@ MeasureDistanceToolModel
return true;
}

if (this._drawingTypeCache) {
for (const drawing of this._drawingTypeCache.drawingtypes) {
for (const drawing of DrawingDataCache.getInstance().getSheetDrawingDataForViewport(ev.viewport)) {
if (drawing.type !== SheetMeasurementsHelper.DrawingType.CrossSection && drawing.type !== SheetMeasurementsHelper.DrawingType.Plan) {
if (SheetMeasurementsHelper.checkIfInDrawing(ev.point, drawing.origin, drawing.extents)) {
if (drawing.type !== SheetMeasurementsHelper.DrawingType.CrossSection && drawing.type !== SheetMeasurementsHelper.DrawingType.Plan) {
return false;
}
return false;
}
}
}
Expand Down
13 changes: 3 additions & 10 deletions packages/itwin/measure-tools/src/tools/MeasureLocationTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ MeasureLocationToolModel
public static override iconSpec = "icon-measure-location";
private static readonly useDynamicMeasurementPropertyName = "useDynamicMeasurement";
private _enableSheetMeasurements: boolean;
private _drawingTypeCache?: DrawingDataCache;

private static _isUserNotifiedOfGeolocationFailure = false;
private _useDynamicMeasurement: boolean = false;
Expand Down Expand Up @@ -84,10 +83,6 @@ MeasureLocationToolModel

public override async onPostInstall(): Promise<void> {
await super.onPostInstall();
if (this._enableSheetMeasurements) {
this._drawingTypeCache = new DrawingDataCache();
await this._drawingTypeCache.updateDrawingTypeCache(this.iModel);
}
}

public override async onDataButtonDown(
Expand Down Expand Up @@ -160,12 +155,10 @@ MeasureLocationToolModel
return true;
}

if (this._drawingTypeCache) {
for (const drawing of this._drawingTypeCache.drawingtypes) {
for (const drawing of DrawingDataCache.getInstance().getSheetDrawingDataForViewport(ev.viewport)) {
if (drawing.type !== SheetMeasurementsHelper.DrawingType.CrossSection && drawing.type !== SheetMeasurementsHelper.DrawingType.Plan) {
if (SheetMeasurementsHelper.checkIfInDrawing(ev.point, drawing.origin, drawing.extents)) {
if (drawing.type !== SheetMeasurementsHelper.DrawingType.CrossSection && drawing.type !== SheetMeasurementsHelper.DrawingType.Plan) {
return false;
}
return false;
}
}
}
Expand Down
Loading