@itwin/presentation-frontend 4.0.0-dev.1 → 4.0.0-dev.100

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 (122) hide show
  1. package/CHANGELOG.md +55 -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/FrontendLoggerCategory.d.ts +12 -12
  8. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js +20 -20
  9. package/lib/cjs/presentation-frontend/IpcRequestsHandler.d.ts +9 -10
  10. package/lib/cjs/presentation-frontend/IpcRequestsHandler.d.ts.map +1 -1
  11. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js +32 -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 -75
  17. package/lib/cjs/presentation-frontend/Presentation.d.ts.map +1 -1
  18. package/lib/cjs/presentation-frontend/Presentation.js +159 -162
  19. package/lib/cjs/presentation-frontend/Presentation.js.map +1 -1
  20. package/lib/cjs/presentation-frontend/PresentationManager.d.ts +207 -186
  21. package/lib/cjs/presentation-frontend/PresentationManager.d.ts.map +1 -1
  22. package/lib/cjs/presentation-frontend/PresentationManager.js +442 -436
  23. package/lib/cjs/presentation-frontend/PresentationManager.js.map +1 -1
  24. package/lib/cjs/presentation-frontend/RulesetManager.d.ts +63 -63
  25. package/lib/cjs/presentation-frontend/RulesetManager.js +85 -85
  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/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  30. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts.map +1 -1
  31. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +520 -518
  32. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  33. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  34. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +290 -293
  35. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  36. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  37. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.js +97 -97
  38. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  39. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.js +9 -9
  40. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  41. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.js +33 -33
  42. package/lib/cjs/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  43. package/lib/cjs/presentation-frontend/selection/SelectionHandler.js +108 -108
  44. package/lib/cjs/presentation-frontend/selection/SelectionHelper.d.ts +17 -14
  45. package/lib/cjs/presentation-frontend/selection/SelectionHelper.d.ts.map +1 -1
  46. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js +40 -37
  47. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  48. package/lib/cjs/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  49. package/lib/cjs/presentation-frontend/selection/SelectionManager.js +414 -414
  50. package/lib/cjs/presentation-frontend/selection/SelectionManager.js.map +1 -1
  51. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  52. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js +91 -91
  53. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  54. package/lib/cjs/presentation-frontend.d.ts +33 -34
  55. package/lib/cjs/presentation-frontend.d.ts.map +1 -1
  56. package/lib/cjs/presentation-frontend.js +53 -50
  57. package/lib/cjs/presentation-frontend.js.map +1 -1
  58. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  59. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js +43 -43
  60. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js.map +1 -1
  61. package/lib/esm/presentation-frontend/Diagnostics.d.ts +14 -14
  62. package/lib/esm/presentation-frontend/Diagnostics.js +56 -56
  63. package/lib/esm/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  64. package/lib/esm/presentation-frontend/FrontendLoggerCategory.js +17 -17
  65. package/lib/esm/presentation-frontend/IpcRequestsHandler.d.ts +9 -10
  66. package/lib/esm/presentation-frontend/IpcRequestsHandler.d.ts.map +1 -1
  67. package/lib/esm/presentation-frontend/IpcRequestsHandler.js +28 -37
  68. package/lib/esm/presentation-frontend/IpcRequestsHandler.js.map +1 -1
  69. package/lib/esm/presentation-frontend/LocalizationHelper.d.ts +10 -10
  70. package/lib/esm/presentation-frontend/LocalizationHelper.js +27 -27
  71. package/lib/esm/presentation-frontend/LocalizationHelper.js.map +1 -1
  72. package/lib/esm/presentation-frontend/Presentation.d.ts +73 -75
  73. package/lib/esm/presentation-frontend/Presentation.d.ts.map +1 -1
  74. package/lib/esm/presentation-frontend/Presentation.js +155 -158
  75. package/lib/esm/presentation-frontend/Presentation.js.map +1 -1
  76. package/lib/esm/presentation-frontend/PresentationManager.d.ts +207 -186
  77. package/lib/esm/presentation-frontend/PresentationManager.d.ts.map +1 -1
  78. package/lib/esm/presentation-frontend/PresentationManager.js +437 -431
  79. package/lib/esm/presentation-frontend/PresentationManager.js.map +1 -1
  80. package/lib/esm/presentation-frontend/RulesetManager.d.ts +63 -63
  81. package/lib/esm/presentation-frontend/RulesetManager.js +81 -81
  82. package/lib/esm/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  83. package/lib/esm/presentation-frontend/RulesetVariablesManager.js +191 -191
  84. package/lib/esm/presentation-frontend/RulesetVariablesManager.js.map +1 -1
  85. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  86. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts.map +1 -1
  87. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +515 -512
  88. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  89. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  90. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +282 -285
  91. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  92. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  93. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js +93 -93
  94. package/lib/esm/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  95. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js +8 -8
  96. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  97. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js +29 -29
  98. package/lib/esm/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  99. package/lib/esm/presentation-frontend/selection/SelectionHandler.js +104 -104
  100. package/lib/esm/presentation-frontend/selection/SelectionHelper.d.ts +17 -14
  101. package/lib/esm/presentation-frontend/selection/SelectionHelper.d.ts.map +1 -1
  102. package/lib/esm/presentation-frontend/selection/SelectionHelper.js +36 -33
  103. package/lib/esm/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  104. package/lib/esm/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  105. package/lib/esm/presentation-frontend/selection/SelectionManager.js +409 -409
  106. package/lib/esm/presentation-frontend/selection/SelectionManager.js.map +1 -1
  107. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  108. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js +85 -85
  109. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  110. package/lib/esm/presentation-frontend.d.ts +33 -34
  111. package/lib/esm/presentation-frontend.d.ts.map +1 -1
  112. package/lib/esm/presentation-frontend.js +37 -38
  113. package/lib/esm/presentation-frontend.js.map +1 -1
  114. package/package.json +23 -22
  115. package/lib/cjs/presentation-frontend/StateTracker.d.ts +0 -39
  116. package/lib/cjs/presentation-frontend/StateTracker.d.ts.map +0 -1
  117. package/lib/cjs/presentation-frontend/StateTracker.js +0 -130
  118. package/lib/cjs/presentation-frontend/StateTracker.js.map +0 -1
  119. package/lib/esm/presentation-frontend/StateTracker.d.ts +0 -39
  120. package/lib/esm/presentation-frontend/StateTracker.d.ts.map +0 -1
  121. package/lib/esm/presentation-frontend/StateTracker.js +0 -126
  122. package/lib/esm/presentation-frontend/StateTracker.js.map +0 -1
@@ -1,286 +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
- 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
+ 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
+ }
286
283
  //# sourceMappingURL=FavoritePropertiesStorage.js.map