@itwin/presentation-frontend 4.0.0-dev.44 → 4.0.0-dev.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  2. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js +47 -47
  3. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js.map +1 -1
  4. package/lib/cjs/presentation-frontend/Diagnostics.d.ts +14 -14
  5. package/lib/cjs/presentation-frontend/Diagnostics.js +61 -61
  6. package/lib/cjs/presentation-frontend/Diagnostics.js.map +1 -1
  7. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  8. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js +20 -20
  9. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js.map +1 -1
  10. package/lib/cjs/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  11. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js +41 -41
  12. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js.map +1 -1
  13. package/lib/cjs/presentation-frontend/LocalizationHelper.d.ts +10 -10
  14. package/lib/cjs/presentation-frontend/LocalizationHelper.js +31 -31
  15. package/lib/cjs/presentation-frontend/LocalizationHelper.js.map +1 -1
  16. package/lib/cjs/presentation-frontend/Presentation.d.ts +73 -73
  17. package/lib/cjs/presentation-frontend/Presentation.js +159 -159
  18. package/lib/cjs/presentation-frontend/Presentation.js.map +1 -1
  19. package/lib/cjs/presentation-frontend/PresentationManager.d.ts +205 -205
  20. package/lib/cjs/presentation-frontend/PresentationManager.d.ts.map +1 -1
  21. package/lib/cjs/presentation-frontend/PresentationManager.js +438 -444
  22. package/lib/cjs/presentation-frontend/PresentationManager.js.map +1 -1
  23. package/lib/cjs/presentation-frontend/RulesetManager.d.ts +63 -63
  24. package/lib/cjs/presentation-frontend/RulesetManager.js +85 -85
  25. package/lib/cjs/presentation-frontend/RulesetManager.js.map +1 -1
  26. package/lib/cjs/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  27. package/lib/cjs/presentation-frontend/RulesetVariablesManager.js +195 -195
  28. package/lib/cjs/presentation-frontend/RulesetVariablesManager.js.map +1 -1
  29. package/lib/cjs/presentation-frontend/StateTracker.d.ts +38 -38
  30. package/lib/cjs/presentation-frontend/StateTracker.js +128 -128
  31. package/lib/cjs/presentation-frontend/StateTracker.js.map +1 -1
  32. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  33. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +525 -525
  34. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  35. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  36. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +290 -290
  37. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  38. package/lib/cjs/presentation-frontend/selection/HiliteRules.json +172 -172
  39. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  40. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.js +97 -97
  41. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.js.map +1 -1
  42. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  43. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.js +9 -9
  44. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.js.map +1 -1
  45. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  46. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.js +33 -33
  47. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.js.map +1 -1
  48. package/lib/cjs/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  49. package/lib/cjs/presentation-frontend/selection/SelectionHandler.js +108 -108
  50. package/lib/cjs/presentation-frontend/selection/SelectionHandler.js.map +1 -1
  51. package/lib/cjs/presentation-frontend/selection/SelectionHelper.d.ts +17 -17
  52. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js +40 -40
  53. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  54. package/lib/cjs/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  55. package/lib/cjs/presentation-frontend/selection/SelectionManager.js +414 -414
  56. package/lib/cjs/presentation-frontend/selection/SelectionManager.js.map +1 -1
  57. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  58. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js +91 -91
  59. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  60. package/lib/cjs/presentation-frontend.d.ts +34 -34
  61. package/lib/cjs/presentation-frontend.js +50 -50
  62. package/lib/cjs/presentation-frontend.js.map +1 -1
  63. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  64. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js +43 -43
  65. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js.map +1 -1
  66. package/lib/esm/presentation-frontend/Diagnostics.d.ts +14 -14
  67. package/lib/esm/presentation-frontend/Diagnostics.js +56 -56
  68. package/lib/esm/presentation-frontend/Diagnostics.js.map +1 -1
  69. package/lib/esm/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  70. package/lib/esm/presentation-frontend/FrontendLoggerCategory.js +17 -17
  71. package/lib/esm/presentation-frontend/FrontendLoggerCategory.js.map +1 -1
  72. package/lib/esm/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  73. package/lib/esm/presentation-frontend/IpcRequestsHandler.js +37 -37
  74. package/lib/esm/presentation-frontend/IpcRequestsHandler.js.map +1 -1
  75. package/lib/esm/presentation-frontend/LocalizationHelper.d.ts +10 -10
  76. package/lib/esm/presentation-frontend/LocalizationHelper.js +27 -27
  77. package/lib/esm/presentation-frontend/LocalizationHelper.js.map +1 -1
  78. package/lib/esm/presentation-frontend/Presentation.d.ts +73 -73
  79. package/lib/esm/presentation-frontend/Presentation.js +155 -155
  80. package/lib/esm/presentation-frontend/Presentation.js.map +1 -1
  81. package/lib/esm/presentation-frontend/PresentationManager.d.ts +205 -205
  82. package/lib/esm/presentation-frontend/PresentationManager.d.ts.map +1 -1
  83. package/lib/esm/presentation-frontend/PresentationManager.js +433 -439
  84. package/lib/esm/presentation-frontend/PresentationManager.js.map +1 -1
  85. package/lib/esm/presentation-frontend/RulesetManager.d.ts +63 -63
  86. package/lib/esm/presentation-frontend/RulesetManager.js +81 -81
  87. package/lib/esm/presentation-frontend/RulesetManager.js.map +1 -1
  88. package/lib/esm/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  89. package/lib/esm/presentation-frontend/RulesetVariablesManager.js +191 -191
  90. package/lib/esm/presentation-frontend/RulesetVariablesManager.js.map +1 -1
  91. package/lib/esm/presentation-frontend/StateTracker.d.ts +38 -38
  92. package/lib/esm/presentation-frontend/StateTracker.js +124 -124
  93. package/lib/esm/presentation-frontend/StateTracker.js.map +1 -1
  94. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  95. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +519 -519
  96. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  97. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  98. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +282 -282
  99. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  100. package/lib/esm/presentation-frontend/selection/HiliteRules.json +172 -172
  101. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  102. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js +93 -93
  103. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js.map +1 -1
  104. package/lib/esm/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  105. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js +8 -8
  106. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js.map +1 -1
  107. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  108. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js +29 -29
  109. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js.map +1 -1
  110. package/lib/esm/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  111. package/lib/esm/presentation-frontend/selection/SelectionHandler.js +104 -104
  112. package/lib/esm/presentation-frontend/selection/SelectionHandler.js.map +1 -1
  113. package/lib/esm/presentation-frontend/selection/SelectionHelper.d.ts +17 -17
  114. package/lib/esm/presentation-frontend/selection/SelectionHelper.js +36 -36
  115. package/lib/esm/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  116. package/lib/esm/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  117. package/lib/esm/presentation-frontend/selection/SelectionManager.js +409 -409
  118. package/lib/esm/presentation-frontend/selection/SelectionManager.js.map +1 -1
  119. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  120. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js +85 -85
  121. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  122. package/lib/esm/presentation-frontend.d.ts +34 -34
  123. package/lib/esm/presentation-frontend.js +38 -38
  124. package/lib/esm/presentation-frontend.js.map +1 -1
  125. package/package.json +14 -14
