@interopio/browser-platform 3.0.1

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 (97) hide show
  1. package/browser.platform.d.ts +455 -0
  2. package/changelog.md +69 -0
  3. package/dist/browser.platform.es.js +46074 -0
  4. package/dist/browser.platform.es.js.map +1 -0
  5. package/dist/browser.platform.umd.js +46082 -0
  6. package/dist/browser.platform.umd.js.map +1 -0
  7. package/license.md +21 -0
  8. package/package.json +78 -0
  9. package/readme.md +9 -0
  10. package/rollup.config.js +45 -0
  11. package/src/common/constants.ts +46 -0
  12. package/src/common/defaultConfig.ts +56 -0
  13. package/src/common/gateway.d.ts +32 -0
  14. package/src/common/types.ts +222 -0
  15. package/src/connection/gateway.ts +132 -0
  16. package/src/connection/no-gateway.ts +1 -0
  17. package/src/connection/portsBridge.ts +441 -0
  18. package/src/connection/preferred.ts +247 -0
  19. package/src/controllers/domains.ts +156 -0
  20. package/src/controllers/glue.ts +604 -0
  21. package/src/controllers/interception.ts +53 -0
  22. package/src/controllers/license.ts +50 -0
  23. package/src/controllers/main.ts +256 -0
  24. package/src/controllers/plugins.ts +121 -0
  25. package/src/controllers/serviceWorker.ts +193 -0
  26. package/src/controllers/state.ts +84 -0
  27. package/src/controllers/system.ts +99 -0
  28. package/src/controllers/transactions.ts +80 -0
  29. package/src/factory.ts +40 -0
  30. package/src/fallbacks/browser.ts +47 -0
  31. package/src/fallbacks/dekstop.ts +21 -0
  32. package/src/index.ts +18 -0
  33. package/src/libs/applications/appStore/directory.ts +235 -0
  34. package/src/libs/applications/appStore/remoteWatcher.ts +112 -0
  35. package/src/libs/applications/controller.ts +469 -0
  36. package/src/libs/applications/decoders.ts +98 -0
  37. package/src/libs/applications/defaults.ts +6 -0
  38. package/src/libs/applications/types.ts +98 -0
  39. package/src/libs/channels/controller.ts +109 -0
  40. package/src/libs/channels/decoders.ts +19 -0
  41. package/src/libs/extension/controller.ts +120 -0
  42. package/src/libs/extension/decoders.ts +18 -0
  43. package/src/libs/extension/types.ts +11 -0
  44. package/src/libs/intents/constants.ts +9 -0
  45. package/src/libs/intents/controller.ts +500 -0
  46. package/src/libs/intents/decoders.ts +96 -0
  47. package/src/libs/intents/resolverHelper.ts +264 -0
  48. package/src/libs/intents/types.ts +119 -0
  49. package/src/libs/layouts/constants.ts +3 -0
  50. package/src/libs/layouts/controller.ts +476 -0
  51. package/src/libs/layouts/decoders.ts +132 -0
  52. package/src/libs/layouts/global/builder.ts +400 -0
  53. package/src/libs/layouts/global/resetter.ts +116 -0
  54. package/src/libs/layouts/global/restorer.ts +217 -0
  55. package/src/libs/layouts/global/validator.ts +169 -0
  56. package/src/libs/layouts/idbStore.ts +63 -0
  57. package/src/libs/layouts/types.ts +133 -0
  58. package/src/libs/manager/controller.ts +175 -0
  59. package/src/libs/manager/decoders.ts +6 -0
  60. package/src/libs/manager/defaults.ts +5 -0
  61. package/src/libs/manager/identity.ts +92 -0
  62. package/src/libs/manager/session.ts +195 -0
  63. package/src/libs/manager/types.ts +51 -0
  64. package/src/libs/notifications/controller.ts +447 -0
  65. package/src/libs/notifications/decoders.ts +166 -0
  66. package/src/libs/notifications/types.ts +49 -0
  67. package/src/libs/notifications/utils.ts +7 -0
  68. package/src/libs/search/controller.ts +232 -0
  69. package/src/libs/search/decoders.ts +6 -0
  70. package/src/libs/search/repositories/applications.ts +62 -0
  71. package/src/libs/search/repositories/layouts.ts +48 -0
  72. package/src/libs/search/repositories/workspaces.ts +47 -0
  73. package/src/libs/search/types.ts +9 -0
  74. package/src/libs/themes/constants.ts +9 -0
  75. package/src/libs/themes/controller.ts +153 -0
  76. package/src/libs/themes/decoders.ts +28 -0
  77. package/src/libs/themes/types.ts +28 -0
  78. package/src/libs/windows/controller.ts +498 -0
  79. package/src/libs/windows/decoders.ts +64 -0
  80. package/src/libs/windows/types.ts +59 -0
  81. package/src/libs/workspaces/controller.ts +922 -0
  82. package/src/libs/workspaces/decoders.ts +868 -0
  83. package/src/libs/workspaces/defaultConfig.ts +11 -0
  84. package/src/libs/workspaces/frames.ts +239 -0
  85. package/src/libs/workspaces/hibernationWatcher.ts +231 -0
  86. package/src/libs/workspaces/types.ts +506 -0
  87. package/src/platform.ts +156 -0
  88. package/src/shared/decoders.ts +479 -0
  89. package/src/shared/fetchTimeout.ts +25 -0
  90. package/src/shared/ioc.ts +580 -0
  91. package/src/shared/logger.ts +19 -0
  92. package/src/shared/promisePlus.ts +62 -0
  93. package/src/shared/sequelizer.ts +48 -0
  94. package/src/shared/utils.ts +86 -0
  95. package/src/storage/local.ts +111 -0
  96. package/src/storage/session.ts +436 -0
  97. package/tsconfig.json +16 -0
