@@ -31,9 +31,13 @@ import { injector } from "../common/yok";
3131import { isIOSMacDevice } from "../common/mobile/ios/mac/ios-mac-constants" ;
3232import { inject } from "../common/di" ;
3333
34+ const TUNNEL_REOPEN_INTERVAL_MS = 60_000 ;
35+
3436export class RunController extends EventEmitter implements IRunController {
3537 private devServerOriginService = inject ( DevServerOriginService ) ;
3638 private iOSDeviceTunnelService = inject ( IOSDeviceTunnelService ) ;
39+ private pendingSyncAfterInstall = new Set < string > ( ) ;
40+ private lastTunnelReopen : IDictionary < number > = { } ;
3741 private prepareReadyEventHandler : any = null ;
3842 private appRestartRequestHandler : (
3943 request : IBundlerAppRestartRequest ,
@@ -143,14 +147,15 @@ export class RunController extends EventEmitter implements IRunController {
143147 this . appRestartRequestHandler ,
144148 ) ;
145149 }
150+ // A tunnel can drop during the initial sync.
151+ this . attachTunnelLostHandler ( ) ;
146152 await this . syncInitialDataOnDevices (
147153 projectData ,
148154 liveSyncInfo ,
149155 deviceDescriptorsForInitialSync ,
150156 ) ;
151157
152158 this . attachDeviceLostHandler ( ) ;
153- this . attachTunnelLostHandler ( ) ;
154159 }
155160
156161 public async stop ( data : IStopRunData ) : Promise < void > {
@@ -175,6 +180,8 @@ export class RunController extends EventEmitter implements IRunController {
175180 ) . map ( ( descriptor ) => descriptor . identifier ) ;
176181
177182 for ( const deviceIdentifier of removedDeviceIdentifiers ) {
183+ this . pendingSyncAfterInstall . delete ( deviceIdentifier ) ;
184+ delete this . lastTunnelReopen [ deviceIdentifier ] ;
178185 this . devServerOriginService . forget ( deviceIdentifier ) ;
179186 await this . iOSDeviceTunnelService . close ( deviceIdentifier ) ;
180187 }
@@ -680,6 +687,7 @@ export class RunController extends EventEmitter implements IRunController {
680687 }
681688 const device =
682689 this . $devicesService . getDeviceByIdentifier ( deviceIdentifier ) ;
690+ await this . reopenUsbTunnel ( device ) ;
683691 const origin = await this . devServerOriginService . chooseOrigin ( device ) ;
684692 if ( origin === previous ) {
685693 return false ;
@@ -716,6 +724,48 @@ export class RunController extends EventEmitter implements IRunController {
716724 }
717725 }
718726
727+ /**
728+ * A device still on USB gets a new tunnel, so it doesn't fall back to the
729+ * LAN. At most once a minute per device: a holder that keeps exiting
730+ * would otherwise reopen and lose the tunnel in a loop.
731+ */
732+ private async reopenUsbTunnel ( device : Mobile . IDevice ) : Promise < void > {
733+ if ( ! this . shouldUseUsbTunnel ( device ) ) {
734+ return ;
735+ }
736+ const id = device . deviceInfo . identifier ;
737+ const now = Date . now ( ) ;
738+ if ( now - ( this . lastTunnelReopen [ id ] ?? 0 ) < TUNNEL_REOPEN_INTERVAL_MS ) {
739+ return ;
740+ }
741+ this . lastTunnelReopen [ id ] = now ;
742+ await this . iOSDeviceTunnelService . open ( device ) ;
743+ }
744+
745+ private shouldUseUsbTunnel ( device : Mobile . IDevice ) : boolean {
746+ return (
747+ ! this . $mobileHelper . isAndroidPlatform ( device . deviceInfo . platform ) &&
748+ ! device . isEmulator &&
749+ ! isTruthyEnvFlag ( process . env . NS_HMR_PREFER_LAN_HOST ) &&
750+ ! process . env . NS_HMR_HOST ?. trim ( )
751+ ) ;
752+ }
753+
754+ /**
755+ * Delivers `dev-server.json` after an install. A device whose delivery
756+ * failed gets it on its next sync instead of the fast path.
757+ */
758+ private async syncAfterInstall (
759+ platformLiveSyncService : IPlatformLiveSyncService ,
760+ device : Mobile . IDevice ,
761+ watchInfo : ILiveSyncWatchInfo ,
762+ ) : Promise < void > {
763+ const id = device . deviceInfo . identifier ;
764+ this . pendingSyncAfterInstall . add ( id ) ;
765+ await platformLiveSyncService . syncAfterInstall ( device , watchInfo ) ;
766+ this . pendingSyncAfterInstall . delete ( id ) ;
767+ }
768+
719769 private async syncInitialDataOnDevices (
720770 projectData : IProjectData ,
721771 liveSyncInfo : ILiveSyncInfo ,
@@ -943,7 +993,11 @@ export class RunController extends EventEmitter implements IRunController {
943993 let adbReverse = false ;
944994 let port : number ;
945995 if ( this . $mobileHelper . isAndroidPlatform ( platform ) ) {
946- if ( ! isTruthyEnvFlag ( process . env . NS_HMR_NO_ADB_REVERSE ) && ! preferLan ) {
996+ if (
997+ ! isTruthyEnvFlag ( process . env . NS_HMR_NO_ADB_REVERSE ) &&
998+ ! preferLan &&
999+ ! process . env . NS_HMR_HOST ?. trim ( )
1000+ ) {
9471001 port = await this . $viteHmrPortService . getPort ( platform ) ;
9481002 try {
9491003 adbReverse = await this . ensureAndroidReverse (
@@ -957,11 +1011,7 @@ export class RunController extends EventEmitter implements IRunController {
9571011 ) ;
9581012 }
9591013 }
960- } else if (
961- ! device . isEmulator &&
962- ! preferLan &&
963- ! process . env . NS_HMR_HOST ?. trim ( )
964- ) {
1014+ } else if ( this . shouldUseUsbTunnel ( device ) ) {
9651015 await this . iOSDeviceTunnelService . open ( device ) ;
9661016 }
9671017
@@ -1220,7 +1270,11 @@ export class RunController extends EventEmitter implements IRunController {
12201270 rebuiltInformation [ platformData . platformNameLowerCase ]
12211271 . packageFilePath ,
12221272 ) ;
1223- await platformLiveSyncService . syncAfterInstall ( device , watchInfo ) ;
1273+ await this . syncAfterInstall (
1274+ platformLiveSyncService ,
1275+ device ,
1276+ watchInfo ,
1277+ ) ;
12241278 await this . refreshApplication (
12251279 projectData ,
12261280 {
@@ -1320,6 +1374,14 @@ export class RunController extends EventEmitter implements IRunController {
13201374 ) ;
13211375 } ;
13221376
1377+ if ( this . pendingSyncAfterInstall . has ( device . deviceInfo . identifier ) ) {
1378+ await this . syncAfterInstall (
1379+ platformLiveSyncService ,
1380+ device ,
1381+ watchInfo ,
1382+ ) ;
1383+ }
1384+
13231385 if ( liveSyncInfo . useHotModuleReload ) {
13241386 try {
13251387 this . $logger . trace (
@@ -1346,7 +1408,11 @@ export class RunController extends EventEmitter implements IRunController {
13461408 reinstalled &&
13471409 this . usesDevServerFile ( device , projectData , liveSyncInfo )
13481410 ) {
1349- await platformLiveSyncService . syncAfterInstall ( device , watchInfo ) ;
1411+ await this . syncAfterInstall (
1412+ platformLiveSyncService ,
1413+ device ,
1414+ watchInfo ,
1415+ ) ;
13501416 }
13511417 watchInfo . connectTimeout = null ;
13521418 await watchAction ( ) ;
0 commit comments