Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait for theia initalWindow to be set before opening sketch through open-file event #2693

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions arduino-ide-extension/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ export function uint8ArrayToString(uint8Array: Uint8Array): string {
export function stringToUint8Array(text: string): Uint8Array {
return Uint8Array.from(text, (char) => char.charCodeAt(0));
}

export function poolWhile(
whileCondition: () => boolean,
intervalMs: number,
timeoutMs: number
): Promise<void> {
return new Promise((resolve, reject) => {
if (!whileCondition) {
resolve();
}

const start = Date.now();
const interval = setInterval(() => {
if (!whileCondition()) {
clearInterval(interval);
resolve();
} else if (Date.now() - start > timeoutMs) {
clearInterval(interval);
reject(new Error('Timed out while polling.'));
}
}, intervalMs);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type { AddressInfo } from 'node:net';
import { isAbsolute, join, resolve } from 'node:path';
import type { Argv } from 'yargs';
import { Sketch } from '../../common/protocol';
import { poolWhile } from '../../common/utils';
import {
AppInfo,
appInfoPropertyLiterals,
Expand Down Expand Up @@ -292,6 +293,16 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
);
if (sketchFolderPath) {
this.openFilePromise.reject(new InterruptWorkspaceRestoreError());

// open-file event is triggered before the app is ready and initialWindow is created.
// Wait for initialWindow to be set before opening the sketch on the first instance.
// See https://github.com/arduino/arduino-ide/pull/2693
try {
await app.whenReady();
if (!this.firstWindowId) {
await poolWhile(() => !this.initialWindow, 100, 3000);
}
} catch {}
await this.openSketch(sketchFolderPath);
}
}
Expand Down Expand Up @@ -890,7 +901,10 @@ const fallbackFrontendAppConfig: FrontendApplicationConfig = {
defaultIconTheme: 'none',
validatePreferencesSchema: false,
defaultLocale: '',
electron: { showWindowEarly: true, uriScheme: 'custom://arduino-ide' },
electron: {
showWindowEarly: true,
uriScheme: 'arduino-ide',
},
reloadOnReconnect: true,
};

Expand Down
3 changes: 2 additions & 1 deletion electron-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"defaultIconTheme": "none",
"validatePreferencesSchema": false,
"electron": {
"showWindowEarly": true
"showWindowEarly": true,
"uriScheme": "arduino-ide"
},
"reloadOnReconnect": true,
"preferences": {
Expand Down
Loading