File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
arduino-ide-extension/src Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -38,3 +38,26 @@ export function uint8ArrayToString(uint8Array: Uint8Array): string {
3838export function stringToUint8Array ( text : string ) : Uint8Array {
3939 return Uint8Array . from ( text , ( char ) => char . charCodeAt ( 0 ) ) ;
4040}
41+
42+ export function poolWhile (
43+ whileCondition : ( ) => boolean ,
44+ intervalMs : number ,
45+ timeoutMs : number
46+ ) : Promise < void > {
47+ return new Promise ( ( resolve , reject ) => {
48+ if ( ! whileCondition ) {
49+ resolve ( ) ;
50+ }
51+
52+ const start = Date . now ( ) ;
53+ const interval = setInterval ( ( ) => {
54+ if ( ! whileCondition ( ) ) {
55+ clearInterval ( interval ) ;
56+ resolve ( ) ;
57+ } else if ( Date . now ( ) - start > timeoutMs ) {
58+ clearInterval ( interval ) ;
59+ reject ( new Error ( 'Timed out while polling.' ) ) ;
60+ }
61+ } , intervalMs ) ;
62+ } ) ;
63+ }
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ import type { AddressInfo } from 'node:net';
3030import { isAbsolute , join , resolve } from 'node:path' ;
3131import type { Argv } from 'yargs' ;
3232import { Sketch } from '../../common/protocol' ;
33+ import { poolWhile } from '../../common/utils' ;
3334import {
3435 AppInfo ,
3536 appInfoPropertyLiterals ,
@@ -292,6 +293,16 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
292293 ) ;
293294 if ( sketchFolderPath ) {
294295 this . openFilePromise . reject ( new InterruptWorkspaceRestoreError ( ) ) ;
296+
297+ // open-file event is triggered before the app is ready and initialWindow is created.
298+ // Wait for initialWindow to be set before opening the sketch on the first instance.
299+ // See https://github.com/arduino/arduino-ide/pull/2693
300+ try {
301+ await app . whenReady ( ) ;
302+ if ( ! this . firstWindowId ) {
303+ await poolWhile ( ( ) => ! this . initialWindow , 100 , 3000 ) ;
304+ }
305+ } catch { }
295306 await this . openSketch ( sketchFolderPath ) ;
296307 }
297308 }
You can’t perform that action at this time.
0 commit comments