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

Presentation: Test app improvements #1070

Merged
merged 10 commits into from
Mar 31, 2021
31 changes: 31 additions & 0 deletions test-apps/presentation-test-app/src/backend/SampleIpcHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { Id64Arg } from "@bentley/bentleyjs-core";
import { IModelDb, IpcHandler } from "@bentley/imodeljs-backend";
import { ElementProps } from "@bentley/imodeljs-common";
import { PRESENTATION_TEST_APP_IPC_CHANNEL_NAME, SampleIpcInterface } from "../common/SampleIpcInterface";

/** @internal */
export class SampleIpcHandler extends IpcHandler implements SampleIpcInterface {
public channelName = PRESENTATION_TEST_APP_IPC_CHANNEL_NAME;

public async deleteElements(imodelKey: string, elementIds: Id64Arg): Promise<void> {
const iModelDb = IModelDb.tryFindByKey(imodelKey);
if (!iModelDb)
return;

iModelDb.elements.deleteElement(elementIds);
iModelDb.saveChanges();
}

public async updateElement(imodelKey: string, newProps: ElementProps): Promise<void> {
const iModelDb = IModelDb.tryFindByKey(imodelKey);
if (!iModelDb)
return;

iModelDb.elements.updateElement(newProps);
iModelDb.saveChanges();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as path from "path";
import { ElectronHost, ElectronHostOptions } from "@bentley/electron-manager/lib/ElectronBackend";
import { RpcInterfaceDefinition } from "@bentley/imodeljs-common";
import { SampleIpcHandler } from "../SampleIpcHandler";

/**
* Initializes Electron backend
Expand All @@ -18,6 +19,7 @@ export default async function initialize(rpcInterfaces: RpcInterfaceDefinition[]
webResourcesPath: path.join(__dirname, "..", "..", "..", "build"),
rpcInterfaces,
developmentServer: process.env.NODE_ENV === "development",
ipcHandlers: [SampleIpcHandler],
};

await ElectronHost.startup({ electronHost });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { Id64Arg } from "@bentley/bentleyjs-core";
import { ElementProps } from "@bentley/imodeljs-common";

/** @internal */
export const PRESENTATION_TEST_APP_IPC_CHANNEL_NAME = "presentation-test-app-ipc-interface";

/** @internal */
export interface SampleIpcInterface {
updateElement(imodelKey: string, newProps: ElementProps): Promise<void>;
deleteElements(imodelKey: string, elementIds: Id64Arg): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { Guid, Logger } from "@bentley/bentleyjs-core";
import { ViewQueryParams } from "@bentley/imodeljs-common";
import { IModelConnection, SnapshotConnection } from "@bentley/imodeljs-frontend";
import { Guid, Id64Arg, Logger, OpenMode } from "@bentley/bentleyjs-core";
import { ElementProps, IModelError, ViewQueryParams } from "@bentley/imodeljs-common";
import { AsyncMethodsOf, BriefcaseConnection, IModelConnection, IpcApp, PromiseReturnType, SnapshotConnection } from "@bentley/imodeljs-frontend";
import { PRESENTATION_TEST_APP_IPC_CHANNEL_NAME, SampleIpcInterface } from "../../common/SampleIpcInterface";
import SampleRpcInterface from "../../common/SampleRpcInterface";

export class MyAppFrontend {
Expand All @@ -19,8 +20,17 @@ export class MyAppFrontend {
}

public static async openIModel(path: string): Promise<IModelConnection> {
this.iModel = await SnapshotConnection.openFile(path);
Logger.logInfo("presentation", `Opened: ${this.iModel.name}`);
if (IpcApp.isValid) {
Logger.logInfo("presentation", `Trying to open standalone ${path}`);
this.iModel = await tryOpenStandalone(path);
}

if (!this.iModel) {
Logger.logInfo("presentation", `Opening snapshot: ${path}`);
this.iModel = await SnapshotConnection.openFile(path);
Logger.logInfo("presentation", `Opened snapshot: ${this.iModel.name}`);
}

return this.iModel;
}

Expand All @@ -45,4 +55,35 @@ export class MyAppFrontend {
label: spec.userLabel ?? spec.code.value!,
}));
}

public static async updateElement(imodel: IModelConnection, newProps: ElementProps) {
if (!IpcApp.isValid)
return;
return this.callIpc("updateElement", imodel.key, newProps);
}

public static async deleteElements(imodel: IModelConnection, elementIds: Id64Arg) {
if (!IpcApp.isValid)
return;
return this.callIpc("deleteElements", imodel.key, elementIds);
}
grigasp marked this conversation as resolved.
Show resolved Hide resolved

private static async callIpc<T extends AsyncMethodsOf<SampleIpcInterface>>(methodName: T, ...args: Parameters<SampleIpcInterface[T]>): Promise<PromiseReturnType<SampleIpcInterface[T]>> {
return IpcApp.callIpcChannel(PRESENTATION_TEST_APP_IPC_CHANNEL_NAME, methodName, ...args);
}
}

async function tryOpenStandalone(path: string) {
let iModel: IModelConnection | undefined;
try {
iModel = await BriefcaseConnection.openStandalone(path, OpenMode.ReadWrite);
Logger.logInfo("presentation", `Opened standalone: ${iModel.name}`);
} catch (err) {
if (err instanceof IModelError) {
Logger.logError("presentation", `Failed to open standalone: ${err.message}`, err.getMetaData);
} else {
Logger.logError("presentation", `Failed to open standalone.`);
}
}
return iModel;
}