@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,283 +1,283 @@
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
- this._propertiesOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
124
- this._propertiesOrderOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
125
- // eslint-disable-next-line @typescript-eslint/naming-convention
126
- this.onConnectivityStatusChanged = (args) => {
127
- // istanbul ignore else
128
- if (args.status === InternetConnectivityStatus.Online) {
129
- // note: we're copying the cached values to temp arrays because `saveProperties` and `savePropertiesOrder` both
130
- // attempt to modify cache dictionaries
131
- const propertiesCache = new Array();
132
- this._propertiesOfflineCache.forEach((key, value) => propertiesCache.push({ properties: value, iTwinId: key[0], imodelId: key[1] }));
133
- propertiesCache.forEach(async (cached) => this.saveProperties(cached.properties, cached.iTwinId, cached.imodelId));
134
- const ordersCache = new Array();
135
- this._propertiesOrderOfflineCache.forEach((key, value) => ordersCache.push({ order: value, iTwinId: key[0], imodelId: key[1] }));
136
- ordersCache.forEach(async (cached) => this.savePropertiesOrder(cached.order, cached.iTwinId, cached.imodelId));
137
- }
138
- };
139
- this._impl = props.impl;
140
- // istanbul ignore next
141
- this._connectivityInfo = props.connectivityInfo ?? new ConnectivityInformationProvider();
142
- this._connectivityInfo.onInternetConnectivityChanged.addListener(this.onConnectivityStatusChanged);
143
- }
144
- dispose() {
145
- if (isIDisposable(this._connectivityInfo))
146
- this._connectivityInfo.dispose();
147
- }
148
- get impl() { return this._impl; }
149
- async loadProperties(iTwinId, imodelId) {
150
- if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
151
- try {
152
- return await this._impl.loadProperties(iTwinId, imodelId);
153
- }
154
- catch {
155
- // return from offline cache if the above fails
156
- }
157
- }
158
- return this._propertiesOfflineCache.get([iTwinId, imodelId]);
159
- }
160
- async saveProperties(properties, iTwinId, imodelId) {
161
- const key = [iTwinId, imodelId];
162
- if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
163
- this._propertiesOfflineCache.set(key, properties);
164
- return;
165
- }
166
- const reservationId = this._propertiesOfflineCache.reserve(key);
167
- try {
168
- await this._impl.saveProperties(properties, iTwinId, imodelId);
169
- this._propertiesOfflineCache.reservedDelete(key, reservationId);
170
- }
171
- catch {
172
- this._propertiesOfflineCache.reservedSet(key, properties, reservationId);
173
- }
174
- }
175
- async loadPropertiesOrder(iTwinId, imodelId) {
176
- if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
177
- try {
178
- return await this._impl.loadPropertiesOrder(iTwinId, imodelId);
179
- }
180
- catch {
181
- // return from offline cache if the above fails
182
- }
183
- }
184
- return this._propertiesOrderOfflineCache.get([iTwinId, imodelId]);
185
- }
186
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
187
- const key = [iTwinId, imodelId];
188
- if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
189
- this._propertiesOrderOfflineCache.set(key, orderInfos);
190
- return;
191
- }
192
- const reservationId = this._propertiesOrderOfflineCache.reserve(key);
193
- try {
194
- await this._impl.savePropertiesOrder(orderInfos, iTwinId, imodelId);
195
- this._propertiesOrderOfflineCache.reservedDelete(key, reservationId);
196
- }
197
- catch {
198
- this._propertiesOrderOfflineCache.reservedSet(key, orderInfos, reservationId);
199
- }
200
- }
201
- }
202
- class DictionaryWithReservations {
203
- constructor(compareKeys) {
204
- this._impl = new Dictionary(compareKeys);
205
- }
206
- get(key) { return this._impl.get(key)?.value; }
207
- forEach(func) {
208
- this._impl.forEach((key, entry) => {
209
- // istanbul ignore else
210
- if (entry.value)
211
- func(key, entry.value);
212
- });
213
- }
214
- reserve(key) {
215
- const reservationId = Guid.createValue();
216
- this._impl.set(key, { lastReservationId: reservationId });
217
- return reservationId;
218
- }
219
- set(key, value) { return this._impl.set(key, { value }); }
220
- reservedSet(key, value, reservationId) {
221
- const entry = this._impl.get(key);
222
- if (entry && entry.lastReservationId === reservationId)
223
- this._impl.set(key, { value });
224
- }
225
- reservedDelete(key, reservationId) {
226
- const entry = this._impl.get(key);
227
- if (entry && entry.lastReservationId === reservationId)
228
- this._impl.delete(key);
229
- }
230
- }
231
- // istanbul ignore next
232
- function iTwinAndIModelIdsKeyComparer(lhs, rhs) {
233
- const iTwinIdCompare = compareStrings(lhs[0] ?? "", rhs[0] ?? "");
234
- return (iTwinIdCompare !== 0) ? iTwinIdCompare : compareStrings(lhs[1] ?? "", rhs[1] ?? "");
235
- }
236
- /** @internal */
237
- export class NoopFavoritePropertiesStorage {
238
- // istanbul ignore next
239
- async loadProperties(_iTwinId, _imodelId) { return undefined; }
240
- // istanbul ignore next
241
- async saveProperties(_properties, _iTwinId, _imodelId) { }
242
- // istanbul ignore next
243
- async loadPropertiesOrder(_iTwinId, _imodelId) { return undefined; }
244
- // istanbul ignore next
245
- async savePropertiesOrder(_orderInfos, _iTwinId, _imodelId) { }
246
- }
247
- /** @internal */
248
- export class BrowserLocalFavoritePropertiesStorage {
249
- constructor(props) {
250
- // istanbul ignore next
251
- this._localStorage = props?.localStorage ?? window.localStorage;
252
- }
253
- createFavoritesSettingItemKey(iTwinId, imodelId) {
254
- return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
255
- }
256
- createOrderSettingItemKey(iTwinId, imodelId) {
257
- return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
258
- }
259
- async loadProperties(iTwinId, imodelId) {
260
- const value = this._localStorage.getItem(this.createFavoritesSettingItemKey(iTwinId, imodelId));
261
- if (!value)
262
- return undefined;
263
- const properties = JSON.parse(value);
264
- return new Set(properties);
265
- }
266
- async saveProperties(properties, iTwinId, imodelId) {
267
- this._localStorage.setItem(this.createFavoritesSettingItemKey(iTwinId, imodelId), JSON.stringify([...properties]));
268
- }
269
- async loadPropertiesOrder(iTwinId, imodelId) {
270
- const value = this._localStorage.getItem(this.createOrderSettingItemKey(iTwinId, imodelId));
271
- if (!value)
272
- return undefined;
273
- const orderInfos = JSON.parse(value).map((json) => ({
274
- ...json,
275
- orderedTimestamp: new Date(json.orderedTimestamp),
276
- }));
277
- return orderInfos;
278
- }
279
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
280
- this._localStorage.setItem(this.createOrderSettingItemKey(iTwinId, imodelId), JSON.stringify(orderInfos));
281
- }
282
- }
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
+ this._propertiesOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
124
+ this._propertiesOrderOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
125
+ // eslint-disable-next-line @typescript-eslint/naming-convention
126
+ this.onConnectivityStatusChanged = (args) => {
127
+ // istanbul ignore else
128
+ if (args.status === InternetConnectivityStatus.Online) {
129
+ // note: we're copying the cached values to temp arrays because `saveProperties` and `savePropertiesOrder` both
130
+ // attempt to modify cache dictionaries
131
+ const propertiesCache = new Array();
132
+ this._propertiesOfflineCache.forEach((key, value) => propertiesCache.push({ properties: value, iTwinId: key[0], imodelId: key[1] }));
133
+ propertiesCache.forEach(async (cached) => this.saveProperties(cached.properties, cached.iTwinId, cached.imodelId));
134
+ const ordersCache = new Array();
135
+ this._propertiesOrderOfflineCache.forEach((key, value) => ordersCache.push({ order: value, iTwinId: key[0], imodelId: key[1] }));
136
+ ordersCache.forEach(async (cached) => this.savePropertiesOrder(cached.order, cached.iTwinId, cached.imodelId));
137
+ }
138
+ };
139
+ this._impl = props.impl;
140
+ // istanbul ignore next
141
+ this._connectivityInfo = props.connectivityInfo ?? new ConnectivityInformationProvider();
142
+ this._connectivityInfo.onInternetConnectivityChanged.addListener(this.onConnectivityStatusChanged);
143
+ }
144
+ dispose() {
145
+ if (isIDisposable(this._connectivityInfo))
146
+ this._connectivityInfo.dispose();
147
+ }
148
+ get impl() { return this._impl; }
149
+ async loadProperties(iTwinId, imodelId) {
150
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
151
+ try {
152
+ return await this._impl.loadProperties(iTwinId, imodelId);
153
+ }
154
+ catch {
155
+ // return from offline cache if the above fails
156
+ }
157
+ }
158
+ return this._propertiesOfflineCache.get([iTwinId, imodelId]);
159
+ }
160
+ async saveProperties(properties, iTwinId, imodelId) {
161
+ const key = [iTwinId, imodelId];
162
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
163
+ this._propertiesOfflineCache.set(key, properties);
164
+ return;
165
+ }
166
+ const reservationId = this._propertiesOfflineCache.reserve(key);
167
+ try {
168
+ await this._impl.saveProperties(properties, iTwinId, imodelId);
169
+ this._propertiesOfflineCache.reservedDelete(key, reservationId);
170
+ }
171
+ catch {
172
+ this._propertiesOfflineCache.reservedSet(key, properties, reservationId);
173
+ }
174
+ }
175
+ async loadPropertiesOrder(iTwinId, imodelId) {
176
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Online) {
177
+ try {
178
+ return await this._impl.loadPropertiesOrder(iTwinId, imodelId);
179
+ }
180
+ catch {
181
+ // return from offline cache if the above fails
182
+ }
183
+ }
184
+ return this._propertiesOrderOfflineCache.get([iTwinId, imodelId]);
185
+ }
186
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
187
+ const key = [iTwinId, imodelId];
188
+ if (this._connectivityInfo.status === InternetConnectivityStatus.Offline) {
189
+ this._propertiesOrderOfflineCache.set(key, orderInfos);
190
+ return;
191
+ }
192
+ const reservationId = this._propertiesOrderOfflineCache.reserve(key);
193
+ try {
194
+ await this._impl.savePropertiesOrder(orderInfos, iTwinId, imodelId);
195
+ this._propertiesOrderOfflineCache.reservedDelete(key, reservationId);
196
+ }
197
+ catch {
198
+ this._propertiesOrderOfflineCache.reservedSet(key, orderInfos, reservationId);
199
+ }
200
+ }
201
+ }
202
+ class DictionaryWithReservations {
203
+ constructor(compareKeys) {
204
+ this._impl = new Dictionary(compareKeys);
205
+ }
206
+ get(key) { return this._impl.get(key)?.value; }
207
+ forEach(func) {
208
+ this._impl.forEach((key, entry) => {
209
+ // istanbul ignore else
210
+ if (entry.value)
211
+ func(key, entry.value);
212
+ });
213
+ }
214
+ reserve(key) {
215
+ const reservationId = Guid.createValue();
216
+ this._impl.set(key, { lastReservationId: reservationId });
217
+ return reservationId;
218
+ }
219
+ set(key, value) { return this._impl.set(key, { value }); }
220
+ reservedSet(key, value, reservationId) {
221
+ const entry = this._impl.get(key);
222
+ if (entry && entry.lastReservationId === reservationId)
223
+ this._impl.set(key, { value });
224
+ }
225
+ reservedDelete(key, reservationId) {
226
+ const entry = this._impl.get(key);
227
+ if (entry && entry.lastReservationId === reservationId)
228
+ this._impl.delete(key);
229
+ }
230
+ }
231
+ // istanbul ignore next
232
+ function iTwinAndIModelIdsKeyComparer(lhs, rhs) {
233
+ const iTwinIdCompare = compareStrings(lhs[0] ?? "", rhs[0] ?? "");
234
+ return (iTwinIdCompare !== 0) ? iTwinIdCompare : compareStrings(lhs[1] ?? "", rhs[1] ?? "");
235
+ }
236
+ /** @internal */
237
+ export class NoopFavoritePropertiesStorage {
238
+ // istanbul ignore next
239
+ async loadProperties(_iTwinId, _imodelId) { return undefined; }
240
+ // istanbul ignore next
241
+ async saveProperties(_properties, _iTwinId, _imodelId) { }
242
+ // istanbul ignore next
243
+ async loadPropertiesOrder(_iTwinId, _imodelId) { return undefined; }
244
+ // istanbul ignore next
245
+ async savePropertiesOrder(_orderInfos, _iTwinId, _imodelId) { }
246
+ }
247
+ /** @internal */
248
+ export class BrowserLocalFavoritePropertiesStorage {
249
+ constructor(props) {
250
+ // istanbul ignore next
251
+ this._localStorage = props?.localStorage ?? window.localStorage;
252
+ }
253
+ createFavoritesSettingItemKey(iTwinId, imodelId) {
254
+ return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
255
+ }
256
+ createOrderSettingItemKey(iTwinId, imodelId) {
257
+ return `${IMODELJS_PRESENTATION_SETTING_NAMESPACE}${FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
258
+ }
259
+ async loadProperties(iTwinId, imodelId) {
260
+ const value = this._localStorage.getItem(this.createFavoritesSettingItemKey(iTwinId, imodelId));
261
+ if (!value)
262
+ return undefined;
263
+ const properties = JSON.parse(value);
264
+ return new Set(properties);
265
+ }
266
+ async saveProperties(properties, iTwinId, imodelId) {
267
+ this._localStorage.setItem(this.createFavoritesSettingItemKey(iTwinId, imodelId), JSON.stringify([...properties]));
268
+ }
269
+ async loadPropertiesOrder(iTwinId, imodelId) {
270
+ const value = this._localStorage.getItem(this.createOrderSettingItemKey(iTwinId, imodelId));
271
+ if (!value)
272
+ return undefined;
273
+ const orderInfos = JSON.parse(value).map((json) => ({
274
+ ...json,
275
+ orderedTimestamp: new Date(json.orderedTimestamp),
276
+ }));
277
+ return orderInfos;
278
+ }
279
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
280
+ this._localStorage.setItem(this.createOrderSettingItemKey(iTwinId, imodelId), JSON.stringify(orderInfos));
281
+ }
282
+ }
283
283
  //# sourceMappingURL=FavoritePropertiesStorage.js.map