@itwin/presentation-backend 3.5.0-dev.5 → 3.5.0-dev.7

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 (61) hide show
  1. package/lib/cjs/assets/primary-presentation-rules/ElementProperties.PresentationRuleSet.json +14 -14
  2. package/lib/cjs/assets/supplemental-presentation-rules/BisCore.PresentationRuleSet.json +761 -761
  3. package/lib/cjs/assets/supplemental-presentation-rules/Functional.PresentationRuleSet.json +139 -139
  4. package/lib/cjs/presentation-backend/BackendLoggerCategory.d.ts +41 -41
  5. package/lib/cjs/presentation-backend/BackendLoggerCategory.js +50 -50
  6. package/lib/cjs/presentation-backend/BackendLoggerCategory.js.map +1 -1
  7. package/lib/cjs/presentation-backend/Constants.d.ts +13 -13
  8. package/lib/cjs/presentation-backend/Constants.js +43 -43
  9. package/lib/cjs/presentation-backend/Constants.js.map +1 -1
  10. package/lib/cjs/presentation-backend/ElementPropertiesHelper.d.ts +11 -11
  11. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js +274 -274
  12. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js.map +1 -1
  13. package/lib/cjs/presentation-backend/NativePlatform.d.ts +83 -83
  14. package/lib/cjs/presentation-backend/NativePlatform.js +140 -140
  15. package/lib/cjs/presentation-backend/NativePlatform.js.map +1 -1
  16. package/lib/cjs/presentation-backend/Presentation.d.ts +106 -106
  17. package/lib/cjs/presentation-backend/Presentation.js +150 -150
  18. package/lib/cjs/presentation-backend/Presentation.js.map +1 -1
  19. package/lib/cjs/presentation-backend/PresentationIpcHandler.d.ts +12 -12
  20. package/lib/cjs/presentation-backend/PresentationIpcHandler.js +41 -41
  21. package/lib/cjs/presentation-backend/PresentationIpcHandler.js.map +1 -1
  22. package/lib/cjs/presentation-backend/PresentationManager.d.ts +401 -401
  23. package/lib/cjs/presentation-backend/PresentationManager.js +291 -291
  24. package/lib/cjs/presentation-backend/PresentationManager.js.map +1 -1
  25. package/lib/cjs/presentation-backend/PresentationManagerDetail.d.ts +60 -60
  26. package/lib/cjs/presentation-backend/PresentationManagerDetail.js +422 -422
  27. package/lib/cjs/presentation-backend/PresentationManagerDetail.js.map +1 -1
  28. package/lib/cjs/presentation-backend/PresentationRpcImpl.d.ts +59 -59
  29. package/lib/cjs/presentation-backend/PresentationRpcImpl.js +364 -364
  30. package/lib/cjs/presentation-backend/PresentationRpcImpl.js.map +1 -1
  31. package/lib/cjs/presentation-backend/RulesetEmbedder.d.ts +104 -104
  32. package/lib/cjs/presentation-backend/RulesetEmbedder.js +282 -282
  33. package/lib/cjs/presentation-backend/RulesetEmbedder.js.map +1 -1
  34. package/lib/cjs/presentation-backend/RulesetManager.d.ts +53 -53
  35. package/lib/cjs/presentation-backend/RulesetManager.js +73 -73
  36. package/lib/cjs/presentation-backend/RulesetManager.js.map +1 -1
  37. package/lib/cjs/presentation-backend/RulesetVariablesManager.d.ts +140 -140
  38. package/lib/cjs/presentation-backend/RulesetVariablesManager.js +129 -129
  39. package/lib/cjs/presentation-backend/RulesetVariablesManager.js.map +1 -1
  40. package/lib/cjs/presentation-backend/SelectionScopesHelper.d.ts +35 -35
  41. package/lib/cjs/presentation-backend/SelectionScopesHelper.js +229 -229
  42. package/lib/cjs/presentation-backend/SelectionScopesHelper.js.map +1 -1
  43. package/lib/cjs/presentation-backend/TemporaryStorage.d.ts +123 -123
  44. package/lib/cjs/presentation-backend/TemporaryStorage.js +151 -151
  45. package/lib/cjs/presentation-backend/TemporaryStorage.js.map +1 -1
  46. package/lib/cjs/presentation-backend/UpdatesTracker.d.ts +27 -27
  47. package/lib/cjs/presentation-backend/UpdatesTracker.js +54 -54
  48. package/lib/cjs/presentation-backend/UpdatesTracker.js.map +1 -1
  49. package/lib/cjs/presentation-backend/Utils.d.ts +49 -49
  50. package/lib/cjs/presentation-backend/Utils.js +106 -106
  51. package/lib/cjs/presentation-backend/Utils.js.map +1 -1
  52. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.d.ts +16 -16
  53. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js +51 -51
  54. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js.map +1 -1
  55. package/lib/cjs/presentation-backend/domain/RulesetElements.d.ts +21 -21
  56. package/lib/cjs/presentation-backend/domain/RulesetElements.js +38 -38
  57. package/lib/cjs/presentation-backend/domain/RulesetElements.js.map +1 -1
  58. package/lib/cjs/presentation-backend.d.ts +19 -19
  59. package/lib/cjs/presentation-backend.js +35 -35
  60. package/lib/cjs/presentation-backend.js.map +1 -1
  61. package/package.json +13 -13
