@itwin/presentation-frontend 3.7.0-dev.6 → 3.7.0-dev.8

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 (124) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  3. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js +47 -47
  4. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js.map +1 -1
  5. package/lib/cjs/presentation-frontend/Diagnostics.d.ts +14 -14
  6. package/lib/cjs/presentation-frontend/Diagnostics.js +61 -61
  7. package/lib/cjs/presentation-frontend/Diagnostics.js.map +1 -1
  8. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  9. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js +20 -20
  10. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js.map +1 -1
  11. package/lib/cjs/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  12. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js +41 -41
  13. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js.map +1 -1
  14. package/lib/cjs/presentation-frontend/LocalizationHelper.d.ts +10 -10
  15. package/lib/cjs/presentation-frontend/LocalizationHelper.js +31 -31
  16. package/lib/cjs/presentation-frontend/LocalizationHelper.js.map +1 -1
  17. package/lib/cjs/presentation-frontend/Presentation.d.ts +75 -75
  18. package/lib/cjs/presentation-frontend/Presentation.js +162 -162
  19. package/lib/cjs/presentation-frontend/Presentation.js.map +1 -1
  20. package/lib/cjs/presentation-frontend/PresentationManager.d.ts +186 -186
  21. package/lib/cjs/presentation-frontend/PresentationManager.js +436 -436
  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 +129 -129
  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 +526 -526
  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 +293 -293
  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 +14 -14
  52. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js +37 -37
  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 +75 -75
  79. package/lib/esm/presentation-frontend/Presentation.js +158 -158
  80. package/lib/esm/presentation-frontend/Presentation.js.map +1 -1
  81. package/lib/esm/presentation-frontend/PresentationManager.d.ts +186 -186
  82. package/lib/esm/presentation-frontend/PresentationManager.js +431 -431
  83. package/lib/esm/presentation-frontend/PresentationManager.js.map +1 -1
  84. package/lib/esm/presentation-frontend/RulesetManager.d.ts +63 -63
  85. package/lib/esm/presentation-frontend/RulesetManager.js +81 -81
  86. package/lib/esm/presentation-frontend/RulesetManager.js.map +1 -1
  87. package/lib/esm/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  88. package/lib/esm/presentation-frontend/RulesetVariablesManager.js +191 -191
  89. package/lib/esm/presentation-frontend/RulesetVariablesManager.js.map +1 -1
  90. package/lib/esm/presentation-frontend/StateTracker.d.ts +38 -38
  91. package/lib/esm/presentation-frontend/StateTracker.js +125 -125
  92. package/lib/esm/presentation-frontend/StateTracker.js.map +1 -1
  93. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  94. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +520 -520
  95. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  96. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  97. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +285 -285
  98. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  99. package/lib/esm/presentation-frontend/selection/HiliteRules.json +172 -172
  100. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  101. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js +93 -93
  102. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js.map +1 -1
  103. package/lib/esm/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  104. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js +8 -8
  105. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js.map +1 -1
  106. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  107. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js +29 -29
  108. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js.map +1 -1
  109. package/lib/esm/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  110. package/lib/esm/presentation-frontend/selection/SelectionHandler.js +104 -104
  111. package/lib/esm/presentation-frontend/selection/SelectionHandler.js.map +1 -1
  112. package/lib/esm/presentation-frontend/selection/SelectionHelper.d.ts +14 -14
  113. package/lib/esm/presentation-frontend/selection/SelectionHelper.js +33 -33
  114. package/lib/esm/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  115. package/lib/esm/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  116. package/lib/esm/presentation-frontend/selection/SelectionManager.js +409 -409
  117. package/lib/esm/presentation-frontend/selection/SelectionManager.js.map +1 -1
  118. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  119. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js +85 -85
  120. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  121. package/lib/esm/presentation-frontend.d.ts +34 -34
  122. package/lib/esm/presentation-frontend.js +38 -38
  123. package/lib/esm/presentation-frontend.js.map +1 -1
  124. package/package.json +14 -14
