@itwin/presentation-backend
Version:
Backend of iTwin.js Presentation library
161 lines • 8.54 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDefaultNativePlatform = exports.PresentationNativePlatformResponseError = exports.NativePresentationUnitSystem = exports.NativePlatformRequestTypes = void 0;
const core_backend_1 = require("@itwin/core-backend");
const core_bentley_1 = require("@itwin/core-bentley");
const presentation_common_1 = require("@itwin/presentation-common");
const PresentationManager_js_1 = require("./PresentationManager.js");
/** @internal */
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 || (exports.NativePlatformRequestTypes = NativePlatformRequestTypes = {}));
/**
* Enumeration of unit systems supported by native presentation manager.
* @internal
*/
var NativePresentationUnitSystem;
(function (NativePresentationUnitSystem) {
NativePresentationUnitSystem["Metric"] = "metric";
NativePresentationUnitSystem["BritishImperial"] = "british-imperial";
NativePresentationUnitSystem["UsCustomary"] = "us-customary";
NativePresentationUnitSystem["UsSurvey"] = "us-survey";
})(NativePresentationUnitSystem || (exports.NativePresentationUnitSystem = NativePresentationUnitSystem = {}));
/** @internal */
class PresentationNativePlatformResponseError extends presentation_common_1.PresentationError {
diagnostics;
constructor(errorResponse) {
(0, core_bentley_1.assert)(!!errorResponse.error);
super(getPresentationStatusFromNativeResponseStatus(errorResponse.error.status), errorResponse.error.message);
this.diagnostics = errorResponse.diagnostics;
}
}
exports.PresentationNativePlatformResponseError = PresentationNativePlatformResponseError;
function getPresentationStatusFromNativeResponseStatus(nativeResponseStatus) {
switch (nativeResponseStatus) {
case 65537 /* IModelJsNative.ECPresentationStatus.InvalidArgument */:
return presentation_common_1.PresentationStatus.InvalidArgument;
case 65538 /* IModelJsNative.ECPresentationStatus.ResultSetTooLarge */:
return presentation_common_1.PresentationStatus.ResultSetTooLarge;
case 1 /* IModelJsNative.ECPresentationStatus.Canceled */:
return presentation_common_1.PresentationStatus.Canceled;
}
return presentation_common_1.PresentationStatus.Error;
}
/** @internal */
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: PresentationManager_js_1.HierarchyCacheMode.Disk, directory: "" };
const defaultFormats = props.defaultFormats ? this.getSerializedDefaultFormatsMap(props.defaultFormats) : {};
this._nativeAddon = new core_backend_1.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 presentation_common_1.PresentationError(presentation_common_1.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 presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.InvalidArgument, "imodel");
}
return imodel[core_backend_1._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));
}
};
};
exports.createDefaultNativePlatform = createDefaultNativePlatform;
//# sourceMappingURL=NativePlatform.js.map