@@ -1,365 +1,365 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module RPC
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.PresentationRpcImpl = exports.MAX_ALLOWED_KEYS_PAGE_SIZE = exports.MAX_ALLOWED_PAGE_SIZE = void 0;
11
- const core_backend_1 = require("@itwin/core-backend");
12
- const core_bentley_1 = require("@itwin/core-bentley");
13
- const presentation_common_1 = require("@itwin/presentation-common");
14
- const BackendLoggerCategory_1 = require("./BackendLoggerCategory");
15
- const Presentation_1 = require("./Presentation");
16
- const TemporaryStorage_1 = require("./TemporaryStorage");
17
- // eslint-disable-next-line @typescript-eslint/no-var-requires
18
- const packageJsonVersion = require("../../../package.json").version;
19
- /** @internal */
20
- exports.MAX_ALLOWED_PAGE_SIZE = 1000;
21
- /** @internal */
22
- exports.MAX_ALLOWED_KEYS_PAGE_SIZE = 10000;
23
- /**
24
- * The backend implementation of PresentationRpcInterface. All it's basically
25
- * responsible for is forwarding calls to [[Presentation.manager]].
26
- *
27
- * @internal
28
- */
29
- class PresentationRpcImpl extends presentation_common_1.PresentationRpcInterface {
30
- constructor(props) {
31
- var _a;
32
- super();
33
- this._requestTimeout = (_a = props === null || props === void 0 ? void 0 : props.requestTimeout) !== null && _a !== void 0 ? _a : 90 * 1000;
34
- this._pendingRequests = new TemporaryStorage_1.TemporaryStorage({
35
- // remove the pending request after request timeout + 10 seconds - this gives
36
- // frontend 10 seconds to re-send the request until it's removed from requests' cache
37
- unusedValueLifetime: this._requestTimeout + 10 * 1000,
38
- // attempt to clean up every second
39
- cleanupInterval: 1000,
40
- cleanupHandler: (id, _, reason) => {
41
- var _a;
42
- if (reason !== "request") {
43
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Cleaning up request without frontend retrieving it: ${id}.`);
44
- // istanbul ignore next
45
- (_a = this._cancelEvents.get(id)) === null || _a === void 0 ? void 0 : _a.raiseEvent();
46
- }
47
- this._cancelEvents.delete(id);
48
- },
49
- });
50
- this._cancelEvents = new Map();
51
- }
52
- dispose() {
53
- this._pendingRequests.dispose();
54
- }
55
- get requestTimeout() { return this._requestTimeout; }
56
- /** Returns an ok response with result inside */
57
- successResponse(result, diagnostics) {
58
- return {
59
- statusCode: presentation_common_1.PresentationStatus.Success,
60
- result,
61
- diagnostics,
62
- };
63
- }
64
- /** Returns a bad request response with empty result and an error code */
65
- errorResponse(errorCode, errorMessage, diagnostics) {
66
- return {
67
- statusCode: errorCode,
68
- result: undefined,
69
- errorMessage,
70
- diagnostics,
71
- };
72
- }
73
- /**
74
- * Get the [[PresentationManager]] used by this RPC impl.
75
- */
76
- getManager(clientId) {
77
- return Presentation_1.Presentation.getManager(clientId);
78
- }
79
- getIModel(token) {
80
- let imodel;
81
- try {
82
- imodel = core_backend_1.IModelDb.findByKey(token.key);
83
- }
84
- catch {
85
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.InvalidArgument, "IModelRpcProps doesn't point to a valid iModel");
86
- }
87
- return imodel;
88
- }
89
- async makeRequest(token, requestId, requestOptions, request) {
90
- const requestKey = JSON.stringify({ iModelKey: token.key, requestId, requestOptions });
91
- core_bentley_1.Logger.logInfo(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Received '${requestId}' request. Params: ${requestKey}`);
92
- let resultPromise = this._pendingRequests.getValue(requestKey);
93
- if (resultPromise) {
94
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request already pending`);
95
- }
96
- else {
97
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request not found, creating a new one`);
98
- let imodel;
99
- try {
100
- imodel = this.getIModel(token);
101
- }
102
- catch (e) {
103
- (0, core_bentley_1.assert)(e instanceof Error);
104
- return this.errorResponse(presentation_common_1.PresentationStatus.InvalidArgument, e.message);
105
- }
106
- const { clientId: _, diagnostics: diagnosticsOptions, rulesetVariables, ...options } = requestOptions;
107
- const managerRequestOptions = {
108
- ...options,
109
- imodel,
110
- cancelEvent: new core_bentley_1.BeEvent(),
111
- };
112
- // set up ruleset variables
113
- if (rulesetVariables)
114
- managerRequestOptions.rulesetVariables = rulesetVariables.map(presentation_common_1.RulesetVariable.fromJSON);
115
- // set up diagnostics listener
116
- let diagnostics;
117
- const getDiagnostics = () => {
118
- if (!diagnostics)
119
- diagnostics = {};
120
- return diagnostics;
121
- };
122
- if (diagnosticsOptions) {
123
- if (diagnosticsOptions.backendVersion) {
124
- getDiagnostics().backendVersion = packageJsonVersion;
125
- }
126
- managerRequestOptions.diagnostics = {
127
- ...diagnosticsOptions,
128
- handler: (d) => {
129
- if (d.logs) {
130
- const target = getDiagnostics();
131
- if (target.logs)
132
- target.logs.push(...d.logs);
133
- else
134
- target.logs = [...d.logs];
135
- }
136
- },
137
- };
138
- }
139
- // initiate request
140
- resultPromise = request(managerRequestOptions)
141
- .then((result) => this.successResponse(result, diagnostics))
142
- .catch((e) => this.errorResponse(e.errorNumber, e.message, diagnostics));
143
- // store the request promise
144
- this._pendingRequests.addValue(requestKey, resultPromise);
145
- this._cancelEvents.set(requestKey, managerRequestOptions.cancelEvent);
146
- }
147
- if (this._requestTimeout === 0) {
148
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout not configured, returning promise without a timeout.`);
149
- resultPromise.finally(() => {
150
- this._pendingRequests.deleteValue(requestKey);
151
- });
152
- return resultPromise;
153
- }
154
- let timeout;
155
- const timeoutPromise = new Promise((_resolve, reject) => {
156
- timeout = setTimeout(() => {
157
- reject("Timed out");
158
- }, this._requestTimeout);
159
- });
160
- /* eslint-disable @typescript-eslint/indent */
161
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Returning a promise with a timeout of ${this._requestTimeout}.`);
162
- return Promise
163
- .race([resultPromise, timeoutPromise])
164
- .catch(() => {
165
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout, returning "BackendTimeout" status.`);
166
- return this.errorResponse(presentation_common_1.PresentationStatus.BackendTimeout);
167
- })
168
- .then((response) => {
169
- if (response.statusCode !== presentation_common_1.PresentationStatus.BackendTimeout) {
170
- core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request completed, returning result.`);
171
- this._pendingRequests.deleteValue(requestKey);
172
- }
173
- clearTimeout(timeout);
174
- return response;
175
- });
176
- /* eslint-enable @typescript-eslint/indent */
177
- }
178
- async getNodesCount(token, requestOptions) {
179
- return this.makeRequest(token, "getNodesCount", requestOptions, async (options) => {
180
- options = {
181
- ...options,
182
- parentKey: nodeKeyFromJson(options.parentKey),
183
- };
184
- return this.getManager(requestOptions.clientId).getNodesCount(options);
185
- });
186
- }
187
- async getPagedNodes(token, requestOptions) {
188
- return this.makeRequest(token, "getPagedNodes", requestOptions, async (options) => {
189
- options = enforceValidPageSize({
190
- ...options,
191
- parentKey: nodeKeyFromJson(options.parentKey),
192
- });
193
- const [nodes, count] = await Promise.all([
194
- this.getManager(requestOptions.clientId).getDetail().getNodes(options),
195
- this.getManager(requestOptions.clientId).getNodesCount(options),
196
- ]);
197
- return { total: count, items: nodes.map(presentation_common_1.Node.toJSON) };
198
- });
199
- }
200
- async getNodePaths(token, requestOptions) {
201
- return this.makeRequest(token, "getNodePaths", requestOptions, async (options) => {
202
- const result = await this.getManager(requestOptions.clientId).getNodePaths(options);
203
- return result.map(presentation_common_1.NodePathElement.toJSON);
204
- });
205
- }
206
- async getFilteredNodePaths(token, requestOptions) {
207
- return this.makeRequest(token, "getFilteredNodePaths", requestOptions, async (options) => {
208
- const result = await this.getManager(requestOptions.clientId).getFilteredNodePaths(options);
209
- return result.map(presentation_common_1.NodePathElement.toJSON);
210
- });
211
- }
212
- async getContentSources(token, requestOptions) {
213
- return this.makeRequest(token, "getContentSources", requestOptions, async (options) => {
214
- const result = await this.getManager(requestOptions.clientId).getContentSources(options);
215
- const classesMap = {};
216
- const selectClasses = result.map((sci) => presentation_common_1.SelectClassInfo.toCompressedJSON(sci, classesMap));
217
- return { sources: selectClasses, classesMap };
218
- });
219
- }
220
- async getContentDescriptor(token, requestOptions) {
221
- return this.makeRequest(token, "getContentDescriptor", requestOptions, async (options) => {
222
- options = {
223
- ...options,
224
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
225
- };
226
- if (options.transport === "unparsed-json") {
227
- // Here we send a plain JSON string but we will parse it to DescriptorJSON on the frontend. This way we are
228
- // bypassing unnecessary deserialization and serialization.
229
- return Presentation_1.Presentation.getManager().getDetail().getContentDescriptor(options);
230
- }
231
- else {
232
- // Support for older frontends that still expect a parsed descriptor
233
- const descriptor = await Presentation_1.Presentation.getManager().getContentDescriptor(options);
234
- return descriptor === null || descriptor === void 0 ? void 0 : descriptor.toJSON();
235
- }
236
- });
237
- }
238
- async getContentSetSize(token, requestOptions) {
239
- return this.makeRequest(token, "getContentSetSize", requestOptions, async (options) => {
240
- options = {
241
- ...options,
242
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
243
- };
244
- return this.getManager(requestOptions.clientId).getContentSetSize(options);
245
- });
246
- }
247
- async getPagedContent(token, requestOptions) {
248
- return this.makeRequest(token, "getPagedContent", requestOptions, async (options) => {
249
- options = enforceValidPageSize({
250
- ...options,
251
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
252
- });
253
- const [size, content] = await Promise.all([
254
- this.getManager(requestOptions.clientId).getContentSetSize(options),
255
- this.getManager(requestOptions.clientId).getDetail().getContent(options),
256
- ]);
257
- if (!content)
258
- return undefined;
259
- return {
260
- descriptor: content.descriptor.toJSON(),
261
- contentSet: {
262
- total: size,
263
- items: content.contentSet.map((i) => i.toJSON()),
264
- },
265
- };
266
- });
267
- }
268
- async getPagedContentSet(token, requestOptions) {
269
- const content = await this.getPagedContent(token, requestOptions);
270
- return this.successResponse(content.result ? content.result.contentSet : { total: 0, items: [] });
271
- }
272
- async getElementProperties(token, requestOptions) {
273
- return this.makeRequest(token, "getElementProperties", { ...requestOptions }, async (options) => {
274
- return this.getManager(requestOptions.clientId).getDetail().getElementProperties(options);
275
- });
276
- }
277
- async getPagedDistinctValues(token, requestOptions) {
278
- return this.makeRequest(token, "getPagedDistinctValues", requestOptions, async (options) => {
279
- options = enforceValidPageSize({
280
- ...options,
281
- keys: presentation_common_1.KeySet.fromJSON(options.keys),
282
- });
283
- const response = await this.getManager(requestOptions.clientId).getPagedDistinctValues(options);
284
- return {
285
- ...response,
286
- items: response.items.map(presentation_common_1.DisplayValueGroup.toJSON),
287
- };
288
- });
289
- }
290
- async getContentInstanceKeys(token, requestOptions) {
291
- return this.makeRequest(token, "getContentInstanceKeys", requestOptions, async (options) => {
292
- const { displayType, ...optionsNoDisplayType } = options;
293
- options = enforceValidPageSize({
294
- ...optionsNoDisplayType,
295
- keys: presentation_common_1.KeySet.fromJSON(optionsNoDisplayType.keys),
296
- descriptor: {
297
- displayType,
298
- contentFlags: presentation_common_1.ContentFlags.KeysOnly,
299
- },
300
- }, exports.MAX_ALLOWED_KEYS_PAGE_SIZE);
301
- const [size, content] = await Promise.all([
302
- this.getManager(requestOptions.clientId).getContentSetSize(options),
303
- this.getManager(requestOptions.clientId).getDetail().getContent(options),
304
- ]);
305
- if (size === 0 || !content)
306
- return { total: 0, items: new presentation_common_1.KeySet().toJSON() };
307
- return {
308
- total: size,
309
- items: content.contentSet.reduce((keys, item) => keys.add(item.primaryKeys), new presentation_common_1.KeySet()).toJSON(),
310
- };
311
- });
312
- }
313
- async getDisplayLabelDefinition(token, requestOptions) {
314
- return this.makeRequest(token, "getDisplayLabelDefinition", requestOptions, async (options) => {
315
- const label = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinition(options);
316
- return presentation_common_1.LabelDefinition.toJSON(label);
317
- });
318
- }
319
- async getPagedDisplayLabelDefinitions(token, requestOptions) {
320
- const pageOpts = enforceValidPageSize({ paging: { start: 0, size: requestOptions.keys.length } });
321
- if (pageOpts.paging.size < requestOptions.keys.length)
322
- requestOptions.keys.splice(pageOpts.paging.size);
323
- return this.makeRequest(token, "getPagedDisplayLabelDefinitions", requestOptions, async (options) => {
324
- const labels = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinitions({ ...options, keys: options.keys.map(presentation_common_1.InstanceKey.fromJSON) });
325
- return {
326
- total: options.keys.length,
327
- items: labels.map(presentation_common_1.LabelDefinition.toJSON),
328
- };
329
- });
330
- }
331
- async getSelectionScopes(token, requestOptions) {
332
- return this.makeRequest(token, "getSelectionScopes", requestOptions, async (options) => this.getManager(requestOptions.clientId).getSelectionScopes(options));
333
- }
334
- async computeSelection(token, requestOptions, ids, scopeId) {
335
- return this.makeRequest(token, "computeSelection", requestOptions, async (options) => {
336
- if (!(0, presentation_common_1.isComputeSelectionRequestOptions)(options)) {
337
- options = {
338
- ...options,
339
- elementIds: ids,
340
- scope: { id: scopeId },
341
- };
342
- }
343
- const keys = await this.getManager(requestOptions.clientId).computeSelection(options);
344
- return keys.toJSON();
345
- });
346
- }
347
- }
348
- exports.PresentationRpcImpl = PresentationRpcImpl;
349
- const enforceValidPageSize = (requestOptions, maxPageSize = exports.MAX_ALLOWED_PAGE_SIZE) => {
350
- var _a;
351
- const validPageSize = getValidPageSize((_a = requestOptions.paging) === null || _a === void 0 ? void 0 : _a.size, maxPageSize);
352
- if (!requestOptions.paging || requestOptions.paging.size !== validPageSize)
353
- return { ...requestOptions, paging: { ...requestOptions.paging, size: validPageSize } };
354
- return requestOptions;
355
- };
356
- const getValidPageSize = (size, maxPageSize) => {
357
- const requestedSize = size !== null && size !== void 0 ? size : 0;
358
- return (requestedSize === 0 || requestedSize > maxPageSize) ? maxPageSize : requestedSize;
359
- };
360
- const nodeKeyFromJson = (json) => {
361
- if (!json)
362
- return undefined;
363
- return presentation_common_1.NodeKey.fromJSON(json);
364
- };
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module RPC
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.PresentationRpcImpl = exports.MAX_ALLOWED_KEYS_PAGE_SIZE = exports.MAX_ALLOWED_PAGE_SIZE = void 0;
11
+ const core_backend_1 = require("@itwin/core-backend");
12
+ const core_bentley_1 = require("@itwin/core-bentley");
13
+ const presentation_common_1 = require("@itwin/presentation-common");
14
+ const BackendLoggerCategory_1 = require("./BackendLoggerCategory");
15
+ const Presentation_1 = require("./Presentation");
16
+ const TemporaryStorage_1 = require("./TemporaryStorage");
17
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
18
+ const packageJsonVersion = require("../../../package.json").version;
19
+ /** @internal */
20
+ exports.MAX_ALLOWED_PAGE_SIZE = 1000;
21
+ /** @internal */
22
+ exports.MAX_ALLOWED_KEYS_PAGE_SIZE = 10000;
23
+ /**
24
+ * The backend implementation of PresentationRpcInterface. All it's basically
25
+ * responsible for is forwarding calls to [[Presentation.manager]].
26
+ *
27
+ * @internal
28
+ */
29
+ class PresentationRpcImpl extends presentation_common_1.PresentationRpcInterface {
30
+ constructor(props) {
31
+ var _a;
32
+ super();
33
+ this._requestTimeout = (_a = props === null || props === void 0 ? void 0 : props.requestTimeout) !== null && _a !== void 0 ? _a : 90 * 1000;
34
+ this._pendingRequests = new TemporaryStorage_1.TemporaryStorage({
35
+ // remove the pending request after request timeout + 10 seconds - this gives
36
+ // frontend 10 seconds to re-send the request until it's removed from requests' cache
37
+ unusedValueLifetime: this._requestTimeout + 10 * 1000,
38
+ // attempt to clean up every second
39
+ cleanupInterval: 1000,
40
+ cleanupHandler: (id, _, reason) => {
41
+ var _a;
42
+ if (reason !== "request") {
43
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Cleaning up request without frontend retrieving it: ${id}.`);
44
+ // istanbul ignore next
45
+ (_a = this._cancelEvents.get(id)) === null || _a === void 0 ? void 0 : _a.raiseEvent();
46
+ }
47
+ this._cancelEvents.delete(id);
48
+ },
49
+ });
50
+ this._cancelEvents = new Map();
51
+ }
52
+ dispose() {
53
+ this._pendingRequests.dispose();
54
+ }
55
+ get requestTimeout() { return this._requestTimeout; }
56
+ /** Returns an ok response with result inside */
57
+ successResponse(result, diagnostics) {
58
+ return {
59
+ statusCode: presentation_common_1.PresentationStatus.Success,
60
+ result,
61
+ diagnostics,
62
+ };
63
+ }
64
+ /** Returns a bad request response with empty result and an error code */
65
+ errorResponse(errorCode, errorMessage, diagnostics) {
66
+ return {
67
+ statusCode: errorCode,
68
+ result: undefined,
69
+ errorMessage,
70
+ diagnostics,
71
+ };
72
+ }
73
+ /**
74
+ * Get the [[PresentationManager]] used by this RPC impl.
75
+ */
76
+ getManager(clientId) {
77
+ return Presentation_1.Presentation.getManager(clientId);
78
+ }
79
+ getIModel(token) {
80
+ let imodel;
81
+ try {
82
+ imodel = core_backend_1.IModelDb.findByKey(token.key);
83
+ }
84
+ catch {
85
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.InvalidArgument, "IModelRpcProps doesn't point to a valid iModel");
86
+ }
87
+ return imodel;
88
+ }
89
+ async makeRequest(token, requestId, requestOptions, request) {
90
+ const requestKey = JSON.stringify({ iModelKey: token.key, requestId, requestOptions });
91
+ core_bentley_1.Logger.logInfo(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Received '${requestId}' request. Params: ${requestKey}`);
92
+ let resultPromise = this._pendingRequests.getValue(requestKey);
93
+ if (resultPromise) {
94
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request already pending`);
95
+ }
96
+ else {
97
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request not found, creating a new one`);
98
+ let imodel;
99
+ try {
100
+ imodel = this.getIModel(token);
101
+ }
102
+ catch (e) {
103
+ (0, core_bentley_1.assert)(e instanceof Error);
104
+ return this.errorResponse(presentation_common_1.PresentationStatus.InvalidArgument, e.message);
105
+ }
106
+ const { clientId: _, diagnostics: diagnosticsOptions, rulesetVariables, ...options } = requestOptions;
107
+ const managerRequestOptions = {
108
+ ...options,
109
+ imodel,
110
+ cancelEvent: new core_bentley_1.BeEvent(),
111
+ };
112
+ // set up ruleset variables
113
+ if (rulesetVariables)
114
+ managerRequestOptions.rulesetVariables = rulesetVariables.map(presentation_common_1.RulesetVariable.fromJSON);
115
+ // set up diagnostics listener
116
+ let diagnostics;
117
+ const getDiagnostics = () => {
118
+ if (!diagnostics)
119
+ diagnostics = {};
120
+ return diagnostics;
121
+ };
122
+ if (diagnosticsOptions) {
123
+ if (diagnosticsOptions.backendVersion) {
124
+ getDiagnostics().backendVersion = packageJsonVersion;
125
+ }
126
+ managerRequestOptions.diagnostics = {
127
+ ...diagnosticsOptions,
128
+ handler: (d) => {
129
+ if (d.logs) {
130
+ const target = getDiagnostics();
131
+ if (target.logs)
132
+ target.logs.push(...d.logs);
133
+ else
134
+ target.logs = [...d.logs];
135
+ }
136
+ },
137
+ };
138
+ }
139
+ // initiate request
140
+ resultPromise = request(managerRequestOptions)
141
+ .then((result) => this.successResponse(result, diagnostics))
142
+ .catch((e) => this.errorResponse(e.errorNumber, e.message, diagnostics));
143
+ // store the request promise
144
+ this._pendingRequests.addValue(requestKey, resultPromise);
145
+ this._cancelEvents.set(requestKey, managerRequestOptions.cancelEvent);
146
+ }
147
+ if (this._requestTimeout === 0) {
148
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout not configured, returning promise without a timeout.`);
149
+ resultPromise.finally(() => {
150
+ this._pendingRequests.deleteValue(requestKey);
151
+ });
152
+ return resultPromise;
153
+ }
154
+ let timeout;
155
+ const timeoutPromise = new Promise((_resolve, reject) => {
156
+ timeout = setTimeout(() => {
157
+ reject("Timed out");
158
+ }, this._requestTimeout);
159
+ });
160
+ /* eslint-disable @typescript-eslint/indent */
161
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Returning a promise with a timeout of ${this._requestTimeout}.`);
162
+ return Promise
163
+ .race([resultPromise, timeoutPromise])
164
+ .catch(() => {
165
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request timeout, returning "BackendTimeout" status.`);
166
+ return this.errorResponse(presentation_common_1.PresentationStatus.BackendTimeout);
167
+ })
168
+ .then((response) => {
169
+ if (response.statusCode !== presentation_common_1.PresentationStatus.BackendTimeout) {
170
+ core_bentley_1.Logger.logTrace(BackendLoggerCategory_1.PresentationBackendLoggerCategory.Rpc, `Request completed, returning result.`);
171
+ this._pendingRequests.deleteValue(requestKey);
172
+ }
173
+ clearTimeout(timeout);
174
+ return response;
175
+ });
176
+ /* eslint-enable @typescript-eslint/indent */
177
+ }
178
+ async getNodesCount(token, requestOptions) {
179
+ return this.makeRequest(token, "getNodesCount", requestOptions, async (options) => {
180
+ options = {
181
+ ...options,
182
+ parentKey: nodeKeyFromJson(options.parentKey),
183
+ };
184
+ return this.getManager(requestOptions.clientId).getNodesCount(options);
185
+ });
186
+ }
187
+ async getPagedNodes(token, requestOptions) {
188
+ return this.makeRequest(token, "getPagedNodes", requestOptions, async (options) => {
189
+ options = enforceValidPageSize({
190
+ ...options,
191
+ parentKey: nodeKeyFromJson(options.parentKey),
192
+ });
193
+ const [nodes, count] = await Promise.all([
194
+ this.getManager(requestOptions.clientId).getDetail().getNodes(options),
195
+ this.getManager(requestOptions.clientId).getNodesCount(options),
196
+ ]);
197
+ return { total: count, items: nodes.map(presentation_common_1.Node.toJSON) };
198
+ });
199
+ }
200
+ async getNodePaths(token, requestOptions) {
201
+ return this.makeRequest(token, "getNodePaths", requestOptions, async (options) => {
202
+ const result = await this.getManager(requestOptions.clientId).getNodePaths(options);
203
+ return result.map(presentation_common_1.NodePathElement.toJSON);
204
+ });
205
+ }
206
+ async getFilteredNodePaths(token, requestOptions) {
207
+ return this.makeRequest(token, "getFilteredNodePaths", requestOptions, async (options) => {
208
+ const result = await this.getManager(requestOptions.clientId).getFilteredNodePaths(options);
209
+ return result.map(presentation_common_1.NodePathElement.toJSON);
210
+ });
211
+ }
212
+ async getContentSources(token, requestOptions) {
213
+ return this.makeRequest(token, "getContentSources", requestOptions, async (options) => {
214
+ const result = await this.getManager(requestOptions.clientId).getContentSources(options);
215
+ const classesMap = {};
216
+ const selectClasses = result.map((sci) => presentation_common_1.SelectClassInfo.toCompressedJSON(sci, classesMap));
217
+ return { sources: selectClasses, classesMap };
218
+ });
219
+ }
220
+ async getContentDescriptor(token, requestOptions) {
221
+ return this.makeRequest(token, "getContentDescriptor", requestOptions, async (options) => {
222
+ options = {
223
+ ...options,
224
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
225
+ };
226
+ if (options.transport === "unparsed-json") {
227
+ // Here we send a plain JSON string but we will parse it to DescriptorJSON on the frontend. This way we are
228
+ // bypassing unnecessary deserialization and serialization.
229
+ return Presentation_1.Presentation.getManager().getDetail().getContentDescriptor(options);
230
+ }
231
+ else {
232
+ // Support for older frontends that still expect a parsed descriptor
233
+ const descriptor = await Presentation_1.Presentation.getManager().getContentDescriptor(options);
234
+ return descriptor === null || descriptor === void 0 ? void 0 : descriptor.toJSON();
235
+ }
236
+ });
237
+ }
238
+ async getContentSetSize(token, requestOptions) {
239
+ return this.makeRequest(token, "getContentSetSize", requestOptions, async (options) => {
240
+ options = {
241
+ ...options,
242
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
243
+ };
244
+ return this.getManager(requestOptions.clientId).getContentSetSize(options);
245
+ });
246
+ }
247
+ async getPagedContent(token, requestOptions) {
248
+ return this.makeRequest(token, "getPagedContent", requestOptions, async (options) => {
249
+ options = enforceValidPageSize({
250
+ ...options,
251
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
252
+ });
253
+ const [size, content] = await Promise.all([
254
+ this.getManager(requestOptions.clientId).getContentSetSize(options),
255
+ this.getManager(requestOptions.clientId).getDetail().getContent(options),
256
+ ]);
257
+ if (!content)
258
+ return undefined;
259
+ return {
260
+ descriptor: content.descriptor.toJSON(),
261
+ contentSet: {
262
+ total: size,
263
+ items: content.contentSet.map((i) => i.toJSON()),
264
+ },
265
+ };
266
+ });
267
+ }
268
+ async getPagedContentSet(token, requestOptions) {
269
+ const content = await this.getPagedContent(token, requestOptions);
270
+ return this.successResponse(content.result ? content.result.contentSet : { total: 0, items: [] });
271
+ }
272
+ async getElementProperties(token, requestOptions) {
273
+ return this.makeRequest(token, "getElementProperties", { ...requestOptions }, async (options) => {
274
+ return this.getManager(requestOptions.clientId).getDetail().getElementProperties(options);
275
+ });
276
+ }
277
+ async getPagedDistinctValues(token, requestOptions) {
278
+ return this.makeRequest(token, "getPagedDistinctValues", requestOptions, async (options) => {
279
+ options = enforceValidPageSize({
280
+ ...options,
281
+ keys: presentation_common_1.KeySet.fromJSON(options.keys),
282
+ });
283
+ const response = await this.getManager(requestOptions.clientId).getPagedDistinctValues(options);
284
+ return {
285
+ ...response,
286
+ items: response.items.map(presentation_common_1.DisplayValueGroup.toJSON),
287
+ };
288
+ });
289
+ }
290
+ async getContentInstanceKeys(token, requestOptions) {
291
+ return this.makeRequest(token, "getContentInstanceKeys", requestOptions, async (options) => {
292
+ const { displayType, ...optionsNoDisplayType } = options;
293
+ options = enforceValidPageSize({
294
+ ...optionsNoDisplayType,
295
+ keys: presentation_common_1.KeySet.fromJSON(optionsNoDisplayType.keys),
296
+ descriptor: {
297
+ displayType,
298
+ contentFlags: presentation_common_1.ContentFlags.KeysOnly,
299
+ },
300
+ }, exports.MAX_ALLOWED_KEYS_PAGE_SIZE);
301
+ const [size, content] = await Promise.all([
302
+ this.getManager(requestOptions.clientId).getContentSetSize(options),
303
+ this.getManager(requestOptions.clientId).getDetail().getContent(options),
304
+ ]);
305
+ if (size === 0 || !content)
306
+ return { total: 0, items: new presentation_common_1.KeySet().toJSON() };
307
+ return {
308
+ total: size,
309
+ items: content.contentSet.reduce((keys, item) => keys.add(item.primaryKeys), new presentation_common_1.KeySet()).toJSON(),
310
+ };
311
+ });
312
+ }
313
+ async getDisplayLabelDefinition(token, requestOptions) {
314
+ return this.makeRequest(token, "getDisplayLabelDefinition", requestOptions, async (options) => {
315
+ const label = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinition(options);
316
+ return presentation_common_1.LabelDefinition.toJSON(label);
317
+ });
318
+ }
319
+ async getPagedDisplayLabelDefinitions(token, requestOptions) {
320
+ const pageOpts = enforceValidPageSize({ paging: { start: 0, size: requestOptions.keys.length } });
321
+ if (pageOpts.paging.size < requestOptions.keys.length)
322
+ requestOptions.keys.splice(pageOpts.paging.size);
323
+ return this.makeRequest(token, "getPagedDisplayLabelDefinitions", requestOptions, async (options) => {
324
+ const labels = await this.getManager(requestOptions.clientId).getDetail().getDisplayLabelDefinitions({ ...options, keys: options.keys.map(presentation_common_1.InstanceKey.fromJSON) });
325
+ return {
326
+ total: options.keys.length,
327
+ items: labels.map(presentation_common_1.LabelDefinition.toJSON),
328
+ };
329
+ });
330
+ }
331
+ async getSelectionScopes(token, requestOptions) {
332
+ return this.makeRequest(token, "getSelectionScopes", requestOptions, async (options) => this.getManager(requestOptions.clientId).getSelectionScopes(options));
333
+ }
334
+ async computeSelection(token, requestOptions, ids, scopeId) {
335
+ return this.makeRequest(token, "computeSelection", requestOptions, async (options) => {
336
+ if (!(0, presentation_common_1.isComputeSelectionRequestOptions)(options)) {
337
+ options = {
338
+ ...options,
339
+ elementIds: ids,
340
+ scope: { id: scopeId },
341
+ };
342
+ }
343
+ const keys = await this.getManager(requestOptions.clientId).computeSelection(options);
344
+ return keys.toJSON();
345
+ });
346
+ }
347
+ }
348
+ exports.PresentationRpcImpl = PresentationRpcImpl;
349
+ const enforceValidPageSize = (requestOptions, maxPageSize = exports.MAX_ALLOWED_PAGE_SIZE) => {
350
+ var _a;
351
+ const validPageSize = getValidPageSize((_a = requestOptions.paging) === null || _a === void 0 ? void 0 : _a.size, maxPageSize);
352
+ if (!requestOptions.paging || requestOptions.paging.size !== validPageSize)
353
+ return { ...requestOptions, paging: { ...requestOptions.paging, size: validPageSize } };
354
+ return requestOptions;
355
+ };
356
+ const getValidPageSize = (size, maxPageSize) => {
357
+ const requestedSize = size !== null && size !== void 0 ? size : 0;
358
+ return (requestedSize === 0 || requestedSize > maxPageSize) ? maxPageSize : requestedSize;
359
+ };
360
+ const nodeKeyFromJson = (json) => {
361
+ if (!json)
362
+ return undefined;
363
+ return presentation_common_1.NodeKey.fromJSON(json);
364
+ };
365
365
  //# sourceMappingURL=PresentationRpcImpl.js.map