packageDocumentation module import { IModelDb , IModelHost , IModelJsNative } from "@itwin/core-backend" ; import { IDisposable } from "@itwin/core-bentley" ; import { FormatProps } from "@itwin/core-quantity" ; import { DiagnosticsScopeLogs , NodeKeyJSON , PresentationError , PresentationStatus , UpdateInfoJSON , VariableValue , VariableValueJSON , VariableValueTypes , } from "@itwin/presentation-common" ; import { HierarchyCacheMode , PresentationManagerMode } from "./PresentationManager" ; internal export enum NativePlatformRequestTypes { GetRootNodes = "GetRootNodes" , GetRootNodesCount = "GetRootNodesCount" , GetChildren = "GetChildren" , GetChildrenCount = "GetChildrenCount" , GetNodePaths = "GetNodePaths" , GetFilteredNodePaths = "GetFilteredNodePaths" , GetContentSources = "GetContentSources" , GetContentDescriptor = "GetContentDescriptor" , GetContentSetSize = "GetContentSetSize" , GetContent = "GetContent" , GetDistinctValues = "GetDistinctValues" , GetPagedDistinctValues = "GetPagedDistinctValues" , GetDisplayLabel = "GetDisplayLabel" , CompareHierarchies = "CompareHierarchies" } internal export enum NativePresentationUnitSystem { Metric = "metric" , BritishImperial = "british-imperial" , UsCustomary = "us-customary" , UsSurvey = "us-survey" } internal export interface NativePresentationDefaultUnitFormats { [ phenomenon ] : { unitSystems : NativePresentationUnitSystem [ ] ; format : FormatProps ; } ; } internal export interface NativePresentationKeySetJSON { instanceKeys : Array < [ string , string [ ] ] > ; nodeKeys : NodeKeyJSON [ ] ; } internal export interface NativePlatformResponse < TResult > { result : TResult ; diagnostics ? : DiagnosticsScopeLogs ; } internal export interface NativePlatformDefinition extends IDisposable { getImodelAddon ( imodel ) : any ; setupRulesetDirectories ( directories ) : NativePlatformResponse < void > ; setupSupplementalRulesetDirectories ( directories ) : NativePlatformResponse < void > ; forceLoadSchemas ( db ) : Promise < NativePlatformResponse < void > > ; getRulesets ( rulesetId ) : NativePlatformResponse < string > ; addRuleset ( serializedRulesetJson ) : NativePlatformResponse < string > ; removeRuleset ( rulesetId , hash ) : NativePlatformResponse < boolean > ; clearRulesets ( ) : NativePlatformResponse < void > ; handleRequest ( db , options ) : Promise < NativePlatformResponse < string > > ; getRulesetVariableValue ( rulesetId , variableId , type ) : NativePlatformResponse < VariableValue > ; setRulesetVariableValue ( rulesetId , variableId , type , value ) : NativePlatformResponse < void > ; unsetRulesetVariableValue ( rulesetId , variableId ) : NativePlatformResponse < void > ; getUpdateInfo ( ) : NativePlatformResponse < UpdateInfoJSON | undefined > ; updateHierarchyState ( db , rulesetId , changeType , serializedKeys ) : NativePlatformResponse < void > ; } internal export interface DefaultNativePlatformProps { id : string ; localeDirectories : string [ ] ; taskAllocationsMap : { [ priority ] : number ; } ; mode : PresentationManagerMode ; isChangeTrackingEnabled : boolean ; cacheConfig ? : IModelJsNative . ECPresentationHierarchyCacheConfig ; contentCacheSize ? : number ; defaultFormats ? : NativePresentationDefaultUnitFormats ; useMmap ? : boolean | number ; } internal export const createDefaultNativePlatform = ( props ) : new ( ) => NativePlatformDefinition => { return class implements NativePlatformDefinition { private _nativeAddon : IModelJsNative . ECPresentationManager ; public constructor ( ) { const mode = ( props . mode === PresentationManagerMode . ReadOnly ) ? IModelJsNative . ECPresentationManagerMode . ReadOnly : IModelJsNative . ECPresentationManagerMode . ReadWrite ; const cacheConfig = props . cacheConfig ?? { mode : HierarchyCacheMode . Disk , directory : "" } ; const defaultFormats = props . defaultFormats ? this . getSerializedDefaultFormatsMap ( props . defaultFormats ) : { } ; this . _nativeAddon = new IModelHost . platform . ECPresentationManager ( { ... props , mode , cacheConfig , defaultFormats } ) ; } private getStatus ( responseStatus ) { switch ( responseStatus ) { case IModelJsNative . ECPresentationStatus . InvalidArgument : return PresentationStatus . InvalidArgument ; case IModelJsNative . ECPresentationStatus . Canceled : return PresentationStatus . Canceled ; default : return PresentationStatus . Error ; } } private getSerializedDefaultFormatsMap ( defaultMap ) { const res = { } ; Object . keys ( defaultMap ) . forEach ( ( key ) => { const value = defaultMap [ key ] ; res [ key ] = { unitSystems : value . unitSystems , serializedFormat : JSON . stringify ( value . format ) } ; } ) ; return res ; } private createSuccessResponse < T > ( response ) { const retValue = { result : response . result ! } ; if ( response . diagnostics ) retValue . diagnostics = response . diagnostics ; return retValue ; } private handleResult < T > ( response ) { if ( response . error ) throw new PresentationError ( this . getStatus ( response . error . status ) , response . error . message ) ; return this . createSuccessResponse ( response ) ; } private handleVoidResult ( response ) { if ( response . error ) throw new PresentationError ( this . getStatus ( response . error . status ) , response . error . message ) ; return this . createSuccessResponse ( response ) ; } public dispose ( ) { this . _nativeAddon . dispose ( ) ; } public 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 ) ; } public setupRulesetDirectories ( directories ) { return this . handleVoidResult ( this . _nativeAddon . setupRulesetDirectories ( directories ) ) ; } public setupSupplementalRulesetDirectories ( directories ) { return this . handleVoidResult ( this . _nativeAddon . setupSupplementalRulesetDirectories ( directories ) ) ; } public getImodelAddon ( imodel ) { if ( ! imodel . nativeDb ) throw new PresentationError ( PresentationStatus . InvalidArgument , "imodel" ) ; return imodel . nativeDb ; } public getRulesets ( rulesetId ) { return this . handleResult ( this . _nativeAddon . getRulesets ( rulesetId ) ) ; } public addRuleset ( serializedRulesetJson ) { return this . handleResult ( this . _nativeAddon . addRuleset ( serializedRulesetJson ) ) ; } public removeRuleset ( rulesetId , hash ) { return this . handleResult ( this . _nativeAddon . removeRuleset ( rulesetId , hash ) ) ; } public clearRulesets ( ) { return this . handleVoidResult ( this . _nativeAddon . clearRulesets ( ) ) ; } public async handleRequest ( db , options ) { const requestGuid = this . handleResult ( this . _nativeAddon . queueRequest ( db , options ) ) . result ; return new Promise ( ( resolve , reject ) => { const interval = setInterval ( ( ) => { const pollResult = this . _nativeAddon . pollResponse ( requestGuid ) ; if ( pollResult . error ) { if ( pollResult . error . status !== IModelJsNative . ECPresentationStatus . Pending ) { reject ( new PresentationError ( this . getStatus ( pollResult . error . status ) , pollResult . error . message ) ) ; clearInterval ( interval ) ; } return ; } resolve ( this . createSuccessResponse ( pollResult ) ) ; clearInterval ( interval ) ; } , 20 ) ; } ) ; } public getRulesetVariableValue ( rulesetId , variableId , type ) { return this . handleResult ( this . _nativeAddon . getRulesetVariableValue ( rulesetId , variableId , type ) ) ; } public setRulesetVariableValue ( rulesetId , variableId , type , value ) { return this . handleVoidResult ( this . _nativeAddon . setRulesetVariableValue ( rulesetId , variableId , type , value ) ) ; } public unsetRulesetVariableValue ( rulesetId , variableId ) { return this . handleVoidResult ( this . _nativeAddon . unsetRulesetVariableValue ( rulesetId , variableId ) ) ; } public getUpdateInfo ( ) { return this . handleResult ( this . _nativeAddon . getUpdateInfo ( ) ) ; } public updateHierarchyState ( db , rulesetId , changeType , serializedKeys ) { return this . handleResult ( this . _nativeAddon . updateHierarchyState ( db , rulesetId , changeType , serializedKeys ) ) ; } } ; } ;