@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,11 @@
1
+ import { IOConnectBrowserPlatform } from "../../../browser.platform";
2
+
3
+ export const defaultLoadingConfig: IOConnectBrowserPlatform.Workspaces.LoadingConfig = {
4
+ defaultStrategy: "direct",
5
+ delayed: {
6
+ batch: 1,
7
+ initialOffsetInterval: 1000,
8
+ interval: 5000
9
+ },
10
+ showDelayedIndicator: false
11
+ };
@@ -0,0 +1,239 @@
1
+ import { IOConnectBrowser } from "@interopio/browser";
2
+ import { IOConnectWorkspaces } from "@interopio/workspaces-api";
3
+ import { generate } from "shortid";
4
+ import { IOConnectBrowserPlatform } from "../../../browser.platform";
5
+ import { BridgeOperation } from "../../common/types";
6
+ import { GlueController } from "../../controllers/glue";
7
+ import { SessionStorageController } from "../../storage/session";
8
+ import { IoC } from "../../shared/ioc";
9
+ import { PromisePlus } from "../../shared/promisePlus";
10
+ import { FrameInstance, FrameLock, FrameQueryConfig, FrameSessionData, FrameSummaryResult, GetFrameSummaryConfig } from "./types";
11
+
12
+ export class FramesController {
13
+ private config!: IOConnectBrowserPlatform.Workspaces.Config;
14
+ private defaultBounds!: IOConnectBrowser.Windows.Bounds;
15
+ private frameSummaryOperation!: BridgeOperation;
16
+ private locks: { [key: string]: FrameLock } = {};
17
+ private defaultFrameHelloTimeoutMs = 15000;
18
+ private myFrameId: string | undefined;
19
+ private _handleUnload?: () => void;
20
+
21
+ constructor(
22
+ private readonly sessionController: SessionStorageController,
23
+ private readonly glueController: GlueController,
24
+ private readonly ioc: IoC
25
+ ) { }
26
+
27
+ public stop(): void {
28
+ if (this._handleUnload) {
29
+ window.removeEventListener("unload", this._handleUnload);
30
+ }
31
+ }
32
+
33
+ public async start(config: IOConnectBrowserPlatform.Workspaces.Config, defaultBounds: IOConnectBrowser.Windows.Bounds, frameSummaryOperation: BridgeOperation): Promise<void> {
34
+ this.config = config;
35
+ this.defaultBounds = defaultBounds;
36
+ this.frameSummaryOperation = frameSummaryOperation;
37
+
38
+ if (config.isFrame) {
39
+ this.myFrameId = this.sessionController.getAllFrames().find((frame) => frame.isPlatform)?.windowId;
40
+
41
+ this._handleUnload = this.handleUnload.bind(this);
42
+
43
+ window.addEventListener("unload", this._handleUnload);
44
+ }
45
+ }
46
+
47
+ public async openFrame(newFrameConfig?: IOConnectWorkspaces.NewFrameConfig | boolean, layoutComponentId?: string): Promise<FrameInstance> {
48
+
49
+ const providedBounds = typeof newFrameConfig === "object" ? newFrameConfig.bounds ?? {} : {};
50
+
51
+ const openBounds: IOConnectBrowser.Windows.Bounds = {
52
+ top: providedBounds.top ?? this.defaultBounds.top,
53
+ left: providedBounds.left ?? this.defaultBounds.left,
54
+ width: providedBounds.width ?? this.defaultBounds.width,
55
+ height: providedBounds.height ?? this.defaultBounds.height
56
+ };
57
+
58
+ const frameWindowId = typeof newFrameConfig === "object" && newFrameConfig?.frameId ? newFrameConfig.frameId : `g42-${generate()}`;
59
+
60
+ const allExistingFrames = this.sessionController.getAllFrames();
61
+
62
+ if (allExistingFrames.some((frame) => frame.windowId === frameWindowId)) {
63
+ throw new Error(`Cannot open a frame with id: ${frameWindowId}, because a frame with this id already exists`);
64
+ }
65
+
66
+ const frameData: FrameSessionData = {
67
+ windowId: frameWindowId,
68
+ active: false,
69
+ isPlatform: false,
70
+ layoutComponentId
71
+ };
72
+
73
+ const options = `left=${openBounds.left},top=${openBounds.top},width=${openBounds.width},height=${openBounds.height}`;
74
+
75
+ const frameSrc = (await this.getWorkspacesUrls()).workspacesUrl.current;
76
+
77
+ const frameUrl = `${frameSrc}?emptyFrame=true`;
78
+
79
+ const childWindow = window.open(frameUrl, frameData.windowId, options);
80
+
81
+ if (!childWindow) {
82
+ throw new Error("Cannot open a new workspace frame, because the user has not allowed popups or uses a blocker");
83
+ }
84
+
85
+ this.sessionController.saveFrameData(frameData);
86
+
87
+ try {
88
+ await this.waitHello(frameData.windowId);
89
+ return { windowId: frameData.windowId };
90
+ } catch (error) {
91
+ delete this.locks[frameData.windowId];
92
+ throw new Error("Cannot open a new frame, because the workspace frame app did not send a hello in time");
93
+ }
94
+ }
95
+
96
+ public async closeFrame(windowId: string): Promise<void> {
97
+ const frameData = this.sessionController.getFrameData(windowId);
98
+
99
+ if (!frameData) {
100
+ throw new Error(`Cannot close a frame with id: ${windowId}, because it is not known by the platform`);
101
+ }
102
+
103
+ this.handleFrameDisappeared(windowId);
104
+
105
+ window.open(undefined, windowId)?.close();
106
+ }
107
+
108
+ public processNewHello(windowId: string): void {
109
+ const frameData = this.sessionController.getFrameData(windowId);
110
+
111
+ if (!frameData) {
112
+ return;
113
+ }
114
+
115
+ this.sessionController.setFrameActive(windowId);
116
+
117
+ this.locks[windowId]?.lift();
118
+ }
119
+
120
+ public handleFrameDisappeared(frameId: string): void {
121
+ const foundFrame = this.sessionController.getFrameData(frameId);
122
+
123
+ if (!foundFrame) {
124
+ return;
125
+ }
126
+
127
+ this.sessionController.removeFrameData(frameId);
128
+
129
+ this.clearAllWorkspaceWindows(frameId);
130
+ }
131
+
132
+ public getAll(): FrameInstance[] {
133
+ const allFrames = this.sessionController.getAllFrames();
134
+
135
+ return allFrames.filter((frame) => frame.active).map((frame) => ({ windowId: frame.windowId }));
136
+ }
137
+
138
+ public async getFrameInstance(config?: FrameQueryConfig): Promise<FrameInstance> {
139
+ const props: Array<"frameId" | "itemId" | "newFrame"> = ["frameId", "itemId", "newFrame"];
140
+
141
+ if (config) {
142
+ const definedKeys = props.reduce<string[]>((soFar, prop) => {
143
+ if (config[prop]) {
144
+ soFar.push(prop);
145
+ }
146
+ return soFar;
147
+ }, []);
148
+
149
+ if (definedKeys.length > 1) {
150
+ throw new Error(`Cannot retrieve the frame, because of over-specification: the provided selection object must have either 1 or none of the possible properties: ${JSON.stringify(config)}`);
151
+ }
152
+ }
153
+
154
+ const allFrames = this.getAll();
155
+
156
+ if (config?.frameId) {
157
+ const foundFrame = allFrames.find((frame) => frame.windowId === config.frameId);
158
+
159
+ if (!foundFrame) {
160
+ throw new Error(`Cannot retrieve a frame with Id: ${config.frameId}, because it is not known by the platform`);
161
+ }
162
+
163
+ return foundFrame;
164
+ }
165
+
166
+ if (config?.itemId) {
167
+ return this.getFrameByItemId(config.itemId, allFrames);
168
+ }
169
+
170
+ if (config?.newFrame) {
171
+ return this.openFrame(config.newFrame);
172
+ }
173
+
174
+ return allFrames.length ? this.getLastOpenedFrame() : this.openFrame();
175
+ }
176
+
177
+ public getPlatformFrameSessionData(): FrameSessionData | undefined {
178
+ return this.sessionController.getAllFrames().find((frame) => frame.isPlatform);
179
+ }
180
+
181
+ public getFrameConfig(frameId: string): FrameSessionData | undefined {
182
+ return this.sessionController.getAllFrames().find((frame) => frame.windowId === frameId);
183
+ }
184
+
185
+ private clearAllWorkspaceWindows(frameId: string): void {
186
+ const workspaceWindows = this.sessionController.pickWorkspaceClients((client) => client.frameId === frameId);
187
+
188
+ workspaceWindows.forEach((workspaceWindow) => this.ioc.applicationsController.unregisterWorkspaceApp({ windowId: workspaceWindow.windowId }));
189
+ }
190
+
191
+ private async waitHello(windowId: string): Promise<void> {
192
+ return PromisePlus((resolve) => {
193
+ this.locks[windowId] = { lift: resolve };
194
+ }, this.defaultFrameHelloTimeoutMs, "Frame hello timed out");
195
+ }
196
+
197
+ private getLastOpenedFrame(): FrameInstance {
198
+ const allData = this.sessionController.getAllFrames().filter((frame) => frame.active);
199
+
200
+ return allData[allData.length - 1];
201
+ }
202
+
203
+ private async getFrameByItemId(itemId: string, frames: FrameInstance[]): Promise<FrameInstance> {
204
+ if (!frames.length) {
205
+ throw new Error(`Cannot get frame by item id for: ${itemId}, because not frames were found`);
206
+ }
207
+
208
+ for (const frame of frames) {
209
+ const queryResult = await this.glueController.callFrame<GetFrameSummaryConfig, FrameSummaryResult>(this.frameSummaryOperation, { itemId }, frame.windowId);
210
+
211
+ if (queryResult.id !== "none") {
212
+ return frame;
213
+ }
214
+ }
215
+
216
+ throw new Error(`Cannot find frame for item: ${itemId}`);
217
+ }
218
+
219
+ private getWorkspacesUrls(): Promise<{ workspacesUrl: { current: string; default: string } }> {
220
+ const currentProtocol = (new URL(window.location.href)).protocol;
221
+
222
+ if (!currentProtocol.includes("extension")) {
223
+ return Promise.resolve({ workspacesUrl: { current: this.config.src, default: this.config.src } });
224
+ }
225
+
226
+ return new Promise((resolve) => {
227
+ chrome.storage.local.get("workspacesUrl", (entry) => {
228
+ resolve(entry as { workspacesUrl: { current: string; default: string } });
229
+ });
230
+ });
231
+ }
232
+
233
+ private handleUnload(): void {
234
+ if (this.myFrameId) {
235
+ this.clearAllWorkspaceWindows(this.myFrameId);
236
+ }
237
+ }
238
+
239
+ }
@@ -0,0 +1,231 @@
1
+ import { WindowStreamData, WorkspaceConfigResult, WorkspaceEventPayload, WorkspaceSnapshotResult, WorkspaceStreamData } from "./types";
2
+ import { WorkspacesController } from "./controller";
3
+ import { generate } from "shortid";
4
+ import { IOConnectBrowserPlatform } from "../../../browser.platform";
5
+ import logger from "../../shared/logger";
6
+ import { IOConnectBrowser } from "@interopio/browser";
7
+ import { SessionStorageController } from "../../storage/session";
8
+ import { AsyncSequelizer } from "../../shared/sequelizer";
9
+
10
+ export class WorkspaceHibernationWatcher {
11
+ private workspacesController!: WorkspacesController;
12
+ private settings: IOConnectBrowserPlatform.Workspaces.HibernationConfig | undefined;
13
+ private running?: boolean;
14
+
15
+ constructor(private readonly session: SessionStorageController, private readonly sequelizer: AsyncSequelizer) {
16
+ }
17
+
18
+ private get logger(): IOConnectBrowser.Logger.API | undefined {
19
+ return logger.get("workspaces.hibernation");
20
+ }
21
+
22
+ public stop(): void {
23
+ this.running = false;
24
+ }
25
+
26
+ public start(workspacesController: WorkspacesController, settings: IOConnectBrowserPlatform.Workspaces.HibernationConfig): void {
27
+
28
+ this.logger?.trace(`starting the hibernation watcher with following settings: ${JSON.stringify(this.settings)}`);
29
+
30
+ this.running = true;
31
+
32
+ this.workspacesController = workspacesController;
33
+ this.settings = settings;
34
+
35
+ const allTimeoutData = this.session.exportClearTimeouts();
36
+
37
+ if (this.settings?.idleWorkspaces?.idleMSThreshold) {
38
+ allTimeoutData.forEach((timeoutData) => this.buildTimer(timeoutData.workspaceId));
39
+ }
40
+
41
+ this.logger?.trace("The hibernation watcher has started successfully");
42
+ }
43
+
44
+ public notifyEvent(event: WorkspaceEventPayload): void {
45
+
46
+ if (event.type === "window") {
47
+ this.handleWorkspaceWindowEvent(event);
48
+ }
49
+
50
+ if (event.type === "workspace") {
51
+ this.handleWorkspaceEvent(event);
52
+ }
53
+ }
54
+
55
+ private handleWorkspaceWindowEvent(event: WorkspaceEventPayload): void {
56
+ // listens for new windows, in case an empty workspace is no longer empty, therefore adding it to the standard hibernation logic
57
+ const isWindowOpened = event.action === "opened" || event.action === "added";
58
+
59
+ if (!isWindowOpened) {
60
+ return;
61
+ }
62
+
63
+ this.sequelizer.enqueue(() => this.checkMaximumAmountCore());
64
+ this.addTimersForWorkspacesInFrame((event.payload as WindowStreamData).windowSummary.config.frameId);
65
+ }
66
+
67
+ private handleWorkspaceEvent(event: WorkspaceEventPayload): void {
68
+ const isWorkspaceSelected = event.action === "selected";
69
+ const isLockConfigurationChanged = event.action === "lock-configuration-changed";
70
+
71
+ const workspaceData = event.payload as WorkspaceStreamData;
72
+ const isEventHibernationRelated = event.action === "selected" || event.action === "opened" || event.action === "lock-configuration-changed";
73
+
74
+ if (!isEventHibernationRelated) {
75
+ return;
76
+ }
77
+
78
+ this.sequelizer.enqueue(() => this.checkMaximumAmountCore());
79
+
80
+ const isSystemHibernationAllowed = workspaceData.workspaceSummary.config.allowSystemHibernation;
81
+ const isSystemHibernationBeingEnabled = isLockConfigurationChanged && isSystemHibernationAllowed;
82
+ const willAffectLastActiveTimer = isWorkspaceSelected || isSystemHibernationBeingEnabled;
83
+
84
+ if (!willAffectLastActiveTimer) {
85
+ return;
86
+ }
87
+
88
+ const timeout = this.session.getTimeout(workspaceData.workspaceSummary.id);
89
+
90
+ if (timeout) {
91
+ clearTimeout(timeout);
92
+ this.session.removeTimeout(workspaceData.workspaceSummary.id);
93
+ }
94
+
95
+ this.addTimersForWorkspacesInFrame(workspaceData.frameSummary.id);
96
+ }
97
+
98
+ private compare(ws1: WorkspaceSnapshotResult, ws2: WorkspaceSnapshotResult): number {
99
+ if (ws1.config.lastActive > ws2.config.lastActive) {
100
+ return 1;
101
+ }
102
+ if (ws1.config.lastActive < ws2.config.lastActive) {
103
+ return -1;
104
+ }
105
+ return 0;
106
+ }
107
+
108
+ private async checkMaximumAmountCore(): Promise<void> {
109
+ const threshold = this.settings?.maximumActiveWorkspaces?.threshold;
110
+
111
+ this.logger?.trace(`Checking for maximum active workspaces rule. The threshold is ${threshold}`);
112
+
113
+ if (typeof threshold !== "number") {
114
+ return;
115
+ }
116
+
117
+ const commandId = generate();
118
+ const result = await this.workspacesController.getAllWorkspacesSummaries({}, commandId);
119
+ const snapshotsPromises = result.summaries.map(s => this.workspacesController.getWorkspaceSnapshot({ itemId: s.id }, commandId));
120
+ const snapshots = await Promise.all(snapshotsPromises);
121
+
122
+ const activeWorkspaces = snapshots.filter((snapshot) => !this.isWorkspaceHibernated(snapshot.config) && !this.isWorkspaceEmpty(snapshot));
123
+ const eligibleForHibernation = activeWorkspaces.filter((snapshot) => this.isSystemHibernationAllowed(snapshot));
124
+
125
+ if (activeWorkspaces.length <= threshold) {
126
+ return;
127
+ }
128
+
129
+ this.logger?.trace(`Found ${eligibleForHibernation.length} eligible for hibernation workspaces`);
130
+
131
+ const hibernationPromises = eligibleForHibernation
132
+ .sort(this.compare)
133
+ .slice(0, activeWorkspaces.length - threshold)
134
+ .map((w) => this.tryHibernateWorkspace(w.id));
135
+
136
+ await Promise.all(hibernationPromises);
137
+ }
138
+
139
+ private async tryHibernateWorkspace(workspaceId: string): Promise<void> {
140
+ try {
141
+ const snapshot = await this.workspacesController.getWorkspaceSnapshot({ itemId: workspaceId }, generate());
142
+
143
+ if (!this.canBeHibernated(snapshot)) {
144
+ return;
145
+ }
146
+
147
+ this.logger?.trace(`trying to hibernate workspace ${workspaceId}`);
148
+
149
+ await this.workspacesController.hibernateWorkspace({ workspaceId }, generate());
150
+
151
+ this.logger?.trace(`workspace ${workspaceId} was hibernated successfully`);
152
+ } catch (error: any) {
153
+ this.logger?.trace(error);
154
+ }
155
+ }
156
+
157
+ private canBeHibernated(snapshot: WorkspaceSnapshotResult): boolean {
158
+ const isWorkspaceHibernated = this.isWorkspaceHibernated(snapshot.config);
159
+ const isWorkspaceSelected = this.isWorkspaceSelected(snapshot.config);
160
+ const isWorkspaceEmpty = this.isWorkspaceEmpty(snapshot);
161
+ const isSystemHibernationAllowed = this.isSystemHibernationAllowed(snapshot);
162
+
163
+ return !isWorkspaceHibernated && !isWorkspaceSelected && !isWorkspaceEmpty && isSystemHibernationAllowed;
164
+ }
165
+
166
+ private isWorkspaceHibernated(workspaceSnapshot: WorkspaceConfigResult): boolean {
167
+ return workspaceSnapshot.isHibernated;
168
+ }
169
+
170
+ private isWorkspaceSelected(workspaceSnapshot: WorkspaceConfigResult): boolean {
171
+ return workspaceSnapshot.isSelected;
172
+ }
173
+
174
+ private isWorkspaceEmpty(workspaceSnapshot: WorkspaceSnapshotResult): boolean {
175
+ return !workspaceSnapshot.children.length;
176
+ }
177
+
178
+ private isSystemHibernationAllowed(workspaceSnapshot: WorkspaceSnapshotResult): boolean {
179
+ const { allowSystemHibernation } = workspaceSnapshot.config;
180
+ // backwards compatibility for older frames that don't have the allowSystemHibernation configuration
181
+ return typeof allowSystemHibernation === "boolean" ? allowSystemHibernation : true;
182
+ }
183
+
184
+ private async getWorkspacesInFrame(frameId: string): Promise<WorkspaceSnapshotResult[]> {
185
+ const result = await this.workspacesController.getAllWorkspacesSummaries({}, generate());
186
+
187
+ const snapshotPromises = result.summaries.reduce((promises, summary) => {
188
+ if (summary.config.frameId === frameId) {
189
+ promises.push(this.workspacesController.getWorkspaceSnapshot({ itemId: summary.id }, generate()));
190
+ }
191
+
192
+ return promises;
193
+ }, [] as Array<Promise<WorkspaceSnapshotResult>>);
194
+
195
+ return await Promise.all(snapshotPromises);
196
+ }
197
+
198
+ private async addTimersForWorkspacesInFrame(frameId: string): Promise<void> {
199
+ if (!this.settings?.idleWorkspaces?.idleMSThreshold) {
200
+ return;
201
+ }
202
+
203
+ const workspacesInFrame = await this.getWorkspacesInFrame(frameId);
204
+
205
+ workspacesInFrame.map((w) => {
206
+
207
+ if (!this.canBeHibernated(w) || this.session.getTimeout(w.id)) {
208
+ return;
209
+ }
210
+
211
+ this.buildTimer(w.id);
212
+
213
+ this.logger?.trace(`Starting workspace idle timer ( ${this.settings?.idleWorkspaces?.idleMSThreshold}ms ) for workspace ${w.id}`);
214
+ });
215
+ }
216
+
217
+ private buildTimer(workspaceId: string): void {
218
+ const timeout = window.setTimeout(() => {
219
+
220
+ if (!this.running) {
221
+ return;
222
+ }
223
+
224
+ this.logger?.trace(`Timer triggered will try to hibernated ${workspaceId}`);
225
+ this.tryHibernateWorkspace(workspaceId);
226
+ this.session.removeTimeout(workspaceId);
227
+ }, this.settings?.idleWorkspaces?.idleMSThreshold);
228
+
229
+ this.session.saveTimeout(workspaceId, timeout);
230
+ }
231
+ }