@itwin/presentation-backend
Version:
Backend of iTwin.js Presentation library
156 lines • 7.82 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Core
*/
import { _nativeDb, IModelNative } from "@itwin/core-backend";
import { assert } from "@itwin/core-bentley";
import { PresentationError, PresentationStatus, } from "@itwin/presentation-common";
import { HierarchyCacheMode } from "./PresentationManager.js";
/** @internal */
export var NativePlatformRequestTypes;
(function (NativePlatformRequestTypes) {
NativePlatformRequestTypes["GetRootNodes"] = "GetRootNodes";
NativePlatformRequestTypes["GetRootNodesCount"] = "GetRootNodesCount";
NativePlatformRequestTypes["GetChildren"] = "GetChildren";
NativePlatformRequestTypes["GetChildrenCount"] = "GetChildrenCount";
NativePlatformRequestTypes["GetNodesDescriptor"] = "GetNodesDescriptor";
NativePlatformRequestTypes["GetNodePaths"] = "GetNodePaths";
NativePlatformRequestTypes["GetFilteredNodePaths"] = "GetFilteredNodePaths";
NativePlatformRequestTypes["GetContentSources"] = "GetContentSources";
NativePlatformRequestTypes["GetContentDescriptor"] = "GetContentDescriptor";
NativePlatformRequestTypes["GetContentSetSize"] = "GetContentSetSize";
NativePlatformRequestTypes["GetContentSet"] = "GetContentSet";
NativePlatformRequestTypes["GetContent"] = "GetContent";
NativePlatformRequestTypes["GetPagedDistinctValues"] = "GetPagedDistinctValues";
NativePlatformRequestTypes["GetDisplayLabel"] = "GetDisplayLabel";
NativePlatformRequestTypes["CompareHierarchies"] = "CompareHierarchies";
})(NativePlatformRequestTypes || (NativePlatformRequestTypes = {}));
/**
* Enumeration of unit systems supported by native presentation manager.
* @internal
*/
export var NativePresentationUnitSystem;
(function (NativePresentationUnitSystem) {
NativePresentationUnitSystem["Metric"] = "metric";
NativePresentationUnitSystem["BritishImperial"] = "british-imperial";
NativePresentationUnitSystem["UsCustomary"] = "us-customary";
NativePresentationUnitSystem["UsSurvey"] = "us-survey";
})(NativePresentationUnitSystem || (NativePresentationUnitSystem = {}));
/** @internal */
export class PresentationNativePlatformResponseError extends PresentationError {
diagnostics;
constructor(errorResponse) {
assert(!!errorResponse.error);
super(getPresentationStatusFromNativeResponseStatus(errorResponse.error.status), errorResponse.error.message);
this.diagnostics = errorResponse.diagnostics;
}
}
function getPresentationStatusFromNativeResponseStatus(nativeResponseStatus) {
switch (nativeResponseStatus) {
case 65537 /* IModelJsNative.ECPresentationStatus.InvalidArgument */:
return PresentationStatus.InvalidArgument;
case 65538 /* IModelJsNative.ECPresentationStatus.ResultSetTooLarge */:
return PresentationStatus.ResultSetTooLarge;
case 1 /* IModelJsNative.ECPresentationStatus.Canceled */:
return PresentationStatus.Canceled;
}
return PresentationStatus.Error;
}
/** @internal */
export const createDefaultNativePlatform = (props) => {
// note the implementation is constructed here to make PresentationManager
// usable without loading the actual addon (if addon is set to something other)
return class {
_nativeAddon;
constructor() {
const cacheConfig = props.cacheConfig ?? { mode: HierarchyCacheMode.Disk, directory: "" };
const defaultFormats = props.defaultFormats ? this.getSerializedDefaultFormatsMap(props.defaultFormats) : {};
this._nativeAddon = new IModelNative.platform.ECPresentationManager({ ...props, cacheConfig, defaultFormats });
}
getSerializedDefaultFormatsMap(defaultMap) {
const res = {};
Object.entries(defaultMap).forEach(([phenomenon, formats]) => {
res[phenomenon] = formats.map((value) => ({ unitSystems: value.unitSystems, serializedFormat: JSON.stringify(value.format) }));
});
return res;
}
createSuccessResponse(response) {
const retValue = { result: response.result };
if (response.diagnostics) {
retValue.diagnostics = response.diagnostics;
}
return retValue;
}
handleResult(response) {
if (response.error) {
throw new PresentationNativePlatformResponseError(response);
}
return this.createSuccessResponse(response);
}
handleConvertedResult(response, conv) {
return this.handleResult((response.result ? { ...response, result: conv(response.result) } : response));
}
handleVoidResult(response) {
if (response.error) {
throw new PresentationNativePlatformResponseError(response);
}
return this.createSuccessResponse(response);
}
[Symbol.dispose]() {
this._nativeAddon.dispose();
}
async forceLoadSchemas(db) {
const response = await this._nativeAddon.forceLoadSchemas(db);
if (response.error) {
throw new PresentationError(PresentationStatus.Error, response.error.message);
}
return this.createSuccessResponse(response);
}
setupRulesetDirectories(directories) {
return this.handleVoidResult(this._nativeAddon.setupRulesetDirectories(directories));
}
setupSupplementalRulesetDirectories(directories) {
return this.handleVoidResult(this._nativeAddon.setupSupplementalRulesetDirectories(directories));
}
getImodelAddon(imodel) {
if (!imodel.isOpen) {
throw new PresentationError(PresentationStatus.InvalidArgument, "imodel");
}
return imodel[_nativeDb];
}
registerSupplementalRuleset(serializedRulesetJson) {
return this.handleResult(this._nativeAddon.registerSupplementalRuleset(serializedRulesetJson));
}
getRulesets(rulesetId) {
return this.handleResult(this._nativeAddon.getRulesets(rulesetId));
}
addRuleset(serializedRulesetJson) {
return this.handleResult(this._nativeAddon.addRuleset(serializedRulesetJson));
}
removeRuleset(rulesetId, hash) {
return this.handleResult(this._nativeAddon.removeRuleset(rulesetId, hash));
}
clearRulesets() {
return this.handleVoidResult(this._nativeAddon.clearRulesets());
}
async handleRequest(db, options, cancelEvent) {
const response = this._nativeAddon.handleRequest(db, options);
cancelEvent?.addOnce(() => response.cancel());
const result = await response.result;
return this.handleConvertedResult(result, (buffer) => buffer.toString());
}
getRulesetVariableValue(rulesetId, variableId, type) {
return this.handleResult(this._nativeAddon.getRulesetVariableValue(rulesetId, variableId, type));
}
setRulesetVariableValue(rulesetId, variableId, type, value) {
return this.handleVoidResult(this._nativeAddon.setRulesetVariableValue(rulesetId, variableId, type, value));
}
unsetRulesetVariableValue(rulesetId, variableId) {
return this.handleVoidResult(this._nativeAddon.unsetRulesetVariableValue(rulesetId, variableId));
}
};
};
//# sourceMappingURL=NativePlatform.js.map