Skip to content

Commit

Permalink
Fix long webview delay (3 min) after the first fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva committed Dec 22, 2023
1 parent 7052119 commit 31aca93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/main/webview/event/eventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import { RunUtils } from '../../utils/runUtils';
*/
export class EventManager {
protected send: EventSender;
private webviewLoaded: boolean = false;
private static TIMEOUT_THREE_MINUTES_IN_MS: number = 3 * 60 * 1000;
private static RETRY_DELAY_TEN_MS: number = 3 * 60 * 1000;
private webviewAPILoaded: boolean = false;
// 3 minutes
private static TIMEOUT_MILLISECOND: number = 3 * 60 * 1000;
private static RETRY_DELAY_MILLISECOND: number = 10;

private constructor(webview: vscode.Webview, private connectionManager: ConnectionManager, private logManager: LogManager) {
this.setEventReceiver(webview);
Expand All @@ -31,16 +32,23 @@ export class EventManager {
return eventManager;
}

/**
* Waits until the webview is loaded by sending requests at intervals until loaded or until timed out.
* @param eventManager The EventManager instance responsible for handling events.
*/
private static async waitUntilWebviewLoaded(eventManager: EventManager) {
const startedTime: number = Date.now();
for (let i: number = 1; !EventManager.timedOut(startedTime) && !eventManager.webviewLoaded; i++) {
eventManager.send.setEventEmitter();
await RunUtils.delay(EventManager.RETRY_DELAY_TEN_MS * i);
for (let i: number = 1; !EventManager.timedOut(startedTime); i++) {
eventManager.send.loadWebviewAPI();
if (eventManager.webviewAPILoaded) {
return;
}
await RunUtils.delay(EventManager.RETRY_DELAY_MILLISECOND * i);
}
}

private static timedOut(startedTime: number) {
return Date.now() - startedTime > EventManager.TIMEOUT_THREE_MINUTES_IN_MS;
return Date.now() - startedTime > EventManager.TIMEOUT_MILLISECOND;
}

/**
Expand All @@ -66,7 +74,7 @@ export class EventManager {
await new LoginTask(this.send, message.data, this.connectionManager, this.logManager).run();
break;
case WebviewEventType.WebviewLoaded:
this.webviewLoaded = true;
this.webviewAPILoaded = true;
break;
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/main/webview/event/eventSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { LogManager } from '../../log/logManager';
*/
export class EventSender {
constructor(private webview: vscode.Webview, private logManager: LogManager) {
this.setEventEmitter();
this.loadWebviewAPI();
}

/**
* Establishes the event emitter within the webview to enable sending messages from the webview back to the IDE.
*/
public async setEventEmitter(): Promise<void> {
public async loadWebviewAPI(): Promise<void> {
await this.sendEvent(this.webview, {
type: IdeEventType.SetEmitter,
data: 'return acquireVsCodeApi().postMessage'
Expand Down

0 comments on commit 31aca93

Please sign in to comment.