@@ -1,440 +1,434 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
- * See LICENSE.md in the project root for license terms and full copyright notice.
4
- *--------------------------------------------------------------------------------------------*/
5
- /** @packageDocumentation
6
- * @module Core
7
- */
8
- import { BeEvent, CompressedId64Set, OrderedId64Iterable } from "@itwin/core-bentley";
9
- import { IModelApp, IpcApp } from "@itwin/core-frontend";
10
- import { Content, Descriptor, DisplayValueGroup, Item, Key, KeySet, Node, NodePathElement, PresentationIpcEvents, RpcRequestsHandler, RulesetVariable, SelectClassInfo, UpdateInfo, VariableValueTypes, } from "@itwin/presentation-common";
11
- import { IpcRequestsHandler } from "./IpcRequestsHandler";
12
- import { FrontendLocalizationHelper } from "./LocalizationHelper";
13
- import { RulesetManagerImpl } from "./RulesetManager";
14
- import { RulesetVariablesManagerImpl } from "./RulesetVariablesManager";
15
- import { TRANSIENT_ELEMENT_CLASSNAME } from "./selection/SelectionManager";
16
- import { StateTracker } from "./StateTracker";
17
- /**
18
- * Frontend Presentation manager which basically just forwards all calls to
19
- * the backend implementation.
20
- *
21
- * @public
22
- */
23
- export class PresentationManager {
24
- constructor(props) {
25
- /**
26
- * An event raised when hierarchies created using specific ruleset change
27
- * @alpha
28
- */
29
- this.onIModelHierarchyChanged = new BeEvent();
30
- /**
31
- * An event raised when content created using specific ruleset changes
32
- * @alpha
33
- */
34
- this.onIModelContentChanged = new BeEvent();
35
- // eslint-disable-next-line @typescript-eslint/naming-convention
36
- this.onUpdate = (_evt, report) => {
37
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
38
- this.handleUpdateAsync(UpdateInfo.fromJSON(report));
39
- };
40
- if (props) {
41
- // eslint-disable-next-line deprecation/deprecation
42
- this._explicitActiveUnitSystem = props.activeUnitSystem;
43
- }
44
- this._requestsHandler = props?.rpcRequestsHandler ?? new RpcRequestsHandler(props ? { clientId: props.clientId, timeout: props.requestTimeout } : undefined);
45
- this._rulesetVars = new Map();
46
- this._rulesets = RulesetManagerImpl.create();
47
- this._localizationHelper = new FrontendLocalizationHelper(props?.activeLocale);
48
- this._connections = new Map();
49
- if (IpcApp.isValid) {
50
- // Ipc only works in ipc apps, so the `onUpdate` callback will only be called there.
51
- this._clearEventListener = IpcApp.addListener(PresentationIpcEvents.Update, this.onUpdate);
52
- this._ipcRequestsHandler = props?.ipcRequestsHandler ?? new IpcRequestsHandler(this._requestsHandler.clientId);
53
- this._stateTracker = props?.stateTracker ?? new StateTracker(this._ipcRequestsHandler);
54
- }
55
- }
56
- /**
57
- * Get / set active unit system used to format property values with units.
58
- *
59
- * @deprecated in 4.0. `IModelApp.quantityFormatter` should be used to get/set the active unit system. At the moment
60
- * [[PresentationManager]] allows overriding it, but returns `IModelApp.quantityFormatter.activeUnitSystem` if override
61
- * is not set.
62
- */
63
- get activeUnitSystem() {
64
- return this._explicitActiveUnitSystem ?? IModelApp.quantityFormatter.activeUnitSystem;
65
- }
66
- set activeUnitSystem(value) { this._explicitActiveUnitSystem = value; }
67
- /** Get / set active locale used for localizing presentation data */
68
- get activeLocale() { return this._localizationHelper.locale; }
69
- set activeLocale(locale) { this._localizationHelper.locale = locale; }
70
- dispose() {
71
- if (this._clearEventListener) {
72
- this._clearEventListener();
73
- this._clearEventListener = undefined;
74
- }
75
- }
76
- async onConnection(imodel) {
77
- if (!this._connections.has(imodel))
78
- this._connections.set(imodel, this.initializeIModel(imodel));
79
- await this._connections.get(imodel);
80
- }
81
- async initializeIModel(imodel) {
82
- imodel.onClose.addOnce(() => {
83
- this._connections.delete(imodel);
84
- });
85
- await this.onNewiModelConnection(imodel);
86
- }
87
- /** @note This is only called in native apps after changes in iModels */
88
- async handleUpdateAsync(report) {
89
- for (const imodelKey in report) {
90
- // istanbul ignore if
91
- if (!report.hasOwnProperty(imodelKey))
92
- continue;
93
- const imodelReport = report[imodelKey];
94
- for (const rulesetId in imodelReport) {
95
- // istanbul ignore if
96
- if (!imodelReport.hasOwnProperty(rulesetId))
97
- continue;
98
- const updateInfo = imodelReport[rulesetId];
99
- if (updateInfo.content)
100
- this.onIModelContentChanged.raiseEvent({ rulesetId, updateInfo: updateInfo.content, imodelKey });
101
- if (updateInfo.hierarchy)
102
- this.onIModelHierarchyChanged.raiseEvent({ rulesetId, updateInfo: updateInfo.hierarchy, imodelKey });
103
- }
104
- }
105
- }
106
- /**
107
- * Function that is called when a new IModelConnection is used to retrieve data.
108
- * @internal
109
- */
110
- async onNewiModelConnection(_) { }
111
- /**
112
- * Create a new PresentationManager instance
113
- * @param props Optional properties used to configure the manager
114
- */
115
- static create(props) {
116
- return new PresentationManager(props);
117
- }
118
- /** @internal */
119
- get rpcRequestsHandler() { return this._requestsHandler; }
120
- /** @internal */
121
- get ipcRequestsHandler() { return this._ipcRequestsHandler; }
122
- /** @internal */
123
- get stateTracker() { return this._stateTracker; }
124
- /**
125
- * Get rulesets manager
126
- */
127
- rulesets() { return this._rulesets; }
128
- /**
129
- * Get ruleset variables manager for specific ruleset
130
- * @param rulesetId Id of the ruleset to get the vars manager for
131
- */
132
- vars(rulesetId) {
133
- if (!this._rulesetVars.has(rulesetId)) {
134
- const varsManager = new RulesetVariablesManagerImpl(rulesetId, this._ipcRequestsHandler);
135
- this._rulesetVars.set(rulesetId, varsManager);
136
- }
137
- return this._rulesetVars.get(rulesetId);
138
- }
139
- toRpcTokenOptions(requestOptions) {
140
- // 1. put default `locale` and `unitSystem`
141
- // 2. put all `requestOptions` members (if `locale` or `unitSystem` are set, they'll override the defaults put at #1)
142
- // 3. put `imodel` of type `IModelRpcProps` which overwrites the `imodel` from `requestOptions` put at #2
143
- const defaultOptions = {};
144
- if (this.activeLocale)
145
- defaultOptions.locale = this.activeLocale;
146
- defaultOptions.unitSystem = this.activeUnitSystem; // eslint-disable-line deprecation/deprecation
147
- const { imodel, rulesetVariables, ...rpcRequestOptions } = requestOptions;
148
- return {
149
- ...defaultOptions,
150
- ...rpcRequestOptions,
151
- ...(rulesetVariables ? { rulesetVariables: rulesetVariables.map(RulesetVariable.toJSON) } : {}),
152
- imodel: imodel.getRpcProps(),
153
- };
154
- }
155
- async addRulesetAndVariablesToOptions(options) {
156
- const { rulesetOrId, rulesetVariables } = options;
157
- let foundRulesetOrId;
158
- if (typeof rulesetOrId === "object") {
159
- foundRulesetOrId = rulesetOrId;
160
- }
161
- else {
162
- const foundRuleset = await this._rulesets.get(rulesetOrId);
163
- foundRulesetOrId = foundRuleset ? foundRuleset.toJSON() : rulesetOrId;
164
- }
165
- const rulesetId = (typeof foundRulesetOrId === "object") ? foundRulesetOrId.id : foundRulesetOrId;
166
- // All Id64Array variable values must be sorted for serialization to JSON to work. RulesetVariablesManager
167
- // sorts them before storing, so that part is taken care of, but we need to ensure that variables coming from
168
- // request options are also sorted.
169
- const variables = (rulesetVariables ?? []).map((variable) => {
170
- if (variable.type === VariableValueTypes.Id64Array)
171
- return { ...variable, value: OrderedId64Iterable.sortArray(variable.value) };
172
- return variable;
173
- });
174
- if (!this._ipcRequestsHandler) {
175
- // only need to add variables from variables manager if there's no IPC
176
- // handler - if there is one, the variables are already known by the backend
177
- variables.push(...this.vars(rulesetId).getAllVariables());
178
- }
179
- return { ...options, rulesetOrId: foundRulesetOrId, rulesetVariables: variables };
180
- }
181
- /** Retrieves nodes */
182
- async getNodes(requestOptions) {
183
- await this.onConnection(requestOptions.imodel);
184
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
185
- const rpcOptions = this.toRpcTokenOptions({ ...options });
186
- const result = await buildPagedArrayResponse(options.paging, async (partialPageOptions) => this._requestsHandler.getPagedNodes({ ...rpcOptions, paging: partialPageOptions }));
187
- // eslint-disable-next-line deprecation/deprecation
188
- return this._localizationHelper.getLocalizedNodes(result.items.map(Node.fromJSON));
189
- }
190
- /** Retrieves nodes count. */
191
- async getNodesCount(requestOptions) {
192
- await this.onConnection(requestOptions.imodel);
193
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
194
- const rpcOptions = this.toRpcTokenOptions({ ...options });
195
- return this._requestsHandler.getNodesCount(rpcOptions);
196
- }
197
- /** Retrieves total nodes count and a single page of nodes. */
198
- async getNodesAndCount(requestOptions) {
199
- await this.onConnection(requestOptions.imodel);
200
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
201
- const rpcOptions = this.toRpcTokenOptions({ ...options });
202
- const result = await buildPagedArrayResponse(options.paging, async (partialPageOptions) => this._requestsHandler.getPagedNodes({ ...rpcOptions, paging: partialPageOptions }));
203
- return {
204
- count: result.total,
205
- // eslint-disable-next-line deprecation/deprecation
206
- nodes: this._localizationHelper.getLocalizedNodes(result.items.map(Node.fromJSON)),
207
- };
208
- }
209
- /**
210
- * Retrieves hierarchy level descriptor.
211
- * @beta
212
- */
213
- async getNodesDescriptor(requestOptions) {
214
- await this.onConnection(requestOptions.imodel);
215
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
216
- const rpcOptions = this.toRpcTokenOptions({ ...options });
217
- const result = await this._requestsHandler.getNodesDescriptor(rpcOptions);
218
- return Descriptor.fromJSON(result);
219
- }
220
- /** Retrieves paths from root nodes to children nodes according to specified keys. Intersecting paths will be merged. */
221
- async getNodePaths(requestOptions) {
222
- await this.onConnection(requestOptions.imodel);
223
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
224
- const rpcOptions = this.toRpcTokenOptions({ ...options });
225
- const result = await this._requestsHandler.getNodePaths(rpcOptions);
226
- // eslint-disable-next-line deprecation/deprecation
227
- return result.map(NodePathElement.fromJSON);
228
- }
229
- /** Retrieves paths from root nodes to nodes containing filter text in their label. */
230
- async getFilteredNodePaths(requestOptions) {
231
- await this.onConnection(requestOptions.imodel);
232
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
233
- const result = await this._requestsHandler.getFilteredNodePaths(this.toRpcTokenOptions(options));
234
- // eslint-disable-next-line deprecation/deprecation
235
- return result.map(NodePathElement.fromJSON);
236
- }
237
- /**
238
- * Get information about the sources of content when building it for specific ECClasses. Sources involve classes of the primary select instance,
239
- * its related instances for loading related and navigation properties.
240
- * @public
241
- */
242
- async getContentSources(requestOptions) {
243
- await this.onConnection(requestOptions.imodel);
244
- const rpcOptions = this.toRpcTokenOptions(requestOptions);
245
- const result = await this._requestsHandler.getContentSources(rpcOptions);
246
- return SelectClassInfo.listFromCompressedJSON(result.sources, result.classesMap);
247
- }
248
- /** Retrieves the content descriptor which describes the content and can be used to customize it. */
249
- async getContentDescriptor(requestOptions) {
250
- await this.onConnection(requestOptions.imodel);
251
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
252
- const rpcOptions = this.toRpcTokenOptions({
253
- ...options,
254
- keys: stripTransientElementKeys(options.keys).toJSON(),
255
- });
256
- const result = await this._requestsHandler.getContentDescriptor(rpcOptions);
257
- return Descriptor.fromJSON(result);
258
- }
259
- /** Retrieves overall content set size. */
260
- async getContentSetSize(requestOptions) {
261
- await this.onConnection(requestOptions.imodel);
262
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
263
- const rpcOptions = this.toRpcTokenOptions({
264
- ...options,
265
- descriptor: getDescriptorOverrides(requestOptions.descriptor),
266
- keys: stripTransientElementKeys(requestOptions.keys).toJSON(),
267
- });
268
- return this._requestsHandler.getContentSetSize(rpcOptions);
269
- }
270
- /** Retrieves content which consists of a content descriptor and a page of records. */
271
- async getContent(requestOptions) {
272
- return (await this.getContentAndSize(requestOptions))?.content;
273
- }
274
- /** Retrieves content set size and content which consists of a content descriptor and a page of records. */
275
- async getContentAndSize(requestOptions) {
276
- await this.onConnection(requestOptions.imodel);
277
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
278
- const rpcOptions = this.toRpcTokenOptions({
279
- ...options,
280
- descriptor: getDescriptorOverrides(requestOptions.descriptor),
281
- keys: stripTransientElementKeys(requestOptions.keys).toJSON(),
282
- });
283
- let descriptor = (requestOptions.descriptor instanceof Descriptor) ? requestOptions.descriptor : undefined;
284
- const result = await buildPagedArrayResponse(options.paging, async (partialPageOptions, requestIndex) => {
285
- if (0 === requestIndex && !descriptor) {
286
- const content = await this._requestsHandler.getPagedContent({ ...rpcOptions, paging: partialPageOptions });
287
- if (content) {
288
- descriptor = Descriptor.fromJSON(content.descriptor);
289
- return content.contentSet;
290
- }
291
- return { total: 0, items: [] };
292
- }
293
- return this._requestsHandler.getPagedContentSet({ ...rpcOptions, paging: partialPageOptions });
294
- });
295
- if (!descriptor)
296
- return undefined;
297
- const items = result.items.map((itemJson) => Item.fromJSON(itemJson)).filter((item) => (item !== undefined));
298
- return {
299
- size: result.total,
300
- content: this._localizationHelper.getLocalizedContent(new Content(descriptor, items)),
301
- };
302
- }
303
- /** Retrieves distinct values of specific field from the content. */
304
- async getPagedDistinctValues(requestOptions) {
305
- await this.onConnection(requestOptions.imodel);
306
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
307
- const rpcOptions = {
308
- ...this.toRpcTokenOptions(options),
309
- descriptor: getDescriptorOverrides(options.descriptor),
310
- keys: stripTransientElementKeys(options.keys).toJSON(),
311
- };
312
- const result = await buildPagedArrayResponse(requestOptions.paging, async (partialPageOptions) => this._requestsHandler.getPagedDistinctValues({ ...rpcOptions, paging: partialPageOptions }));
313
- return {
314
- ...result,
315
- // eslint-disable-next-line deprecation/deprecation
316
- items: result.items.map(DisplayValueGroup.fromJSON),
317
- };
318
- }
319
- /**
320
- * Retrieves property data in a simplified format for a single element specified by ID.
321
- * @public
322
- */
323
- async getElementProperties(requestOptions) {
324
- await this.onConnection(requestOptions.imodel);
325
- const results = await this._requestsHandler.getElementProperties(this.toRpcTokenOptions(requestOptions));
326
- // istanbul ignore if
327
- if (!results)
328
- return undefined;
329
- return this._localizationHelper.getLocalizedElementProperties(results);
330
- }
331
- /**
332
- * Retrieves content item instance keys.
333
- * @public
334
- */
335
- async getContentInstanceKeys(requestOptions) {
336
- await this.onConnection(requestOptions.imodel);
337
- const options = await this.addRulesetAndVariablesToOptions(requestOptions);
338
- const rpcOptions = {
339
- ...this.toRpcTokenOptions(options),
340
- keys: stripTransientElementKeys(options.keys).toJSON(),
341
- };
342
- const props = {
343
- page: requestOptions.paging,
344
- get: async (page) => {
345
- const keys = await this._requestsHandler.getContentInstanceKeys({ ...rpcOptions, paging: page });
346
- return {
347
- total: keys.total,
348
- items: keys.items.instanceKeys.reduce((instanceKeys, entry) => {
349
- for (const id of CompressedId64Set.iterable(entry[1])) {
350
- instanceKeys.push({ className: entry[0], id });
351
- }
352
- return instanceKeys;
353
- }, new Array()),
354
- };
355
- },
356
- };
357
- return createPagedGeneratorResponse(props);
358
- }
359
- /** Retrieves display label definition of specific item. */
360
- async getDisplayLabelDefinition(requestOptions) {
361
- await this.onConnection(requestOptions.imodel);
362
- const rpcOptions = this.toRpcTokenOptions({ ...requestOptions });
363
- const result = await this._requestsHandler.getDisplayLabelDefinition(rpcOptions);
364
- return this._localizationHelper.getLocalizedLabelDefinition(result);
365
- }
366
- /** Retrieves display label definition of specific items. */
367
- async getDisplayLabelDefinitions(requestOptions) {
368
- await this.onConnection(requestOptions.imodel);
369
- const rpcOptions = this.toRpcTokenOptions({ ...requestOptions });
370
- const result = await buildPagedArrayResponse(undefined, async (partialPageOptions) => {
371
- const partialKeys = (!partialPageOptions.start) ? rpcOptions.keys : rpcOptions.keys.slice(partialPageOptions.start);
372
- return this._requestsHandler.getPagedDisplayLabelDefinitions({ ...rpcOptions, keys: partialKeys });
373
- });
374
- return this._localizationHelper.getLocalizedLabelDefinitions(result.items);
375
- }
376
- }
377
- const getDescriptorOverrides = (descriptorOrOverrides) => {
378
- if (descriptorOrOverrides instanceof Descriptor)
379
- return descriptorOrOverrides.createDescriptorOverrides();
380
- return descriptorOrOverrides;
381
- };
382
- async function createPagedGeneratorResponse(props) {
383
- let pageStart = props.page?.start ?? 0;
384
- let pageSize = props.page?.size ?? 0;
385
- let requestIndex = 0;
386
- const firstPage = await props.get({ start: pageStart, size: pageSize }, requestIndex++);
387
- return {
388
- total: firstPage.total,
389
- async *items() {
390
- let partialResult = firstPage;
391
- while (true) {
392
- for (const item of partialResult.items) {
393
- yield item;
394
- }
395
- const receivedItemsCount = partialResult.items.length;
396
- if (partialResult.total !== 0 && receivedItemsCount === 0) {
397
- if (pageStart >= partialResult.total)
398
- throw new Error(`Requested page with start index ${pageStart} is out of bounds. Total number of items: ${partialResult.total}`);
399
- throw new Error("Paged request returned non zero total count but no items");
400
- }
401
- if (pageSize !== 0 && receivedItemsCount >= pageSize || receivedItemsCount >= (partialResult.total - pageStart))
402
- break;
403
- if (pageSize !== 0)
404
- pageSize -= receivedItemsCount;
405
- pageStart += receivedItemsCount;
406
- partialResult = await props.get({ start: pageStart, size: pageSize }, requestIndex++);
407
- }
408
- },
409
- };
410
- }
411
- /** @internal */
412
- export const buildPagedArrayResponse = async (requestedPage, getter) => {
413
- try {
414
- const items = new Array();
415
- const gen = await createPagedGeneratorResponse({ page: requestedPage, get: getter });
416
- for await (const item of gen.items()) {
417
- items.push(item);
418
- }
419
- return { total: gen.total, items };
420
- }
421
- catch {
422
- // FIXME: we should propagate the error...
423
- return { total: 0, items: [] };
424
- }
425
- };
426
- const stripTransientElementKeys = (keys) => {
427
- if (!keys.some((key) => Key.isInstanceKey(key) && key.className === TRANSIENT_ELEMENT_CLASSNAME))
428
- return keys;
429
- const copy = new KeySet();
430
- copy.add(keys, (key) => {
431
- // the callback is not going to be called with EntityProps as KeySet converts them
432
- // to InstanceKeys, but we want to keep the EntityProps case for correctness
433
- // istanbul ignore next
434
- const isTransient = Key.isInstanceKey(key) && key.className === TRANSIENT_ELEMENT_CLASSNAME
435
- || Key.isEntityProps(key) && key.classFullName === TRANSIENT_ELEMENT_CLASSNAME;
436
- return !isTransient;
437
- });
438
- return copy;
439
- };
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Core
7
+ */
8
+ import { BeEvent, CompressedId64Set, OrderedId64Iterable } from "@itwin/core-bentley";
9
+ import { IModelApp, IpcApp } from "@itwin/core-frontend";
10
+ import { Content, Descriptor, DisplayValueGroup, Item, Key, KeySet, Node, NodePathElement, PresentationIpcEvents, RpcRequestsHandler, RulesetVariable, SelectClassInfo, UpdateInfo, VariableValueTypes, } from "@itwin/presentation-common";
11
+ import { IpcRequestsHandler } from "./IpcRequestsHandler";
12
+ import { FrontendLocalizationHelper } from "./LocalizationHelper";
13
+ import { RulesetManagerImpl } from "./RulesetManager";
14
+ import { RulesetVariablesManagerImpl } from "./RulesetVariablesManager";
15
+ import { TRANSIENT_ELEMENT_CLASSNAME } from "./selection/SelectionManager";
16
+ import { StateTracker } from "./StateTracker";
17
+ /**
18
+ * Frontend Presentation manager which basically just forwards all calls to
19
+ * the backend implementation.
20
+ *
21
+ * @public
22
+ */
23
+ export class PresentationManager {
24
+ constructor(props) {
25
+ /**
26
+ * An event raised when hierarchies created using specific ruleset change
27
+ * @alpha
28
+ */
29
+ this.onIModelHierarchyChanged = new BeEvent();
30
+ /**
31
+ * An event raised when content created using specific ruleset changes
32
+ * @alpha
33
+ */
34
+ this.onIModelContentChanged = new BeEvent();
35
+ // eslint-disable-next-line @typescript-eslint/naming-convention
36
+ this.onUpdate = (_evt, report) => {
37
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
38
+ this.handleUpdateAsync(UpdateInfo.fromJSON(report));
39
+ };
40
+ if (props) {
41
+ // eslint-disable-next-line deprecation/deprecation
42
+ this._explicitActiveUnitSystem = props.activeUnitSystem;
43
+ }
44
+ this._requestsHandler = props?.rpcRequestsHandler ?? new RpcRequestsHandler(props ? { clientId: props.clientId, timeout: props.requestTimeout } : undefined);
45
+ this._rulesetVars = new Map();
46
+ this._rulesets = RulesetManagerImpl.create();
47
+ this._localizationHelper = new FrontendLocalizationHelper(props?.activeLocale);
48
+ this._connections = new Map();
49
+ if (IpcApp.isValid) {
50
+ // Ipc only works in ipc apps, so the `onUpdate` callback will only be called there.
51
+ this._clearEventListener = IpcApp.addListener(PresentationIpcEvents.Update, this.onUpdate);
52
+ this._ipcRequestsHandler = props?.ipcRequestsHandler ?? new IpcRequestsHandler(this._requestsHandler.clientId);
53
+ this._stateTracker = props?.stateTracker ?? new StateTracker(this._ipcRequestsHandler);
54
+ }
55
+ }
56
+ /**
57
+ * Get / set active unit system used to format property values with units.
58
+ *
59
+ * @deprecated in 4.0. `IModelApp.quantityFormatter` should be used to get/set the active unit system. At the moment
60
+ * [[PresentationManager]] allows overriding it, but returns `IModelApp.quantityFormatter.activeUnitSystem` if override
61
+ * is not set.
62
+ */
63
+ get activeUnitSystem() {
64
+ return this._explicitActiveUnitSystem ?? IModelApp.quantityFormatter.activeUnitSystem;
65
+ }
66
+ set activeUnitSystem(value) { this._explicitActiveUnitSystem = value; }
67
+ /** Get / set active locale used for localizing presentation data */
68
+ get activeLocale() { return this._localizationHelper.locale; }
69
+ set activeLocale(locale) { this._localizationHelper.locale = locale; }
70
+ dispose() {
71
+ if (this._clearEventListener) {
72
+ this._clearEventListener();
73
+ this._clearEventListener = undefined;
74
+ }
75
+ }
76
+ async onConnection(imodel) {
77
+ if (!this._connections.has(imodel))
78
+ this._connections.set(imodel, this.initializeIModel(imodel));
79
+ await this._connections.get(imodel);
80
+ }
81
+ async initializeIModel(imodel) {
82
+ imodel.onClose.addOnce(() => {
83
+ this._connections.delete(imodel);
84
+ });
85
+ await this.onNewiModelConnection(imodel);
86
+ }
87
+ /** @note This is only called in native apps after changes in iModels */
88
+ async handleUpdateAsync(report) {
89
+ for (const imodelKey in report) {
90
+ // istanbul ignore if
91
+ if (!report.hasOwnProperty(imodelKey))
92
+ continue;
93
+ const imodelReport = report[imodelKey];
94
+ for (const rulesetId in imodelReport) {
95
+ // istanbul ignore if
96
+ if (!imodelReport.hasOwnProperty(rulesetId))
97
+ continue;
98
+ const updateInfo = imodelReport[rulesetId];
99
+ if (updateInfo.content)
100
+ this.onIModelContentChanged.raiseEvent({ rulesetId, updateInfo: updateInfo.content, imodelKey });
101
+ if (updateInfo.hierarchy)
102
+ this.onIModelHierarchyChanged.raiseEvent({ rulesetId, updateInfo: updateInfo.hierarchy, imodelKey });
103
+ }
104
+ }
105
+ }
106
+ /**
107
+ * Function that is called when a new IModelConnection is used to retrieve data.
108
+ * @internal
109
+ */
110
+ async onNewiModelConnection(_) { }
111
+ /**
112
+ * Create a new PresentationManager instance
113
+ * @param props Optional properties used to configure the manager
114
+ */
115
+ static create(props) {
116
+ return new PresentationManager(props);
117
+ }
118
+ /** @internal */
119
+ get rpcRequestsHandler() { return this._requestsHandler; }
120
+ /** @internal */
121
+ get ipcRequestsHandler() { return this._ipcRequestsHandler; }
122
+ /** @internal */
123
+ get stateTracker() { return this._stateTracker; }
124
+ /**
125
+ * Get rulesets manager
126
+ */
127
+ rulesets() { return this._rulesets; }
128
+ /**
129
+ * Get ruleset variables manager for specific ruleset
130
+ * @param rulesetId Id of the ruleset to get the vars manager for
131
+ */
132
+ vars(rulesetId) {
133
+ if (!this._rulesetVars.has(rulesetId)) {
134
+ const varsManager = new RulesetVariablesManagerImpl(rulesetId, this._ipcRequestsHandler);
135
+ this._rulesetVars.set(rulesetId, varsManager);
136
+ }
137
+ return this._rulesetVars.get(rulesetId);
138
+ }
139
+ toRpcTokenOptions(requestOptions) {
140
+ // 1. put default `locale` and `unitSystem`
141
+ // 2. put all `requestOptions` members (if `locale` or `unitSystem` are set, they'll override the defaults put at #1)
142
+ // 3. put `imodel` of type `IModelRpcProps` which overwrites the `imodel` from `requestOptions` put at #2
143
+ const defaultOptions = {};
144
+ if (this.activeLocale)
145
+ defaultOptions.locale = this.activeLocale;
146
+ defaultOptions.unitSystem = this.activeUnitSystem; // eslint-disable-line deprecation/deprecation
147
+ const { imodel, rulesetVariables, ...rpcRequestOptions } = requestOptions;
148
+ return {
149
+ ...defaultOptions,
150
+ ...rpcRequestOptions,
151
+ ...(rulesetVariables ? { rulesetVariables: rulesetVariables.map(RulesetVariable.toJSON) } : {}),
152
+ imodel: imodel.getRpcProps(),
153
+ };
154
+ }
155
+ async addRulesetAndVariablesToOptions(options) {
156
+ const { rulesetOrId, rulesetVariables } = options;
157
+ let foundRulesetOrId;
158
+ if (typeof rulesetOrId === "object") {
159
+ foundRulesetOrId = rulesetOrId;
160
+ }
161
+ else {
162
+ const foundRuleset = await this._rulesets.get(rulesetOrId);
163
+ foundRulesetOrId = foundRuleset ? foundRuleset.toJSON() : rulesetOrId;
164
+ }
165
+ const rulesetId = (typeof foundRulesetOrId === "object") ? foundRulesetOrId.id : foundRulesetOrId;
166
+ // All Id64Array variable values must be sorted for serialization to JSON to work. RulesetVariablesManager
167
+ // sorts them before storing, so that part is taken care of, but we need to ensure that variables coming from
168
+ // request options are also sorted.
169
+ const variables = (rulesetVariables ?? []).map((variable) => {
170
+ if (variable.type === VariableValueTypes.Id64Array)
171
+ return { ...variable, value: OrderedId64Iterable.sortArray(variable.value) };
172
+ return variable;
173
+ });
174
+ if (!this._ipcRequestsHandler) {
175
+ // only need to add variables from variables manager if there's no IPC
176
+ // handler - if there is one, the variables are already known by the backend
177
+ variables.push(...this.vars(rulesetId).getAllVariables());
178
+ }
179
+ return { ...options, rulesetOrId: foundRulesetOrId, rulesetVariables: variables };
180
+ }
181
+ /** Retrieves nodes */
182
+ async getNodes(requestOptions) {
183
+ await this.onConnection(requestOptions.imodel);
184
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
185
+ const rpcOptions = this.toRpcTokenOptions({ ...options });
186
+ const result = await buildPagedArrayResponse(options.paging, async (partialPageOptions) => this._requestsHandler.getPagedNodes({ ...rpcOptions, paging: partialPageOptions }));
187
+ // eslint-disable-next-line deprecation/deprecation
188
+ return this._localizationHelper.getLocalizedNodes(result.items.map(Node.fromJSON));
189
+ }
190
+ /** Retrieves nodes count. */
191
+ async getNodesCount(requestOptions) {
192
+ await this.onConnection(requestOptions.imodel);
193
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
194
+ const rpcOptions = this.toRpcTokenOptions({ ...options });
195
+ return this._requestsHandler.getNodesCount(rpcOptions);
196
+ }
197
+ /** Retrieves total nodes count and a single page of nodes. */
198
+ async getNodesAndCount(requestOptions) {
199
+ await this.onConnection(requestOptions.imodel);
200
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
201
+ const rpcOptions = this.toRpcTokenOptions({ ...options });
202
+ const result = await buildPagedArrayResponse(options.paging, async (partialPageOptions) => this._requestsHandler.getPagedNodes({ ...rpcOptions, paging: partialPageOptions }));
203
+ return {
204
+ count: result.total,
205
+ // eslint-disable-next-line deprecation/deprecation
206
+ nodes: this._localizationHelper.getLocalizedNodes(result.items.map(Node.fromJSON)),
207
+ };
208
+ }
209
+ /**
210
+ * Retrieves hierarchy level descriptor.
211
+ * @beta
212
+ */
213
+ async getNodesDescriptor(requestOptions) {
214
+ await this.onConnection(requestOptions.imodel);
215
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
216
+ const rpcOptions = this.toRpcTokenOptions({ ...options });
217
+ const result = await this._requestsHandler.getNodesDescriptor(rpcOptions);
218
+ return Descriptor.fromJSON(result);
219
+ }
220
+ /** Retrieves paths from root nodes to children nodes according to specified keys. Intersecting paths will be merged. */
221
+ async getNodePaths(requestOptions) {
222
+ await this.onConnection(requestOptions.imodel);
223
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
224
+ const rpcOptions = this.toRpcTokenOptions({ ...options });
225
+ const result = await this._requestsHandler.getNodePaths(rpcOptions);
226
+ // eslint-disable-next-line deprecation/deprecation
227
+ return result.map(NodePathElement.fromJSON);
228
+ }
229
+ /** Retrieves paths from root nodes to nodes containing filter text in their label. */
230
+ async getFilteredNodePaths(requestOptions) {
231
+ await this.onConnection(requestOptions.imodel);
232
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
233
+ const result = await this._requestsHandler.getFilteredNodePaths(this.toRpcTokenOptions(options));
234
+ // eslint-disable-next-line deprecation/deprecation
235
+ return result.map(NodePathElement.fromJSON);
236
+ }
237
+ /**
238
+ * Get information about the sources of content when building it for specific ECClasses. Sources involve classes of the primary select instance,
239
+ * its related instances for loading related and navigation properties.
240
+ * @public
241
+ */
242
+ async getContentSources(requestOptions) {
243
+ await this.onConnection(requestOptions.imodel);
244
+ const rpcOptions = this.toRpcTokenOptions(requestOptions);
245
+ const result = await this._requestsHandler.getContentSources(rpcOptions);
246
+ return SelectClassInfo.listFromCompressedJSON(result.sources, result.classesMap);
247
+ }
248
+ /** Retrieves the content descriptor which describes the content and can be used to customize it. */
249
+ async getContentDescriptor(requestOptions) {
250
+ await this.onConnection(requestOptions.imodel);
251
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
252
+ const rpcOptions = this.toRpcTokenOptions({
253
+ ...options,
254
+ keys: stripTransientElementKeys(options.keys).toJSON(),
255
+ });
256
+ const result = await this._requestsHandler.getContentDescriptor(rpcOptions);
257
+ return Descriptor.fromJSON(result);
258
+ }
259
+ /** Retrieves overall content set size. */
260
+ async getContentSetSize(requestOptions) {
261
+ await this.onConnection(requestOptions.imodel);
262
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
263
+ const rpcOptions = this.toRpcTokenOptions({
264
+ ...options,
265
+ descriptor: getDescriptorOverrides(requestOptions.descriptor),
266
+ keys: stripTransientElementKeys(requestOptions.keys).toJSON(),
267
+ });
268
+ return this._requestsHandler.getContentSetSize(rpcOptions);
269
+ }
270
+ /** Retrieves content which consists of a content descriptor and a page of records. */
271
+ async getContent(requestOptions) {
272
+ return (await this.getContentAndSize(requestOptions))?.content;
273
+ }
274
+ /** Retrieves content set size and content which consists of a content descriptor and a page of records. */
275
+ async getContentAndSize(requestOptions) {
276
+ await this.onConnection(requestOptions.imodel);
277
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
278
+ const rpcOptions = this.toRpcTokenOptions({
279
+ ...options,
280
+ descriptor: getDescriptorOverrides(requestOptions.descriptor),
281
+ keys: stripTransientElementKeys(requestOptions.keys).toJSON(),
282
+ });
283
+ let descriptor = (requestOptions.descriptor instanceof Descriptor) ? requestOptions.descriptor : undefined;
284
+ const result = await buildPagedArrayResponse(options.paging, async (partialPageOptions, requestIndex) => {
285
+ if (0 === requestIndex && !descriptor) {
286
+ const content = await this._requestsHandler.getPagedContent({ ...rpcOptions, paging: partialPageOptions });
287
+ if (content) {
288
+ descriptor = Descriptor.fromJSON(content.descriptor);
289
+ return content.contentSet;
290
+ }
291
+ return { total: 0, items: [] };
292
+ }
293
+ return this._requestsHandler.getPagedContentSet({ ...rpcOptions, paging: partialPageOptions });
294
+ });
295
+ if (!descriptor)
296
+ return undefined;
297
+ const items = result.items.map((itemJson) => Item.fromJSON(itemJson)).filter((item) => (item !== undefined));
298
+ return {
299
+ size: result.total,
300
+ content: this._localizationHelper.getLocalizedContent(new Content(descriptor, items)),
301
+ };
302
+ }
303
+ /** Retrieves distinct values of specific field from the content. */
304
+ async getPagedDistinctValues(requestOptions) {
305
+ await this.onConnection(requestOptions.imodel);
306
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
307
+ const rpcOptions = {
308
+ ...this.toRpcTokenOptions(options),
309
+ descriptor: getDescriptorOverrides(options.descriptor),
310
+ keys: stripTransientElementKeys(options.keys).toJSON(),
311
+ };
312
+ const result = await buildPagedArrayResponse(requestOptions.paging, async (partialPageOptions) => this._requestsHandler.getPagedDistinctValues({ ...rpcOptions, paging: partialPageOptions }));
313
+ return {
314
+ ...result,
315
+ // eslint-disable-next-line deprecation/deprecation
316
+ items: result.items.map(DisplayValueGroup.fromJSON),
317
+ };
318
+ }
319
+ /**
320
+ * Retrieves property data in a simplified format for a single element specified by ID.
321
+ * @public
322
+ */
323
+ async getElementProperties(requestOptions) {
324
+ await this.onConnection(requestOptions.imodel);
325
+ const results = await this._requestsHandler.getElementProperties(this.toRpcTokenOptions(requestOptions));
326
+ // istanbul ignore if
327
+ if (!results)
328
+ return undefined;
329
+ return this._localizationHelper.getLocalizedElementProperties(results);
330
+ }
331
+ /**
332
+ * Retrieves content item instance keys.
333
+ * @public
334
+ */
335
+ async getContentInstanceKeys(requestOptions) {
336
+ await this.onConnection(requestOptions.imodel);
337
+ const options = await this.addRulesetAndVariablesToOptions(requestOptions);
338
+ const rpcOptions = {
339
+ ...this.toRpcTokenOptions(options),
340
+ keys: stripTransientElementKeys(options.keys).toJSON(),
341
+ };
342
+ const props = {
343
+ page: requestOptions.paging,
344
+ get: async (page) => {
345
+ const keys = await this._requestsHandler.getContentInstanceKeys({ ...rpcOptions, paging: page });
346
+ return {
347
+ total: keys.total,
348
+ items: keys.items.instanceKeys.reduce((instanceKeys, entry) => {
349
+ for (const id of CompressedId64Set.iterable(entry[1])) {
350
+ instanceKeys.push({ className: entry[0], id });
351
+ }
352
+ return instanceKeys;
353
+ }, new Array()),
354
+ };
355
+ },
356
+ };
357
+ return createPagedGeneratorResponse(props);
358
+ }
359
+ /** Retrieves display label definition of specific item. */
360
+ async getDisplayLabelDefinition(requestOptions) {
361
+ await this.onConnection(requestOptions.imodel);
362
+ const rpcOptions = this.toRpcTokenOptions({ ...requestOptions });
363
+ const result = await this._requestsHandler.getDisplayLabelDefinition(rpcOptions);
364
+ return this._localizationHelper.getLocalizedLabelDefinition(result);
365
+ }
366
+ /** Retrieves display label definition of specific items. */
367
+ async getDisplayLabelDefinitions(requestOptions) {
368
+ await this.onConnection(requestOptions.imodel);
369
+ const rpcOptions = this.toRpcTokenOptions({ ...requestOptions });
370
+ const result = await buildPagedArrayResponse(undefined, async (partialPageOptions) => {
371
+ const partialKeys = (!partialPageOptions.start) ? rpcOptions.keys : rpcOptions.keys.slice(partialPageOptions.start);
372
+ return this._requestsHandler.getPagedDisplayLabelDefinitions({ ...rpcOptions, keys: partialKeys });
373
+ });
374
+ return this._localizationHelper.getLocalizedLabelDefinitions(result.items);
375
+ }
376
+ }
377
+ const getDescriptorOverrides = (descriptorOrOverrides) => {
378
+ if (descriptorOrOverrides instanceof Descriptor)
379
+ return descriptorOrOverrides.createDescriptorOverrides();
380
+ return descriptorOrOverrides;
381
+ };
382
+ async function createPagedGeneratorResponse(props) {
383
+ let pageStart = props.page?.start ?? 0;
384
+ let pageSize = props.page?.size ?? 0;
385
+ let requestIndex = 0;
386
+ const firstPage = await props.get({ start: pageStart, size: pageSize }, requestIndex++);
387
+ return {
388
+ total: firstPage.total,
389
+ async *items() {
390
+ let partialResult = firstPage;
391
+ while (true) {
392
+ for (const item of partialResult.items) {
393
+ yield item;
394
+ }
395
+ const receivedItemsCount = partialResult.items.length;
396
+ if (partialResult.total !== 0 && receivedItemsCount === 0) {
397
+ if (pageStart >= partialResult.total)
398
+ throw new Error(`Requested page with start index ${pageStart} is out of bounds. Total number of items: ${partialResult.total}`);
399
+ throw new Error("Paged request returned non zero total count but no items");
400
+ }
401
+ if (pageSize !== 0 && receivedItemsCount >= pageSize || receivedItemsCount >= (partialResult.total - pageStart))
402
+ break;
403
+ if (pageSize !== 0)
404
+ pageSize -= receivedItemsCount;
405
+ pageStart += receivedItemsCount;
406
+ partialResult = await props.get({ start: pageStart, size: pageSize }, requestIndex++);
407
+ }
408
+ },
409
+ };
410
+ }
411
+ /** @internal */
412
+ export const buildPagedArrayResponse = async (requestedPage, getter) => {
413
+ const items = new Array();
414
+ const gen = await createPagedGeneratorResponse({ page: requestedPage, get: getter });
415
+ for await (const item of gen.items()) {
416
+ items.push(item);
417
+ }
418
+ return { total: gen.total, items };
419
+ };
420
+ const stripTransientElementKeys = (keys) => {
421
+ if (!keys.some((key) => Key.isInstanceKey(key) && key.className === TRANSIENT_ELEMENT_CLASSNAME))
422
+ return keys;
423
+ const copy = new KeySet();
424
+ copy.add(keys, (key) => {
425
+ // the callback is not going to be called with EntityProps as KeySet converts them
426
+ // to InstanceKeys, but we want to keep the EntityProps case for correctness
427
+ // istanbul ignore next
428
+ const isTransient = Key.isInstanceKey(key) && key.className === TRANSIENT_ELEMENT_CLASSNAME
429
+ || Key.isEntityProps(key) && key.classFullName === TRANSIENT_ELEMENT_CLASSNAME;
430
+ return !isTransient;
431
+ });
432
+ return copy;
433
+ };
440
434
  //# sourceMappingURL=PresentationManager.js.map