@@ -1,286 +1,286 @@
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 { compareStrings, Dictionary, Guid, isIDisposable } from "@itwin/core-bentley";
9
- import { InternetConnectivityStatus } from "@itwin/core-common";
10
- import { IModelApp } from "@itwin/core-frontend";
11
- import { PresentationError, PresentationStatus } from "@itwin/presentation-common";
12
- import { ConnectivityInformationProvider } from "../ConnectivityInformationProvider";
13
- /** @internal */
14
- export const IMODELJS_PRESENTATION_SETTING_NAMESPACE = "imodeljs.presentation";
15
- /** @internal */
16
- export const DEPRECATED_PROPERTIES_SETTING_NAMESPACE = "Properties";
17
- /** @internal */
18
- export const FAVORITE_PROPERTIES_SETTING_NAME = "FavoriteProperties";
19
- /** @internal */
20
- export const FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME = "FavoritePropertiesOrderInfo";
21
- /**
22
- * Available implementations of [[IFavoritePropertiesStorage]].
23
- * @public
24
- */
25
- export var DefaultFavoritePropertiesStorageTypes;
26
- (function (DefaultFavoritePropertiesStorageTypes) {
27
- /** A no-op storage that doesn't store or return anything. Used for cases when favorite properties aren't used by the application. */
28
- DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["Noop"] = 0] = "Noop";
29
- /** A storage that stores favorite properties information in a browser local storage. */
30
- DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["BrowserLocalStorage"] = 1] = "BrowserLocalStorage";
31
- /** A storage that stores favorite properties in a user preferences storage (see [[IModelApp.userPreferences]]). */
32
- DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["UserPreferencesStorage"] = 2] = "UserPreferencesStorage";
33
- })(DefaultFavoritePropertiesStorageTypes || (DefaultFavoritePropertiesStorageTypes = {}));
34
- /**
35
- * A factory method to create one of the available [[IFavoritePropertiesStorage]] implementations.
36
- * @public
37
- */
38
- export function createFavoritePropertiesStorage(type) {
39
- switch (type) {
40
- case DefaultFavoritePropertiesStorageTypes.Noop: return new NoopFavoritePropertiesStorage();
41
- case DefaultFavoritePropertiesStorageTypes.BrowserLocalStorage: return new BrowserLocalFavoritePropertiesStorage();
42
- case DefaultFavoritePropertiesStorageTypes.UserPreferencesStorage: return new OfflineCachingFavoritePropertiesStorage({ impl: new IModelAppFavoritePropertiesStorage() });
43
- }
44
- }
45
- /**
46
- * @internal
47
- */
48
- export class IModelAppFavoritePropertiesStorage {
49
- async ensureIsSignedIn() {
50
- const accessToken = IModelApp.authorizationClient ? await IModelApp.authorizationClient.getAccessToken() : "";
51
- if (accessToken)
52
- return { accessToken };
53
- throw new PresentationError(PresentationStatus.Error, "Current user is not authorized to use the settings service");
54
- }
55
- async loadProperties(iTwinId, imodelId) {
56
- if (!IModelApp.userPreferences)
57
- throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
58
- const { accessToken } = await this.ensureIsSignedIn();
59
- let setting = await IModelApp.userPreferences.get({
60
- accessToken,
61
- iTwinId,
62
- iModelId: imodelId,
63
- namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
64
- key: FAVORITE_PROPERTIES_SETTING_NAME,
65
- });
66
- if (setting !== undefined)
67
- return new Set(setting);
68
- // try to check the old namespace
69
- setting = await IModelApp.userPreferences.get({
70
- accessToken,
71
- iTwinId,
72
- iModelId: imodelId,
73
- namespace: DEPRECATED_PROPERTIES_SETTING_NAMESPACE,
74
- key: FAVORITE_PROPERTIES_SETTING_NAME,
75
- });
76
- if (setting !== undefined && setting.hasOwnProperty("nestedContentInfos") && setting.hasOwnProperty("propertyInfos") && setting.hasOwnProperty("baseFieldInfos"))
77
- return new Set([...setting.nestedContentInfos, ...setting.propertyInfos, ...setting.baseFieldInfos]);
78
- return undefined;
79
- }
80
- async saveProperties(properties, iTwinId, imodelId) {
81
- if (!IModelApp.userPreferences)
82
- throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
83
- const { accessToken } = await this.ensureIsSignedIn();
84
- await IModelApp.userPreferences.save({
85
- accessToken,
86
- iTwinId,
87
- iModelId: imodelId,
88
- namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
89
- key: FAVORITE_PROPERTIES_SETTING_NAME,
90
- content: Array.from(properties),
91
- });
92
- }
93
- async loadPropertiesOrder(iTwinId, imodelId) {
94
- if (!IModelApp.userPreferences)
95
- throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
96
- const { accessToken } = await this.ensureIsSignedIn();
97
- const setting = await IModelApp.userPreferences.get({
98
- accessToken,
99
- iTwinId,
100
- iModelId: imodelId,
101
- namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
102
- key: FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
103
- });
104
- return setting;
105
- }
106
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
107
- if (!IModelApp.userPreferences)
108
- throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
109
- const { accessToken } = await this.ensureIsSignedIn();
110
- await IModelApp.userPreferences.save({
111
- accessToken,
112
- iTwinId,
113
- iModelId: imodelId,
114
- namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
115
- key: FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
116
- content: orderInfos,
117
- });
118
- }
119
- }
120
- /** @internal */
121
- export class OfflineCachingFavoritePropertiesStorage {
122
- constructor(props) {
123
- var _a;
124
- this._propertiesOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
125
- this._propertiesOrderOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
126
- // eslint-disable-next-line @typescript-eslint/naming-convention
127
- this.onConnectivityStatusChanged = (args) => {
128
- // istanbul ignore else
129
- if (args.status === InternetConnectivityStatus.Online) {
130
- // note: we're copying the cached values to temp arrays because `saveProperties` and `savePropertiesOrder` both
131
- // attempt to modify cache dictionaries
132
- const propertiesCache = new Array();
133
- this._propertiesOfflineCache.forEach((key, value) => propertiesCache.push({ properties: value, iTwinId: key[0], imodelId: key[1] }));
134
- propertiesCache.forEach(async (cached) => this.saveProperties(cached.properties, cached.iTwinId, cached.imodelId));
135
- const ordersCache = new Array();
136
- this._propertiesOrderOfflineCache.forEach((key, value) => ordersCache.push({ order: value, iTwinId: key[0], imodelId: key[1] }));
137
- ordersCache.forEach(async (cached) => this.savePropertiesOrder(cached.order, cached.iTwinId, cached.imodelId));
138
- }
139
- };
140
- this._impl = props.impl;
141
- // istanbul ignore next
142
- this._connectivityInfo = (_a = props.connectivityInfo) !== null && _a !== void 0 ? _a : new ConnectivityInformationProvider();
143
- this._connectivityInfo.onInternetConnectivityChanged.addListener(this.onConnectivityStatusChanged);
144
- }
145
- dispose() {
146
- if (isIDisposable(this._connectivityInfo))
147
- this._connectivityInfo.dispose();
148
- }
149
- get impl() { return this._impl; }
150
- async loadProperties(iTwinId, imodelId) {
151
- if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
152
- try {
153
- return await this._impl.loadProperties(iTwinId, imodelId);
154
- }
155
- catch {
156
- // return from offline cache if the above fails
157
- }
158
- }
159
- return this._propertiesOfflineCache.get([iTwinId, imodelId]);
160
- }
161
- async saveProperties(properties, iTwinId, imodelId) {
162
- const key = [iTwinId, imodelId];
163
- if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
164
- this._propertiesOfflineCache.set(key, properties);
165
- return;
166
- }
167
- const reservationId = this._propertiesOfflineCache.reserve(key);
168
- try {
169
- await this._impl.saveProperties(properties, iTwinId, imodelId);
170
- this._propertiesOfflineCache.reservedDelete(key, reservationId);
171
- }
172
- catch {
173
- this._propertiesOfflineCache.reservedSet(key, properties, reservationId);
174
- }
175
- }
176
- async loadPropertiesOrder(iTwinId, imodelId) {
177
- if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
178
- try {
179
- return await this._impl.loadPropertiesOrder(iTwinId, imodelId);
180
- }
181
- catch {
182
- // return from offline cache if the above fails
183
- }
184
- }
185
- return this._propertiesOrderOfflineCache.get([iTwinId, imodelId]);
186
- }
187
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
188
- const key = [iTwinId, imodelId];
189
- if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
190
- this._propertiesOrderOfflineCache.set(key, orderInfos);
191
- return;
192
- }
193
- const reservationId = this._propertiesOrderOfflineCache.reserve(key);
194
- try {
195
- await this._impl.savePropertiesOrder(orderInfos, iTwinId, imodelId);
196
- this._propertiesOrderOfflineCache.reservedDelete(key, reservationId);
197
- }
198
- catch {
199
- this._propertiesOrderOfflineCache.reservedSet(key, orderInfos, reservationId);
200
- }
201
- }
202
- }
203
- class DictionaryWithReservations {
204
- constructor(compareKeys) {
205
- this._impl = new Dictionary(compareKeys);
206
- }
207
- get(key) { var _a; return (_a = this._impl.get(key)) === null || _a === void 0 ? void 0 : _a.value; }
208
- forEach(func) {
209
- this._impl.forEach((key, entry) => {
210
- // istanbul ignore else
211
- if (entry.value)
212
- func(key, entry.value);
213
- });
214
- }
215
- reserve(key) {
216
- const reservationId = Guid.createValue();
217
- this._impl.set(key, { lastReservationId: reservationId });
218
- return reservationId;
219
- }
220
- set(key, value) { return this._impl.set(key, { value }); }
221
- reservedSet(key, value, reservationId) {
222
- const entry = this._impl.get(key);
223
- if (entry && entry.lastReservationId === reservationId)
224
- this._impl.set(key, { value });
225
- }
226
- reservedDelete(key, reservationId) {
227
- const entry = this._impl.get(key);
228
- if (entry && entry.lastReservationId === reservationId)
229
- this._impl.delete(key);
230
- }
231
- }
232
- // istanbul ignore next
233
- function iTwinAndIModelIdsKeyComparer(lhs, rhs) {
234
- var _a, _b, _c, _d;
235
- const iTwinIdCompare = compareStrings((_a = lhs[0]) !== null && _a !== void 0 ? _a : "", (_b = rhs[0]) !== null && _b !== void 0 ? _b : "");
236
- return (iTwinIdCompare !== 0) ? iTwinIdCompare : compareStrings((_c = lhs[1]) !== null && _c !== void 0 ? _c : "", (_d = rhs[1]) !== null && _d !== void 0 ? _d : "");
237
- }
238
- /** @internal */
239
- export class NoopFavoritePropertiesStorage {
240
- // istanbul ignore next
241
- async loadProperties(_iTwinId, _imodelId) { return undefined; }
242
- // istanbul ignore next
243
- async saveProperties(_properties, _iTwinId, _imodelId) { }
244
- // istanbul ignore next
245
- async loadPropertiesOrder(_iTwinId, _imodelId) { return undefined; }
246
- // istanbul ignore next
247
- async savePropertiesOrder(_orderInfos, _iTwinId, _imodelId) { }
248
- }
249
- /** @internal */
250
- export class BrowserLocalFavoritePropertiesStorage {
251
- constructor(props) {
252
- var _a;
253
- // istanbul ignore next
254
- this._localStorage = (_a = props === null || props === void 0 ? void 0 : props.localStorage) !== null && _a !== void 0 ? _a : window.localStorage;
255
- }
256
- createFavoritesSettingItemKey(iTwinId, imodelId) {
257
- return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
258
- }
259
- createOrderSettingItemKey(iTwinId, imodelId) {
260
- return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
261
- }
262
- async loadProperties(iTwinId, imodelId) {
263
- const value = this._localStorage.getItem(this.createFavoritesSettingItemKey(iTwinId, imodelId));
264
- if (!value)
265
- return undefined;
266
- const properties = JSON.parse(value);
267
- return new Set(properties);
268
- }
269
- async saveProperties(properties, iTwinId, imodelId) {
270
- this._localStorage.setItem(this.createFavoritesSettingItemKey(iTwinId, imodelId), JSON.stringify([...properties]));
271
- }
272
- async loadPropertiesOrder(iTwinId, imodelId) {
273
- const value = this._localStorage.getItem(this.createOrderSettingItemKey(iTwinId, imodelId));
274
- if (!value)
275
- return undefined;
276
- const orderInfos = JSON.parse(value).map((json) => ({
277
- ...json,
278
- orderedTimestamp: new Date(json.orderedTimestamp),
279
- }));
280
- return orderInfos;
281
- }
282
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
283
- this._localStorage.setItem(this.createOrderSettingItemKey(iTwinId, imodelId), JSON.stringify(orderInfos));
284
- }
285
- }
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 { compareStrings, Dictionary, Guid, isIDisposable } from "@itwin/core-bentley";
9
+ import { InternetConnectivityStatus } from "@itwin/core-common";
10
+ import { IModelApp } from "@itwin/core-frontend";
11
+ import { PresentationError, PresentationStatus } from "@itwin/presentation-common";
12
+ import { ConnectivityInformationProvider } from "../ConnectivityInformationProvider";
13
+ /** @internal */
14
+ export const IMODELJS_PRESENTATION_SETTING_NAMESPACE = "imodeljs.presentation";
15
+ /** @internal */
16
+ export const DEPRECATED_PROPERTIES_SETTING_NAMESPACE = "Properties";
17
+ /** @internal */
18
+ export const FAVORITE_PROPERTIES_SETTING_NAME = "FavoriteProperties";
19
+ /** @internal */
20
+ export const FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME = "FavoritePropertiesOrderInfo";
21
+ /**
22
+ * Available implementations of [[IFavoritePropertiesStorage]].
23
+ * @public
24
+ */
25
+ export var DefaultFavoritePropertiesStorageTypes;
26
+ (function (DefaultFavoritePropertiesStorageTypes) {
27
+ /** A no-op storage that doesn't store or return anything. Used for cases when favorite properties aren't used by the application. */
28
+ DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["Noop"] = 0] = "Noop";
29
+ /** A storage that stores favorite properties information in a browser local storage. */
30
+ DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["BrowserLocalStorage"] = 1] = "BrowserLocalStorage";
31
+ /** A storage that stores favorite properties in a user preferences storage (see [[IModelApp.userPreferences]]). */
32
+ DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["UserPreferencesStorage"] = 2] = "UserPreferencesStorage";
33
+ })(DefaultFavoritePropertiesStorageTypes || (DefaultFavoritePropertiesStorageTypes = {}));
34
+ /**
35
+ * A factory method to create one of the available [[IFavoritePropertiesStorage]] implementations.
36
+ * @public
37
+ */
38
+ export function createFavoritePropertiesStorage(type) {
39
+ switch (type) {
40
+ case DefaultFavoritePropertiesStorageTypes.Noop: return new NoopFavoritePropertiesStorage();
41
+ case DefaultFavoritePropertiesStorageTypes.BrowserLocalStorage: return new BrowserLocalFavoritePropertiesStorage();
42
+ case DefaultFavoritePropertiesStorageTypes.UserPreferencesStorage: return new OfflineCachingFavoritePropertiesStorage({ impl: new IModelAppFavoritePropertiesStorage() });
43
+ }
44
+ }
45
+ /**
46
+ * @internal
47
+ */
48
+ export class IModelAppFavoritePropertiesStorage {
49
+ async ensureIsSignedIn() {
50
+ const accessToken = IModelApp.authorizationClient ? await IModelApp.authorizationClient.getAccessToken() : "";
51
+ if (accessToken)
52
+ return { accessToken };
53
+ throw new PresentationError(PresentationStatus.Error, "Current user is not authorized to use the settings service");
54
+ }
55
+ async loadProperties(iTwinId, imodelId) {
56
+ if (!IModelApp.userPreferences)
57
+ throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
58
+ const { accessToken } = await this.ensureIsSignedIn();
59
+ let setting = await IModelApp.userPreferences.get({
60
+ accessToken,
61
+ iTwinId,
62
+ iModelId: imodelId,
63
+ namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
64
+ key: FAVORITE_PROPERTIES_SETTING_NAME,
65
+ });
66
+ if (setting !== undefined)
67
+ return new Set(setting);
68
+ // try to check the old namespace
69
+ setting = await IModelApp.userPreferences.get({
70
+ accessToken,
71
+ iTwinId,
72
+ iModelId: imodelId,
73
+ namespace: DEPRECATED_PROPERTIES_SETTING_NAMESPACE,
74
+ key: FAVORITE_PROPERTIES_SETTING_NAME,
75
+ });
76
+ if (setting !== undefined && setting.hasOwnProperty("nestedContentInfos") && setting.hasOwnProperty("propertyInfos") && setting.hasOwnProperty("baseFieldInfos"))
77
+ return new Set([...setting.nestedContentInfos, ...setting.propertyInfos, ...setting.baseFieldInfos]);
78
+ return undefined;
79
+ }
80
+ async saveProperties(properties, iTwinId, imodelId) {
81
+ if (!IModelApp.userPreferences)
82
+ throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
83
+ const { accessToken } = await this.ensureIsSignedIn();
84
+ await IModelApp.userPreferences.save({
85
+ accessToken,
86
+ iTwinId,
87
+ iModelId: imodelId,
88
+ namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
89
+ key: FAVORITE_PROPERTIES_SETTING_NAME,
90
+ content: Array.from(properties),
91
+ });
92
+ }
93
+ async loadPropertiesOrder(iTwinId, imodelId) {
94
+ if (!IModelApp.userPreferences)
95
+ throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
96
+ const { accessToken } = await this.ensureIsSignedIn();
97
+ const setting = await IModelApp.userPreferences.get({
98
+ accessToken,
99
+ iTwinId,
100
+ iModelId: imodelId,
101
+ namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
102
+ key: FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
103
+ });
104
+ return setting;
105
+ }
106
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
107
+ if (!IModelApp.userPreferences)
108
+ throw new PresentationError(PresentationStatus.Error, "User preferences service is not set up");
109
+ const { accessToken } = await this.ensureIsSignedIn();
110
+ await IModelApp.userPreferences.save({
111
+ accessToken,
112
+ iTwinId,
113
+ iModelId: imodelId,
114
+ namespace: IMODELJS_PRESENTATION_SETTING_NAMESPACE,
115
+ key: FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
116
+ content: orderInfos,
117
+ });
118
+ }
119
+ }
120
+ /** @internal */
121
+ export class OfflineCachingFavoritePropertiesStorage {
122
+ constructor(props) {
123
+ var _a;
124
+ this._propertiesOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
125
+ this._propertiesOrderOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
126
+ // eslint-disable-next-line @typescript-eslint/naming-convention
127
+ this.onConnectivityStatusChanged = (args) => {
128
+ // istanbul ignore else
129
+ if (args.status === InternetConnectivityStatus.Online) {
130
+ // note: we're copying the cached values to temp arrays because `saveProperties` and `savePropertiesOrder` both
131
+ // attempt to modify cache dictionaries
132
+ const propertiesCache = new Array();
133
+ this._propertiesOfflineCache.forEach((key, value) => propertiesCache.push({ properties: value, iTwinId: key[0], imodelId: key[1] }));
134
+ propertiesCache.forEach(async (cached) => this.saveProperties(cached.properties, cached.iTwinId, cached.imodelId));
135
+ const ordersCache = new Array();
136
+ this._propertiesOrderOfflineCache.forEach((key, value) => ordersCache.push({ order: value, iTwinId: key[0], imodelId: key[1] }));
137
+ ordersCache.forEach(async (cached) => this.savePropertiesOrder(cached.order, cached.iTwinId, cached.imodelId));
138
+ }
139
+ };
140
+ this._impl = props.impl;
141
+ // istanbul ignore next
142
+ this._connectivityInfo = (_a = props.connectivityInfo) !== null && _a !== void 0 ? _a : new ConnectivityInformationProvider();
143
+ this._connectivityInfo.onInternetConnectivityChanged.addListener(this.onConnectivityStatusChanged);
144
+ }
145
+ dispose() {
146
+ if (isIDisposable(this._connectivityInfo))
147
+ this._connectivityInfo.dispose();
148
+ }
149
+ get impl() { return this._impl; }
150
+ async loadProperties(iTwinId, imodelId) {
151
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
152
+ try {
153
+ return await this._impl.loadProperties(iTwinId, imodelId);
154
+ }
155
+ catch {
156
+ // return from offline cache if the above fails
157
+ }
158
+ }
159
+ return this._propertiesOfflineCache.get([iTwinId, imodelId]);
160
+ }
161
+ async saveProperties(properties, iTwinId, imodelId) {
162
+ const key = [iTwinId, imodelId];
163
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
164
+ this._propertiesOfflineCache.set(key, properties);
165
+ return;
166
+ }
167
+ const reservationId = this._propertiesOfflineCache.reserve(key);
168
+ try {
169
+ await this._impl.saveProperties(properties, iTwinId, imodelId);
170
+ this._propertiesOfflineCache.reservedDelete(key, reservationId);
171
+ }
172
+ catch {
173
+ this._propertiesOfflineCache.reservedSet(key, properties, reservationId);
174
+ }
175
+ }
176
+ async loadPropertiesOrder(iTwinId, imodelId) {
177
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
178
+ try {
179
+ return await this._impl.loadPropertiesOrder(iTwinId, imodelId);
180
+ }
181
+ catch {
182
+ // return from offline cache if the above fails
183
+ }
184
+ }
185
+ return this._propertiesOrderOfflineCache.get([iTwinId, imodelId]);
186
+ }
187
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
188
+ const key = [iTwinId, imodelId];
189
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
190
+ this._propertiesOrderOfflineCache.set(key, orderInfos);
191
+ return;
192
+ }
193
+ const reservationId = this._propertiesOrderOfflineCache.reserve(key);
194
+ try {
195
+ await this._impl.savePropertiesOrder(orderInfos, iTwinId, imodelId);
196
+ this._propertiesOrderOfflineCache.reservedDelete(key, reservationId);
197
+ }
198
+ catch {
199
+ this._propertiesOrderOfflineCache.reservedSet(key, orderInfos, reservationId);
200
+ }
201
+ }
202
+ }
203
+ class DictionaryWithReservations {
204
+ constructor(compareKeys) {
205
+ this._impl = new Dictionary(compareKeys);
206
+ }
207
+ get(key) { var _a; return (_a = this._impl.get(key)) === null || _a === void 0 ? void 0 : _a.value; }
208
+ forEach(func) {
209
+ this._impl.forEach((key, entry) => {
210
+ // istanbul ignore else
211
+ if (entry.value)
212
+ func(key, entry.value);
213
+ });
214
+ }
215
+ reserve(key) {
216
+ const reservationId = Guid.createValue();
217
+ this._impl.set(key, { lastReservationId: reservationId });
218
+ return reservationId;
219
+ }
220
+ set(key, value) { return this._impl.set(key, { value }); }
221
+ reservedSet(key, value, reservationId) {
222
+ const entry = this._impl.get(key);
223
+ if (entry && entry.lastReservationId === reservationId)
224
+ this._impl.set(key, { value });
225
+ }
226
+ reservedDelete(key, reservationId) {
227
+ const entry = this._impl.get(key);
228
+ if (entry && entry.lastReservationId === reservationId)
229
+ this._impl.delete(key);
230
+ }
231
+ }
232
+ // istanbul ignore next
233
+ function iTwinAndIModelIdsKeyComparer(lhs, rhs) {
234
+ var _a, _b, _c, _d;
235
+ const iTwinIdCompare = compareStrings((_a = lhs[0]) !== null && _a !== void 0 ? _a : "", (_b = rhs[0]) !== null && _b !== void 0 ? _b : "");
236
+ return (iTwinIdCompare !== 0) ? iTwinIdCompare : compareStrings((_c = lhs[1]) !== null && _c !== void 0 ? _c : "", (_d = rhs[1]) !== null && _d !== void 0 ? _d : "");
237
+ }
238
+ /** @internal */
239
+ export class NoopFavoritePropertiesStorage {
240
+ // istanbul ignore next
241
+ async loadProperties(_iTwinId, _imodelId) { return undefined; }
242
+ // istanbul ignore next
243
+ async saveProperties(_properties, _iTwinId, _imodelId) { }
244
+ // istanbul ignore next
245
+ async loadPropertiesOrder(_iTwinId, _imodelId) { return undefined; }
246
+ // istanbul ignore next
247
+ async savePropertiesOrder(_orderInfos, _iTwinId, _imodelId) { }
248
+ }
249
+ /** @internal */
250
+ export class BrowserLocalFavoritePropertiesStorage {
251
+ constructor(props) {
252
+ var _a;
253
+ // istanbul ignore next
254
+ this._localStorage = (_a = props === null || props === void 0 ? void 0 : props.localStorage) !== null && _a !== void 0 ? _a : window.localStorage;
255
+ }
256
+ createFavoritesSettingItemKey(iTwinId, imodelId) {
257
+ return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
258
+ }
259
+ createOrderSettingItemKey(iTwinId, imodelId) {
260
+ return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
261
+ }
262
+ async loadProperties(iTwinId, imodelId) {
263
+ const value = this._localStorage.getItem(this.createFavoritesSettingItemKey(iTwinId, imodelId));
264
+ if (!value)
265
+ return undefined;
266
+ const properties = JSON.parse(value);
267
+ return new Set(properties);
268
+ }
269
+ async saveProperties(properties, iTwinId, imodelId) {
270
+ this._localStorage.setItem(this.createFavoritesSettingItemKey(iTwinId, imodelId), JSON.stringify([...properties]));
271
+ }
272
+ async loadPropertiesOrder(iTwinId, imodelId) {
273
+ const value = this._localStorage.getItem(this.createOrderSettingItemKey(iTwinId, imodelId));
274
+ if (!value)
275
+ return undefined;
276
+ const orderInfos = JSON.parse(value).map((json) => ({
277
+ ...json,
278
+ orderedTimestamp: new Date(json.orderedTimestamp),
279
+ }));
280
+ return orderInfos;
281
+ }
282
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
283
+ this._localStorage.setItem(this.createOrderSettingItemKey(iTwinId, imodelId), JSON.stringify(orderInfos));
284
+ }
285
+ }
286
286
  //# sourceMappingURL=FavoritePropertiesStorage.js.map