@@ -20,9 +20,12 @@ import {
2020 getAngularCoreForHmrReset ,
2121 rememberAngularCoreForHmr ,
2222 resetAngularHmrCompiledComponents ,
23+ setAngularCoreForHmr ,
2324} from './hmr-compiled-components-core' ;
2425import { NativeScriptLoadingService } from './loading.service' ;
2526import { clearAngularHmrRouteConfigCaches } from './legacy/router/hmr-route-cache-core' ;
27+ import { NSLocationStrategy } from './legacy/router/ns-location-strategy' ;
28+ import { NSRouteReuseStrategy } from './legacy/router/ns-route-reuse-strategy' ;
2629import { createAngularRootTransitionGuard } from './root-transition-guard' ;
2730import { APP_ROOT_VIEW , DISABLE_ROOT_VIEW_HANDLING , NATIVESCRIPT_ROOT_MODULE_ID } from './tokens' ;
2831import { NativeScriptDebug } from './trace' ;
@@ -32,6 +35,9 @@ import { NativeScriptDebug } from './trace';
3235// We need to use the original one that has the registered LViews
3336rememberAngularCoreForHmr ( AngularCore as any , globalThis as any ) ;
3437
38+ const angularHmrGlobal = globalThis as any ;
39+ angularHmrGlobal . __NS_REMEMBER_ANGULAR_CORE__ = ( core : any ) => setAngularCoreForHmr ( core , angularHmrGlobal ) ;
40+
3541export interface AppLaunchView extends LayoutBase {
3642 // called when the animation is to begin
3743 startAnimation ?: ( ) => void ;
@@ -234,18 +240,43 @@ export interface ApplicationConfig {
234240}
235241
236242export function runNativeScriptAngularApp < T , K > ( options : AppRunOptions < T , K > ) {
243+ const hmrGlobal = globalThis as any ;
244+
245+ if ( hmrGlobal . __NS_ANGULAR_HMR_REGISTER_ONLY__ && typeof hmrGlobal . __NS_UPDATE_ANGULAR_APP_OPTIONS__ === 'function' ) {
246+ hmrGlobal . __NS_UPDATE_ANGULAR_APP_OPTIONS__ ( options ) ;
247+ return ;
248+ }
249+
250+ let currentOptions = options ;
237251 let mainModuleRef : NgModuleRef < T > | ApplicationRef = null ;
238252 let loadingModuleRef : NgModuleRef < K > | ApplicationRef ;
239253 let platformRef : PlatformRef = null ;
240254 let bootstrapId = - 1 ;
255+
256+ hmrGlobal . __NS_UPDATE_ANGULAR_APP_OPTIONS__ = ( nextOptions : AppRunOptions < T , K > ) => {
257+ currentOptions = nextOptions ;
258+ } ;
259+
241260 const clearAngularHmrRouteCaches = ( ) => {
242261 try {
243262 const injector = ( mainModuleRef as any ) ?. injector ;
263+ const reuseStrategy = injector ?. get ?.( NSRouteReuseStrategy , null ) ;
264+ const locationStrategy = injector ?. get ?.( NSLocationStrategy , null ) ;
244265 const router = injector ?. get ?.( Router , null ) ;
266+ const clearedDetached = reuseStrategy ?. clearAllCaches ?.( ) ?? 0 ;
267+ const clearedLocation = locationStrategy ?. resetForHmr ?.( ) ?? null ;
245268 const cleared = clearAngularHmrRouteConfigCaches ( router ?. config ) ;
246269
247- if ( cleared > 0 ) {
248- console . log ( '[ng-hmr] cleared Angular route caches before reboot:' , cleared ) ;
270+ if (
271+ clearedDetached > 0 ||
272+ cleared > 0 ||
273+ ( clearedLocation && ( clearedLocation . outlets > 0 || clearedLocation . states > 0 || clearedLocation . callbacks > 0 || clearedLocation . hadUrlTree ) )
274+ ) {
275+ console . log ( '[ng-hmr] cleared Angular route caches before reboot:' , {
276+ detachedViews : clearedDetached ,
277+ locationState : clearedLocation ,
278+ routeFields : cleared ,
279+ } ) ;
249280 }
250281 } catch { }
251282 } ;
@@ -299,7 +330,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
299330 if ( NativeScriptDebug . isLogEnabled ( ) ) {
300331 NativeScriptDebug . bootstrapLog ( `Setting RootView to ${ ref } ` ) ;
301332 }
302- if ( options . embedded ) {
333+ if ( currentOptions . embedded ) {
303334 Application . run ( { create : ( ) => ref } ) ;
304335 } else if ( launchEventDone ) {
305336 rootTransitionGuard . runApplicationResetRootView ( Application , ( ) => ref , ref ?. constructor ?. name || 'View' ) ;
@@ -317,11 +348,11 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
317348 'newRoot:' ,
318349 newRoot ?. constructor ?. name ,
319350 ) ;
320- console . log ( '[ng-hmr] setRootView: launchEventDone:' , launchEventDone , 'embedded:' , options . embedded ) ;
351+ console . log ( '[ng-hmr] setRootView: launchEventDone:' , launchEventDone , 'embedded:' , currentOptions . embedded ) ;
321352 if ( NativeScriptDebug . isLogEnabled ( ) ) {
322353 NativeScriptDebug . bootstrapLog ( `Setting RootView to ${ newRoot } ` ) ;
323354 }
324- if ( options . embedded ) {
355+ if ( currentOptions . embedded ) {
325356 console . log ( '[ng-hmr] setRootView: calling Application.run (embedded)' ) ;
326357 Application . run ( { create : ( ) => newRoot } ) ;
327358 } else if ( launchEventDone ) {
@@ -372,7 +403,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
372403 } ;
373404 runSynchronously (
374405 ( ) =>
375- options . appModuleBootstrap ( reason ) . then (
406+ currentOptions . appModuleBootstrap ( reason ) . then (
376407 ( ref ) => {
377408 console . log ( '[ng-hmr] appModuleBootstrap resolved, ref:' , ref ?. constructor ?. name ) ;
378409 console . log ( '[ng-hmr] currentBootstrapId:' , currentBootstrapId , 'bootstrapId:' , bootstrapId ) ;
@@ -482,9 +513,9 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
482513 return ;
483514 }
484515 if ( ! bootstrapped ) {
485- if ( options . loadingModule ) {
516+ if ( currentOptions . loadingModule ) {
486517 runSynchronously ( ( ) =>
487- options . loadingModule ( reason ) . then (
518+ currentOptions . loadingModule ( reason ) . then (
488519 ( loadingRef ) => {
489520 if ( currentBootstrapId !== bootstrapId ) {
490521 // this module is old and not needed anymore
@@ -536,8 +567,8 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
536567 } ,
537568 ) ,
538569 ) ;
539- } else if ( options . launchView ) {
540- let launchView = options . launchView ( reason ) ;
570+ } else if ( currentOptions . launchView ) {
571+ let launchView = currentOptions . launchView ( reason ) ;
541572 setRootView ( launchView ) ;
542573 if ( launchView . startAnimation ) {
543574 setTimeout ( ( ) => {
@@ -606,7 +637,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
606637 global . NativeScriptGlobals . events . addEventListener =
607638 global . NativeScriptGlobals . events [ Zone . __symbol__ ( 'addEventListener' ) ] ;
608639 }
609- if ( ! options . embedded ) {
640+ if ( ! currentOptions . embedded ) {
610641 Application . on ( Application . launchEvent , launchCallback ) ;
611642 }
612643 Application . on ( Application . exitEvent , exitCallback ) ;
@@ -681,7 +712,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
681712 return ;
682713 }
683714
684- if ( options . embedded ) {
715+ if ( currentOptions . embedded ) {
685716 bootstrapRoot ( 'applaunch' ) ;
686717 } else {
687718 Application . run ( ) ;
0 commit comments