@@ -23,6 +23,7 @@ import { Observable, Subject } from 'rxjs';
2323import { filter , map , take } from 'rxjs/operators' ;
2424import { AppHostView } from './app-host-view' ;
2525import { NativeScriptLoadingService } from './loading.service' ;
26+ import { HmrRebootResult , HmrRebootState , HmrRun } from './hmr-reboot-state' ;
2627import { HmrSnapshot , restoreHmrSnapshots , saveHmrSnapshots } from './hmr-snapshots' ;
2728import {
2829 APP_ROOT_VIEW ,
@@ -240,27 +241,9 @@ interface ViteHotContext {
240241
241242type MainRef = NgModuleRef < unknown > | ApplicationRef ;
242243
243- interface HmrRun {
244- mainRef ( ) : MainRef | null ;
245- dispose ( ) : void ;
246- }
247-
248- interface PendingHmrReboot {
249- bootstrapStarted : boolean ;
250- bootstrapped : Promise < MainRef | null > ;
251- resolveBootstrapped ( ref : MainRef | null ) : void ;
252- settled ?: Promise < void > ;
253- }
254-
255244// A zone that never settles (e.g. an app-level setInterval) must not stall the dev client's update queue.
256245const HMR_STABLE_TIMEOUT_MS = 5000 ;
257246
258- // This module is never re-evaluated in a dev session, but the app entry that
259- // calls runNativeScriptAngularApp is, so this state outlives each app run.
260- let currentHmrRun : HmrRun | null = null ;
261- let pendingHmrReboot : PendingHmrReboot | null = null ;
262- let hmrSnapshots = new Map < string , HmrSnapshot > ( ) ;
263-
264247function logHmr ( message : string , error ?: unknown ) {
265248 if ( error === undefined ) {
266249 console . warn ( `[@nativescript/angular] ${ message } ` ) ;
@@ -278,64 +261,7 @@ function readHmrParticipants(ref: MainRef): readonly NativeScriptHmrParticipant[
278261 }
279262}
280263
281- /**
282- * Dev only, called by the dev server's HMR client before it re-imports the app
283- * entry: saves the HMR participants' state and tears the running app down so
284- * the re-imported entry can bootstrap a new one. A no-op when nothing is running.
285- */
286- export async function prepareHmrReboot ( ) : Promise < void > {
287- const run = currentHmrRun ;
288- if ( ! run ) {
289- return ;
290- }
291- currentHmrRun = null ;
292- // Snapshots not yet restored (the previous reboot is unsettled or failed) are newer than anything this run holds.
293- const ref = run . mainRef ( ) ;
294- if ( ref && ! pendingHmrReboot ) {
295- hmrSnapshots = saveHmrSnapshots ( readHmrParticipants ( ref ) , logHmr ) ;
296- }
297- pendingHmrReboot ?. resolveBootstrapped ( null ) ;
298- let resolveBootstrapped : ( ref : MainRef | null ) => void ;
299- const bootstrapped = new Promise < MainRef | null > ( ( resolve ) => ( resolveBootstrapped = resolve ) ) ;
300- pendingHmrReboot = { bootstrapStarted : false , bootstrapped, resolveBootstrapped } ;
301- run . dispose ( ) ;
302- }
303-
304- /**
305- * Dev only: resolves once the app bootstrapped after {@link prepareHmrReboot}
306- * is stable and its HMR participants are restored. Resolves immediately when no
307- * reboot is pending, and also when the bootstrap fails (the error is logged and
308- * shown in the app).
309- */
310- export function whenHmrRebootSettled ( ) : Promise < void > {
311- const reboot = pendingHmrReboot ;
312- if ( ! reboot ) {
313- return Promise . resolve ( ) ;
314- }
315- return ( reboot . settled ??= settleHmrReboot ( reboot ) ) ;
316- }
317-
318- async function settleHmrReboot ( reboot : PendingHmrReboot ) : Promise < void > {
319- try {
320- await restoreAfterHmrReboot ( reboot ) ;
321- } catch ( error ) {
322- logHmr ( 'Could not restore HMR state after a reboot.' , error ) ;
323- }
324- // Unrestored snapshots are kept on purpose: the next reboot will likely carry the fix.
325- if ( pendingHmrReboot === reboot ) {
326- pendingHmrReboot = null ;
327- }
328- }
329-
330- async function restoreAfterHmrReboot ( reboot : PendingHmrReboot ) : Promise < void > {
331- if ( ! reboot . bootstrapStarted ) {
332- logHmr ( 'The app entry did not bootstrap Angular after an HMR reboot; HMR state was not restored.' ) ;
333- return ;
334- }
335- const ref = await reboot . bootstrapped ;
336- if ( ! ref || pendingHmrReboot !== reboot ) {
337- return ;
338- }
264+ async function whenHmrAppStable ( ref : MainRef ) : Promise < boolean > {
339265 const appRef = ref instanceof ApplicationRef ? ref : ref . injector . get ( ApplicationRef ) ;
340266 const zone = ref . injector . get ( NgZone ) ;
341267 let timer : ReturnType < typeof setTimeout > ;
@@ -350,15 +276,41 @@ async function restoreAfterHmrReboot(reboot: PendingHmrReboot): Promise<void> {
350276 if ( ! stable ) {
351277 logHmr ( `The app was not stable ${ HMR_STABLE_TIMEOUT_MS } ms after an HMR reboot; restoring HMR state anyway.` ) ;
352278 }
353- if ( pendingHmrReboot !== reboot || appRef . destroyed ) {
354- return ;
355- }
356- if ( hmrSnapshots . size ) {
357- await restoreHmrSnapshots ( readHmrParticipants ( ref ) , hmrSnapshots , ( fn ) => zone . run ( fn ) , logHmr ) ;
358- }
359- if ( pendingHmrReboot === reboot ) {
360- hmrSnapshots = new Map ( ) ;
361- }
279+ return ! appRef . destroyed ;
280+ }
281+
282+ // This module is never re-evaluated in a dev session, but the app entry that
283+ // calls runNativeScriptAngularApp is, so this state outlives each app run.
284+ const hmrReboots = new HmrRebootState < MainRef , Map < string , HmrSnapshot > > ( {
285+ save : ( ref ) => saveHmrSnapshots ( readHmrParticipants ( ref ) , logHmr ) ,
286+ whenStable : whenHmrAppStable ,
287+ restore : async ( ref , snapshots ) => {
288+ if ( snapshots . size ) {
289+ const zone = ref . injector . get ( NgZone ) ;
290+ await restoreHmrSnapshots ( readHmrParticipants ( ref ) , snapshots , ( fn ) => zone . run ( fn ) , logHmr ) ;
291+ }
292+ } ,
293+ log : logHmr ,
294+ } ) ;
295+
296+ /**
297+ * Dev only, called by the dev server's HMR client before it re-imports the app
298+ * entry: saves the HMR participants' state and tears the running app down so
299+ * the re-imported entry can bootstrap a new one. A no-op when nothing is running.
300+ * The saved state is kept until an app bootstraps and restores it, across any
301+ * number of failed reboots.
302+ */
303+ export async function prepareHmrReboot ( ) : Promise < void > {
304+ hmrReboots . prepare ( ) ;
305+ }
306+
307+ /**
308+ * Dev only: resolves once the app bootstrapped after {@link prepareHmrReboot}
309+ * is stable and its HMR participants are restored, or to `{ error }` when that
310+ * bootstrap failed. Resolves immediately when no reboot is pending. Never rejects.
311+ */
312+ export function whenHmrRebootSettled ( ) : Promise < HmrRebootResult > {
313+ return hmrReboots . settled ( ) ;
362314}
363315
364316/**
@@ -436,13 +388,8 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
436388 errorTextBox . color = new Color ( 'red' ) ;
437389 setRootView ( errorTextBox ) ;
438390 } ;
391+ let hmrRun : HmrRun < MainRef > | null = null ;
439392 const bootstrapRoot = ( reason : NgModuleReason ) => {
440- const hmrReboot = pendingHmrReboot ;
441- if ( hmrReboot && ! hmrReboot . bootstrapStarted ) {
442- hmrReboot . bootstrapStarted = true ;
443- // Started here rather than on demand so the restore runs even if the dev client never awaits it.
444- hmrReboot . settled ??= settleHmrReboot ( hmrReboot ) ;
445- }
446393 try {
447394 bootstrapId = Date . now ( ) ;
448395 const currentBootstrapId = bootstrapId ;
@@ -461,7 +408,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
461408 return ;
462409 }
463410 mainModuleRef = ref ;
464- hmrReboot ?. resolveBootstrapped ( ref ) ;
411+ hmrRun ?. bootstrapped ( ref ) ;
465412
466413 ( ref instanceof ApplicationRef ? ref . components [ 0 ] : ref ) . onDestroy (
467414 ( ) => ( mainModuleRef = mainModuleRef === ref ? null : mainModuleRef ) ,
@@ -480,9 +427,8 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
480427 bootstrapped = true ;
481428 NativeScriptDebug . bootstrapLogError ( `Error bootstrapping app module:\n${ err . message } \n\n${ err . stack } ` ) ;
482429 showErrorUI ( err ) ;
483- if ( hmrReboot && currentBootstrapId === bootstrapId ) {
484- logHmr ( 'The app failed to bootstrap after an HMR reboot.' , err ) ;
485- hmrReboot . resolveBootstrapped ( null ) ;
430+ if ( currentBootstrapId === bootstrapId ) {
431+ hmrRun ?. failed ( err ) ;
486432 }
487433 throw err ;
488434 } ,
@@ -582,10 +528,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
582528 ) ;
583529 } catch ( err ) {
584530 NativeScriptDebug . bootstrapLogError ( `Error in Bootstrap Function:\n${ err . message } \n\n${ err . stack } ` ) ;
585- if ( hmrReboot ) {
586- logHmr ( 'The app failed to bootstrap after an HMR reboot.' , err ) ;
587- hmrReboot . resolveBootstrapped ( null ) ;
588- }
531+ hmrRun ?. failed ( err ) ;
589532 }
590533 } ;
591534 const disposePlatform = ( reason : NgModuleReason ) => {
@@ -625,8 +568,8 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
625568 }
626569 // Must stay the literal `import.meta.hot`: Vite only injects the hot context for that form.
627570 if ( ( import . meta as ImportMeta & { hot ?: ViteHotContext } ) . hot ) {
628- currentHmrRun = {
629- mainRef : ( ) => mainModuleRef ,
571+ hmrRun = hmrReboots . attach ( {
572+ ref : ( ) => mainModuleRef ,
630573 dispose : ( ) => {
631574 Application . off ( Application . launchEvent , launchCallback ) ;
632575 Application . off ( Application . exitEvent , exitCallback ) ;
@@ -636,7 +579,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
636579 // this, dev mode warns NG0912 because cached component IDs map to the old classes.
637580 ɵresetCompiledComponents ( ) ;
638581 } ,
639- } ;
582+ } ) ;
640583 }
641584 if ( import . meta[ 'webpackHot' ] ) {
642585 // handle HMR Application.run
@@ -674,7 +617,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
674617 // A host that already launched the app (e.g. a dev server's boot UI) will not
675618 // fire launchEvent again, so bootstrap now instead of waiting for it.
676619 if ( options . embedded || Application . hasLaunched ( ) ) {
677- bootstrapRoot ( pendingHmrReboot ? 'hotreload' : 'applaunch' ) ;
620+ bootstrapRoot ( hmrRun ?. rebooted ? 'hotreload' : 'applaunch' ) ;
678621 } else {
679622 Application . run ( ) ;
680623 }
0 commit comments