packageDocumentation module import { Guid , Id64String , IDisposable } from "@itwin/core-bentley" ; import { IModelRpcProps , RpcManager } from "@itwin/core-common" ; import { DescriptorJSON , DescriptorOverrides } from "./content/Descriptor" ; import { ItemJSON } from "./content/Item" ; import { DisplayValueGroupJSON } from "./content/Value" ; import { DiagnosticsScopeLogs } from "./Diagnostics" ; import { InstanceKeyJSON } from "./EC" ; import { ElementProperties } from "./ElementProperties" ; import { PresentationError , PresentationStatus } from "./Error" ; import { NodeKeyJSON } from "./hierarchy/Key" ; import { NodeJSON } from "./hierarchy/Node" ; import { NodePathElementJSON } from "./hierarchy/NodePathElement" ; import { KeySetJSON } from "./KeySet" ; import { LabelDefinitionJSON } from "./LabelDefinition" ; import { ContentDescriptorRequestOptions , ContentInstanceKeysRequestOptions , ContentRequestOptions , ContentSourcesRequestOptions , DisplayLabelRequestOptions , DisplayLabelsRequestOptions , DistinctValuesRequestOptions , FilterByInstancePathsHierarchyRequestOptions , FilterByTextHierarchyRequestOptions , HierarchyRequestOptions , Paged , RequestOptions , SelectionScopeRequestOptions , SingleElementPropertiesRequestOptions , } from "./PresentationManagerOptions" ; import { ContentSourcesRpcResult , PresentationRpcInterface , PresentationRpcRequestOptions , PresentationRpcResponse , } from "./PresentationRpcInterface" ; import { RulesetVariableJSON } from "./RulesetVariables" ; import { SelectionScope } from "./selection/SelectionScope" ; import { PagedResponse } from "./Utils" ; internal export interface RpcRequestsHandlerProps { internal clientId ? : string ; } internal export class RpcRequestsHandler implements IDisposable { public readonly maxRequestRepeatCount : number = 5 ; /** ID that identifies this handler as a client */ public readonly clientId : string ; public constructor ( props ? ) { this . clientId = ( props && props . clientId ) ? props . clientId : Guid . createValue ( ) ; } public dispose ( ) { } private get rpcClient ( ) : PresentationRpcInterface { return RpcManager . getClientForInterface ( PresentationRpcInterface ) ; } private async requestRepeatedly < TResult > ( func , diagnosticsHandler ? , repeatCount = 1 ) { let diagnostics ; let error ; let shouldRepeat = false ; try { const response = await func ( ) ; diagnostics = response . diagnostics ; if ( response . statusCode === PresentationStatus . Success ) return response . result ! ; if ( response . statusCode === PresentationStatus . BackendTimeout && repeatCount < this . maxRequestRepeatCount ) shouldRepeat = true ; else error = new PresentationError ( response . statusCode , response . errorMessage ) ; } catch ( e ) { error = e ; if ( repeatCount < this . maxRequestRepeatCount ) shouldRepeat = true ; } finally { if ( diagnosticsHandler && diagnostics ) diagnosticsHandler ( diagnostics ) ; } if ( shouldRepeat ) { ++ repeatCount ; return this . requestRepeatedly ( func , diagnosticsHandler , repeatCount ) ; } throw error ; } internal public async request < TResult , TOptions extends RequestOptions < IModelRpcProps > , TArg = any > ( func , options , ... additionalOptions ) { const { imodel , diagnostics , ... optionsNoIModel } = options ; const { handler : diagnosticsHandler , ... diagnosticsOptions } = diagnostics ?? { } ; const rpcOptions = { ... optionsNoIModel , clientId : this . clientId } ; if ( diagnostics ) rpcOptions . diagnostics = diagnosticsOptions ; const doRequest = async ( ) => func ( imodel , rpcOptions , ... additionalOptions ) ; return this . requestRepeatedly ( doRequest , diagnosticsHandler ) ; } public async getNodesCount ( options ) { return this . request < number , HierarchyRequestOptions < IModelRpcProps , NodeKeyJSON , RulesetVariableJSON > > ( this . rpcClient . getNodesCount . bind ( this . rpcClient ) , options ) ; } public async getPagedNodes ( options ) { return this . request < PagedResponse < NodeJSON > , Paged < HierarchyRequestOptions < IModelRpcProps , NodeKeyJSON , RulesetVariableJSON > > > ( this . rpcClient . getPagedNodes . bind ( this . rpcClient ) , options ) ; } public async getNodePaths ( options ) { return this . request < NodePathElementJSON [ ] , FilterByInstancePathsHierarchyRequestOptions < IModelRpcProps , RulesetVariableJSON > > ( this . rpcClient . getNodePaths . bind ( this . rpcClient ) , options ) ; } public async getFilteredNodePaths ( options ) { return this . request < NodePathElementJSON [ ] , FilterByTextHierarchyRequestOptions < IModelRpcProps , RulesetVariableJSON > > ( this . rpcClient . getFilteredNodePaths . bind ( this . rpcClient ) , options ) ; } public async getContentSources ( options ) { return this . request < ContentSourcesRpcResult , ContentSourcesRequestOptions < IModelRpcProps > > ( this . rpcClient . getContentSources . bind ( this . rpcClient ) , options ) ; } public async getContentDescriptor ( options ) { return this . request < DescriptorJSON | undefined , ContentDescriptorRequestOptions < IModelRpcProps , KeySetJSON , RulesetVariableJSON > > ( this . rpcClient . getContentDescriptor . bind ( this . rpcClient ) , options ) ; } public async getContentSetSize ( options ) { return this . request < number , ContentRequestOptions < IModelRpcProps , DescriptorOverrides , KeySetJSON , RulesetVariableJSON > > ( this . rpcClient . getContentSetSize . bind ( this . rpcClient ) , options ) ; } public async getPagedContent ( options ) { return this . request < { descriptor : DescriptorJSON ; contentSet : PagedResponse < ItemJSON > ; } | undefined , Paged < ContentRequestOptions < IModelRpcProps , DescriptorOverrides , KeySetJSON , RulesetVariableJSON > > > ( this . rpcClient . getPagedContent . bind ( this . rpcClient ) , options ) ; } public async getPagedContentSet ( options ) { return this . request < PagedResponse < ItemJSON > , Paged < ContentRequestOptions < IModelRpcProps , DescriptorOverrides , KeySetJSON , RulesetVariableJSON > > > ( this . rpcClient . getPagedContentSet . bind ( this . rpcClient ) , options ) ; } public async getPagedDistinctValues ( options ) { return this . request < PagedResponse < DisplayValueGroupJSON > , DistinctValuesRequestOptions < IModelRpcProps , DescriptorOverrides , KeySetJSON , RulesetVariableJSON > > ( this . rpcClient . getPagedDistinctValues . bind ( this . rpcClient ) , options ) ; } public async getElementProperties ( options ) { return this . request < ElementProperties | undefined , SingleElementPropertiesRequestOptions < IModelRpcProps > > ( this . rpcClient . getElementProperties . bind ( this . rpcClient ) , options ) ; } public async getContentInstanceKeys ( options ) { return this . request < { total : number ; items : KeySetJSON ; } , ContentInstanceKeysRequestOptions < IModelRpcProps , KeySetJSON , RulesetVariableJSON > > ( this . rpcClient . getContentInstanceKeys . bind ( this . rpcClient ) , options ) ; } public async getDisplayLabelDefinition ( options ) { return this . request < LabelDefinitionJSON , DisplayLabelRequestOptions < IModelRpcProps , InstanceKeyJSON > , any > ( this . rpcClient . getDisplayLabelDefinition . bind ( this . rpcClient ) , options ) ; } public async getPagedDisplayLabelDefinitions ( options ) { return this . request < PagedResponse < LabelDefinitionJSON > , DisplayLabelsRequestOptions < IModelRpcProps , InstanceKeyJSON > , any > ( this . rpcClient . getPagedDisplayLabelDefinitions . bind ( this . rpcClient ) , options ) ; } public async getSelectionScopes ( options ) { return this . request < SelectionScope [ ] , SelectionScopeRequestOptions < IModelRpcProps > > ( this . rpcClient . getSelectionScopes . bind ( this . rpcClient ) , options ) ; } public async computeSelection ( options , ids , scopeId ) { return this . request < KeySetJSON , SelectionScopeRequestOptions < IModelRpcProps > > ( this . rpcClient . computeSelection . bind ( this . rpcClient ) , options , ids , scopeId ) ; } }