@@ -0,0 +1,400 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ import { IOConnectBrowser } from "@interopio/browser";
3
+ import { IOConnectWorkspaces } from "@interopio/workspaces-api";
4
+ import { IOConnectCore } from "@interopio/core";
5
+ import { BaseSaveRequest, FrameCompleteSnapshot, RawWindowsLayoutDataRequestConfig, SaveLayoutConfig, SaveRequestClientResponse, WindowRawLayoutData, WindowsRawLayoutData } from "../types";
6
+ import logger from "../../../shared/logger";
7
+ import { PromiseWrap } from "../../../shared/promisePlus";
8
+ import { WindowsController } from "../../windows/controller";
9
+ import { WorkspacesController } from "../../workspaces/controller";
10
+ import { GlueController } from "../../../controllers/glue";
11
+ import { SessionStorageController } from "../../../storage/session";
12
+ import { defaultPlatformConfig } from "../../../common/defaultConfig";
13
+ import { defaultNoAppWindowComponentAppName } from "../constants";
14
+ import { SessionWindowData } from "../../../common/types";
15
+
16
+ export class Builder {
17
+
18
+ constructor(
19
+ private readonly glueController: GlueController,
20
+ private readonly sessionStore: SessionStorageController,
21
+ private readonly windowsController: WindowsController,
22
+ private readonly workspacesController: WorkspacesController
23
+ ) {}
24
+
25
+ private get logger(): IOConnectCore.Logger.API | undefined {
26
+ return logger.get("layouts.builder");
27
+ }
28
+
29
+ public async saveGlobalLayout(config: SaveLayoutConfig, commandId: string): Promise<IOConnectBrowser.Layouts.Layout> {
30
+ const saveResponses = await this.getRawWindowsLayoutData({
31
+ layoutType: "Global",
32
+ layoutName: config.layout.name,
33
+ context: config.layout.context,
34
+ instances: config.layout.instances,
35
+ ignoreInstances: config.layout.ignoreInstances
36
+ }, commandId);
37
+
38
+ this.logger?.trace(`[${commandId}] received valid data for the eligible windows`);
39
+
40
+ const existingLayout = await this.glueController.getLayout(config.layout.name);
41
+
42
+ const layout = existingLayout ?
43
+ await this.updateLayout(existingLayout, config.layout, saveResponses.windows, commandId) :
44
+ await this.buildNewLayout(config.layout, saveResponses.windows, commandId);
45
+
46
+ this.logger?.trace(`[${commandId}] the layout object was constructed, importing.`);
47
+
48
+ await this.glueController.importLayout(layout);
49
+
50
+ this.logger?.trace(`[${commandId}] the import for layout: ${layout.name} was completed, responding.`);
51
+
52
+ return layout;
53
+ }
54
+
55
+ private async updateLayout(layout: IOConnectBrowser.Layouts.Layout, saveConfig: IOConnectBrowser.Layouts.NewLayoutOptions, currentWindows: WindowRawLayoutData[], commandId: string): Promise<IOConnectBrowser.Layouts.Layout> {
56
+ this.logger?.trace(`[${commandId}] updating an existing layout with name ${saveConfig.name}`);
57
+
58
+ layout.context = saveConfig.context ?? {};
59
+ layout.metadata = saveConfig.metadata ?? {};
60
+
61
+ const existingLayoutComponentIds = currentWindows.filter((webWindow) => !!webWindow.layoutComponentId).map((webWindow) => webWindow.layoutComponentId as string);
62
+
63
+ const layoutIdOccurrenceMap = this.getLayoutIdOccurrenceMap(existingLayoutComponentIds);
64
+
65
+ const windowComponents = currentWindows.map<IOConnectBrowser.Layouts.WindowComponent>((webWindow) => this.generateWindowComponent(layout, webWindow, layoutIdOccurrenceMap, commandId));
66
+
67
+ this.logger?.trace(`[${commandId}] the window components are completed, we have ${windowComponents.length} windows for the layout`);
68
+
69
+ const workspacesSaveRequest: BaseSaveRequest = {
70
+ layoutName: saveConfig.name,
71
+ layoutType: "Global",
72
+ context: saveConfig.context
73
+ };
74
+
75
+ const preExistingFrameComponents = layout.components.filter((component): component is IOConnectBrowser.Layouts.WorkspaceFrameComponent => component.type === "workspaceFrame");
76
+
77
+ const workspaceFrameComponents = await this.compileWorkspacesFrameComponents(preExistingFrameComponents, workspacesSaveRequest, commandId);
78
+
79
+ this.logger?.trace(`[${commandId}] the workspaces frame components are completed, we have ${workspaceFrameComponents.length} frames for the layout`);
80
+
81
+ layout.components = [];
82
+
83
+ layout.components.push(...windowComponents);
84
+ layout.components.push(...workspaceFrameComponents);
85
+
86
+ return layout;
87
+ }
88
+
89
+ private async buildNewLayout(saveConfig: IOConnectBrowser.Layouts.NewLayoutOptions, windowResponses: WindowRawLayoutData[], commandId: string): Promise<IOConnectBrowser.Layouts.Layout> {
90
+ this.logger?.trace(`[${commandId}] build a brand new layout with name ${saveConfig.name}`);
91
+
92
+ const layout: IOConnectBrowser.Layouts.Layout = {
93
+ name: saveConfig.name,
94
+ type: "Global",
95
+ context: saveConfig.context ?? {},
96
+ metadata: saveConfig.metadata ?? {},
97
+ components: [],
98
+ version: 2
99
+ };
100
+
101
+ const windowComponents = windowResponses.map<IOConnectBrowser.Layouts.WindowComponent>((response) => this.buildNewWindowComponent(response, commandId));
102
+
103
+ this.logger?.trace(`[${commandId}] the window components are completed, we have ${windowComponents.length} windows for the layout`);
104
+
105
+ const workspacesSaveRequest: BaseSaveRequest = {
106
+ layoutName: saveConfig.name,
107
+ layoutType: "Global",
108
+ context: saveConfig.context
109
+ };
110
+
111
+ const workspaceFrameComponents = await this.compileWorkspacesFrameComponents([], workspacesSaveRequest, commandId);
112
+
113
+ this.logger?.trace(`[${commandId}] the workspaces frame components are completed, we have ${workspaceFrameComponents.length} frames for the layout`);
114
+
115
+ layout.components.push(...windowComponents);
116
+ layout.components.push(...workspaceFrameComponents);
117
+
118
+ return layout;
119
+ }
120
+
121
+ private async getRawWindowsLayoutData(config: RawWindowsLayoutDataRequestConfig, commandId: string): Promise<WindowsRawLayoutData> {
122
+ this.logger?.trace(`[${commandId}] handling send save requests for layout: ${config.layoutName} to instances: ${config.instances?.join(", ")}`);
123
+
124
+ const glueWindowsRawData = await Promise.all(
125
+ this.getEligibleGlueWindows(config.instances, config.ignoreInstances).map((glueWindow) => this.buildRawGlueWindowData(glueWindow, config, commandId))
126
+ );
127
+
128
+ const nonGlueWindowsRawData = await Promise.all(
129
+ this.getEligibleNonGlueWindows(config.instances, config.ignoreInstances).map((glueWindow) => this.buildRawNonGlueWindowData(glueWindow))
130
+ );
131
+
132
+ const result: WindowsRawLayoutData = {
133
+ windows: [...glueWindowsRawData, ...nonGlueWindowsRawData]
134
+ };
135
+
136
+ this.logger?.trace(`[${commandId}] request completed, responding to the caller`);
137
+
138
+ return result;
139
+ }
140
+
141
+ private async buildRawGlueWindowData(windowData: SessionWindowData, requestConfig: RawWindowsLayoutDataRequestConfig, commandId: string): Promise<WindowRawLayoutData> {
142
+ const timeoutMessage = `Cannot fetch the layout save data from: ${windowData.name} with id: ${windowData.windowId}`;
143
+
144
+ if (!windowData.initialUrl) {
145
+ throw new Error(`Missing URL for client: ${windowData.name}`);
146
+ }
147
+
148
+ // the response will throw when communicating with an older Glue Web client which cannot service this message
149
+ const saveRequestResponse = await PromiseWrap<SaveRequestClientResponse>(async () => {
150
+ try {
151
+ const clientResponse = await this.glueController.callWindow<RawWindowsLayoutDataRequestConfig, SaveRequestClientResponse>("layouts", { name: "clientSaveRequest", execute: async () => {} }, requestConfig, { windowId: windowData.windowId });
152
+ return clientResponse;
153
+ } catch (error) {
154
+ return {};
155
+ }
156
+
157
+ }, 15000, timeoutMessage);
158
+
159
+ const instanceData = this.sessionStore.getAllInstancesData().find((instance) => instance.id === windowData.windowId);
160
+
161
+ const windowBounds = await this.windowsController.getWindowBounds(windowData.windowId, commandId);
162
+
163
+ const clientProvidedContext = saveRequestResponse.windowContext ?? {};
164
+
165
+ return {
166
+ bounds: windowBounds,
167
+ windowContext: clientProvidedContext,
168
+ url: windowData.initialUrl,
169
+ name: windowData.name,
170
+ application: instanceData ? instanceData.applicationName : defaultNoAppWindowComponentAppName,
171
+ initialContext: windowData.initialContext,
172
+ windowId: windowData.windowId,
173
+ layoutComponentId: windowData.layoutComponentId
174
+ };
175
+ }
176
+
177
+ private async buildRawNonGlueWindowData(windowData: SessionWindowData): Promise<WindowRawLayoutData> {
178
+ if (!windowData.initialUrl) {
179
+ throw new Error(`Missing URL for client: ${windowData.name}`);
180
+ }
181
+
182
+ const instanceData = this.sessionStore.getAllInstancesData().find((instance) => instance.id === windowData.windowId);
183
+
184
+ return {
185
+ bounds: windowData.initialBounds ?? defaultPlatformConfig.windows.defaultWindowOpenBounds,
186
+ windowContext: {},
187
+ url: windowData.initialUrl,
188
+ name: windowData.name,
189
+ application: instanceData ? instanceData.applicationName : defaultNoAppWindowComponentAppName,
190
+ initialContext: windowData.initialContext,
191
+ windowId: windowData.windowId,
192
+ layoutComponentId: windowData.layoutComponentId
193
+ };
194
+ }
195
+
196
+ private getEligibleNonGlueWindows(requestedInstances?: string[], ignoreInstances?: string[]): SessionWindowData[] {
197
+ const allEligibleWindows = this.getAllEligibleWindows(requestedInstances, ignoreInstances);
198
+
199
+ const allNonGlueWindows = this.sessionStore.getAllNonGlue();
200
+
201
+ const allWorkspaceClients = this.sessionStore.pickWorkspaceClients(() => true);
202
+
203
+ return allEligibleWindows
204
+ .filter((eligibleWindow) =>
205
+ allNonGlueWindows.some((nonGlueWebWindow) => nonGlueWebWindow.windowId === eligibleWindow.windowId) &&
206
+ allWorkspaceClients.every((workspaceClient) => workspaceClient.windowId !== eligibleWindow.windowId)
207
+ );
208
+ }
209
+
210
+ private getEligibleGlueWindows(requestedInstances?: string[], ignoreInstances?: string[]): SessionWindowData[] {
211
+ const allEligibleWindows = this.getAllEligibleWindows(requestedInstances, ignoreInstances);
212
+
213
+ const allNonGlueWindows = this.sessionStore.getAllNonGlue();
214
+
215
+ const allWorkspaceClients = this.sessionStore.pickWorkspaceClients(() => true);
216
+
217
+ return allEligibleWindows
218
+ .filter((eligibleWindow) =>
219
+ allWorkspaceClients.every((workspaceClient) => workspaceClient.windowId !== eligibleWindow.windowId) &&
220
+ allNonGlueWindows.every((nonGlueWebWindow) => nonGlueWebWindow.windowId !== eligibleWindow.windowId)
221
+ );
222
+ }
223
+
224
+ private getAllEligibleWindows(requestedInstances?: string[], ignoreInstances?: string[]): SessionWindowData[] {
225
+ let allNonPlatformWindows = this.sessionStore.getAllWindowsData().filter((webWindow) => webWindow.name !== "Platform");
226
+
227
+ if (requestedInstances && requestedInstances.length) {
228
+ const requestedServers = this.glueController.getServers().filter((server) => requestedInstances.some((instanceId) => server.instance === instanceId));
229
+
230
+ allNonPlatformWindows = allNonPlatformWindows.filter((eligibleWindow) => requestedServers.some((server) => server.windowId === eligibleWindow.windowId));
231
+ }
232
+
233
+ if (ignoreInstances && ignoreInstances.length) {
234
+ const ignoredServers = this.glueController.getServers().filter((server) => ignoreInstances.some((instanceId) => server.instance === instanceId));
235
+
236
+ allNonPlatformWindows = allNonPlatformWindows.filter((eligibleWindow) => ignoredServers.every((server) => server.windowId !== eligibleWindow.windowId));
237
+ }
238
+
239
+ return allNonPlatformWindows;
240
+ }
241
+
242
+
243
+ private updateExistingWindowComponent(component: IOConnectBrowser.Layouts.WindowComponent, response: WindowRawLayoutData, commandId: string): IOConnectBrowser.Layouts.WindowComponent {
244
+ this.logger?.trace(`[${commandId}] performing a soft update on am existing component ${component.application} with id ${component.state.instanceId}`);
245
+
246
+ component.state.context = response.windowContext ? response.windowContext : component.state.context;
247
+ component.state.bounds = response.bounds;
248
+ component.state.createArgs.context = response.initialContext ? response.initialContext : component.state.createArgs?.context;
249
+ component.state.instanceId = component.state.instanceId ? component.state.instanceId : response.windowId;
250
+
251
+ return component;
252
+ }
253
+
254
+ private buildNewWindowComponent(response: WindowRawLayoutData, commandId: string): IOConnectBrowser.Layouts.WindowComponent {
255
+ this.logger?.trace(`[${commandId}] building a brand new component ${response.application} with id ${response.windowId}`);
256
+
257
+ return {
258
+ type: "window",
259
+ componentType: "application",
260
+ application: response.application,
261
+ state: {
262
+ context: response.windowContext ?? {},
263
+ bounds: response.bounds,
264
+ createArgs: {
265
+ name: response.name,
266
+ url: response.url,
267
+ context: response.initialContext ?? {}
268
+ },
269
+ windowState: "Normal",
270
+ restoreState: "Normal",
271
+ restoreSettings: {
272
+ groupId: "glue_42_core",
273
+ groupZOrder: 0
274
+ },
275
+ instanceId: response.windowId,
276
+ isSticky: true,
277
+ isCollapsed: false
278
+ }
279
+ };
280
+ }
281
+
282
+ private async compileWorkspacesFrameComponents(preExistingFrameComponents: IOConnectBrowser.Layouts.WorkspaceFrameComponent[], request: BaseSaveRequest, commandId: string): Promise<IOConnectBrowser.Layouts.WorkspaceFrameComponent[]> {
283
+ this.logger?.trace(`[${commandId}] requesting information about all running workspaces frames`);
284
+
285
+ const allFramesData = await this.getAllFramesSnapshotsWithBounds(request, commandId);
286
+
287
+ this.logger?.trace(`[${commandId}] got information on ${allFramesData.length} frames, composing the frame components`);
288
+
289
+ const existingLayoutComponentIds = allFramesData.filter((frame) => !!frame.config?.layoutComponentId).map((frame) => frame.config.layoutComponentId as string);
290
+
291
+ const layoutIdOccurrenceMap = this.getLayoutIdOccurrenceMap(existingLayoutComponentIds);
292
+
293
+ const framesComponents = allFramesData.map<IOConnectBrowser.Layouts.WorkspaceFrameComponent>((frameData) => this.generateFrameComponent(frameData, preExistingFrameComponents, layoutIdOccurrenceMap, commandId));
294
+
295
+ return framesComponents;
296
+ }
297
+
298
+ private getLayoutIdOccurrenceMap(layoutComponentIds: string[]): { [key in string]: number } {
299
+
300
+ const duplications: { [key in string]: number } = {};
301
+
302
+ layoutComponentIds.forEach((layoutComponentId) => {
303
+
304
+ if (duplications[layoutComponentId]) {
305
+ duplications[layoutComponentId] = 1 + duplications[layoutComponentId];
306
+ return;
307
+ }
308
+
309
+ duplications[layoutComponentId] = 1;
310
+ });
311
+
312
+ return duplications;
313
+ }
314
+
315
+ private softUpdateFrameComponent(preExistingComponent: IOConnectBrowser.Layouts.WorkspaceFrameComponent, frameData: FrameCompleteSnapshot, selectedWspIndex: number, commandId: string): IOConnectBrowser.Layouts.WorkspaceFrameComponent {
316
+ this.logger?.trace(`[${commandId}] performing a soft update on am existing frame component ${preExistingComponent.state.instanceId}`);
317
+ preExistingComponent.state.bounds = frameData.bounds;
318
+ preExistingComponent.state.selectedWorkspace = selectedWspIndex === -1 ? 0 : selectedWspIndex;
319
+ preExistingComponent.state.workspaces = frameData.snapshot.workspaces;
320
+ preExistingComponent.state.context = Object.assign({}, preExistingComponent.state.context, { isPlatform: frameData.config.isPlatform });
321
+
322
+ return preExistingComponent;
323
+ }
324
+
325
+ private createNewFrameComponent(frameData: FrameCompleteSnapshot, selectedWspIndex: number, commandId: string): IOConnectBrowser.Layouts.WorkspaceFrameComponent {
326
+ this.logger?.trace(`[${commandId}] building a new frame component ${frameData.snapshot.id}`);
327
+
328
+ return {
329
+ type: "workspaceFrame",
330
+ application: "workspaces-demo",
331
+ componentType: "application",
332
+ state: {
333
+ context: { isPlatform: frameData.config.isPlatform },
334
+ bounds: frameData.bounds,
335
+ instanceId: frameData.snapshot.id,
336
+ selectedWorkspace: selectedWspIndex === -1 ? 0 : selectedWspIndex,
337
+ workspaces: frameData.snapshot.workspaces,
338
+ restoreState: "Normal",
339
+ windowState: "Normal"
340
+ }
341
+ };
342
+ }
343
+
344
+ private generateWindowComponent(layout: IOConnectBrowser.Layouts.Layout, webWindow: WindowRawLayoutData, layoutIdOccurrenceMap: { [key in string]: number }, commandId: string): IOConnectBrowser.Layouts.WindowComponent {
345
+ const existingComponent = layout.components.find((component) => component.type === "window" && component.state.instanceId === webWindow.layoutComponentId) as IOConnectBrowser.Layouts.WindowComponent;
346
+
347
+ // if there are more than one windows with the same layoutComponentId, this means that
348
+ // the same layout was restored more than once without closing the running instances
349
+ // if we save in this situation, we cannot perform a soft-update of the layout
350
+ const layoutComponentIdDuplications = webWindow.layoutComponentId ? layoutIdOccurrenceMap[webWindow.layoutComponentId] : 0;
351
+
352
+ return existingComponent && layoutComponentIdDuplications < 2 ?
353
+ this.updateExistingWindowComponent(existingComponent, webWindow, commandId) :
354
+ this.buildNewWindowComponent(webWindow, commandId);
355
+ }
356
+
357
+ private generateFrameComponent(frameData: FrameCompleteSnapshot, preExistingFrameComponents: IOConnectBrowser.Layouts.WorkspaceFrameComponent[], layoutIdOccurrenceMap: { [key in string]: number }, commandId: string): IOConnectBrowser.Layouts.WorkspaceFrameComponent {
358
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
359
+ const selectedWspIndex = frameData.snapshot.workspaces.findIndex((wsp) => wsp?.config?.isSelected);
360
+
361
+ const preExistingComponent = preExistingFrameComponents.find((component) => component.state.instanceId === frameData.config.layoutComponentId);
362
+
363
+ // if there are more than one windows with the same layoutComponentId, this means that
364
+ // the same layout was restored more than once without closing the running instances
365
+ // if we save in this situation, we cannot perform a soft-update of the layout
366
+ const layoutComponentIdDuplications = frameData.config.layoutComponentId ? layoutIdOccurrenceMap[frameData.config.layoutComponentId] : 0;
367
+
368
+ if (preExistingComponent && layoutComponentIdDuplications < 2) {
369
+ return this.softUpdateFrameComponent(preExistingComponent, frameData, selectedWspIndex, commandId);
370
+ }
371
+
372
+ return this.createNewFrameComponent(frameData, selectedWspIndex, commandId);
373
+ }
374
+
375
+ private async getAllFramesSnapshotsWithBounds(request: BaseSaveRequest, commandId: string): Promise<FrameCompleteSnapshot[]> {
376
+
377
+ const allFramesSummaries = (await this.workspacesController.getAllFramesSummaries(undefined, commandId)).summaries || [];
378
+
379
+ return await Promise.all(allFramesSummaries.map(async (frameSummary) => {
380
+ const snapshot = await this.workspacesController.handleGetWorkspacesLayouts({ frameId: frameSummary.id, ...request }, commandId);
381
+
382
+ const frameConfig = await this.workspacesController.getFrameSessionData({ frameId: frameSummary.id }, commandId);
383
+
384
+ const bounds = (await this.workspacesController.getFrameBounds({ itemId: frameSummary.id }, commandId)).bounds;
385
+
386
+ return {
387
+ bounds,
388
+ snapshot: {
389
+ id: frameSummary.id,
390
+ workspaces: snapshot.workspaces as IOConnectWorkspaces.WorkspaceSnapshot[],
391
+ config: {}
392
+ },
393
+ config: {
394
+ isPlatform: frameConfig?.isPlatform,
395
+ layoutComponentId: frameConfig?.layoutComponentId
396
+ }
397
+ };
398
+ }));
399
+ }
400
+ }
@@ -0,0 +1,116 @@
1
+ import { IOConnectCore } from "@interopio/core";
2
+ import logger from "../../../shared/logger";
3
+ import { GlueController } from "../../../controllers/glue";
4
+ import { WorkspacesController } from "../../workspaces/controller";
5
+
6
+ export class Resetter {
7
+ constructor(
8
+ private readonly glueController: GlueController,
9
+ private readonly workspacesController: WorkspacesController
10
+ ) {}
11
+
12
+ private get logger(): IOConnectCore.Logger.API | undefined {
13
+ return logger.get("layouts.resetter");
14
+ }
15
+
16
+ public async closeAllExceptCaller(callerId: string, commandId: string): Promise<void> {
17
+ // closing all non-platform, non-caller and non-workspace windows
18
+ const allWindows = this.glueController.getAllOtherNonPlatformWindows(callerId);
19
+
20
+ await Promise.all(allWindows.map(async (webWindow) => {
21
+
22
+ if (this.glueController.isWorkspacesEnabled) {
23
+ const foundWorkspaceWindow = await this.glueController.getWorkspaceWindowById(webWindow.id);
24
+
25
+ if (foundWorkspaceWindow) {
26
+ return;
27
+ }
28
+ }
29
+
30
+ return webWindow.close();
31
+ }));
32
+
33
+ if (this.glueController.isWorkspacesEnabled) {
34
+ await this.closeNecessaryWorkspacesFrames(callerId, commandId);
35
+ }
36
+ }
37
+
38
+ public async closeCaller(callerType: "plugin" | "client", callerId: string, commandId: string): Promise<void> {
39
+ // if the callerType is not client, this means that the caller is a platform plugin and
40
+ // we can't close the platform
41
+ if (callerType === "plugin") {
42
+ return;
43
+ }
44
+
45
+ const callerWorkspaceWindow = await this.glueController.getWorkspaceWindowById(callerId);
46
+
47
+ if (callerWorkspaceWindow) {
48
+ await this.cleanupWorkspaceCaller(callerId, commandId);
49
+ return;
50
+ }
51
+
52
+ const foundCaller = this.glueController.getWindowById(callerId);
53
+
54
+ if (!foundCaller || foundCaller.name === "Platform") {
55
+ this.logger?.warn("The close caller was missing, is an iframe or is a platform");
56
+ return;
57
+ }
58
+
59
+ await foundCaller.close();
60
+ }
61
+
62
+ private async closeNecessaryWorkspacesFrames(callerId: string, commandId: string): Promise<void> {
63
+ // close all workspaces frames except the platform and the frame of the caller
64
+ const platformFrameId = (await this.workspacesController.handleGetPlatformFrameId({}, commandId)).id;
65
+
66
+ let allFrames = await this.glueController.getAllWorkspacesFrames();
67
+
68
+ if (platformFrameId) {
69
+ allFrames = allFrames.filter((frame) => frame.id !== platformFrameId);
70
+ await this.cleanUpFrameExceptCaller(platformFrameId, callerId);
71
+ }
72
+
73
+ const callerWorkspaceWindow = await this.glueController.getWorkspaceWindowById(callerId);
74
+
75
+ if (callerWorkspaceWindow) {
76
+ allFrames = allFrames.filter((frame) => frame.id !== callerWorkspaceWindow.frameId);
77
+ await this.cleanUpFrameExceptCaller(callerWorkspaceWindow.frameId, callerId);
78
+ }
79
+
80
+ await Promise.all(allFrames.map((frame) => frame.close()));
81
+ }
82
+
83
+ private async cleanUpFrameExceptCaller(frameId: string, callerId: string): Promise<void> {
84
+ const targetWorkspaces = await this.glueController.getWorkspacesByFrameId(frameId);
85
+
86
+ // close all workspaces in the frame, where the caller is not
87
+ const workspacesWithoutCaller = targetWorkspaces.filter((workspace) => !workspace.getWindow((workspaceWindow) => workspaceWindow.id === callerId));
88
+ const callerWorkspace = targetWorkspaces.find((workspace) => workspace.getWindow((workspaceWindow) => workspaceWindow.id === callerId));
89
+
90
+ await Promise.all(workspacesWithoutCaller.map((workspace) => workspace.close()));
91
+
92
+ // close all remaining workspace windows, which are not the caller
93
+ const allRemainingWindows = callerWorkspace ?
94
+ callerWorkspace.getAllWindows((workspaceWindow) => workspaceWindow.id !== callerId) :
95
+ [];
96
+
97
+ await Promise.all(allRemainingWindows.map((workspaceWindow) => workspaceWindow.close()));
98
+ }
99
+
100
+ private async cleanupWorkspaceCaller(callerId: string, commandId: string): Promise<void> {
101
+ const platformFrameId = (await this.workspacesController.handleGetPlatformFrameId({}, commandId)).id;
102
+
103
+ const callerWorkspaceWindow = await this.glueController.getWorkspaceWindowById(callerId);
104
+
105
+ if (!callerWorkspaceWindow) {
106
+ return;
107
+ }
108
+
109
+ if (callerWorkspaceWindow.frameId === platformFrameId) {
110
+ await callerWorkspaceWindow.workspace.close();
111
+ return;
112
+ }
113
+
114
+ await callerWorkspaceWindow.frame.close();
115
+ }
116
+ }