@@ -29,6 +29,17 @@ function isComponentFactoryResolver(item: any): item is ComponentFactoryResolver
29
29
return ! ! item . resolveComponentFactory ;
30
30
}
31
31
32
+ function callableOnce < T > ( fn : ( ...args : T [ ] ) => void ) {
33
+ let called = false ;
34
+ return ( ...args : T [ ] ) => {
35
+ if ( called ) {
36
+ return ;
37
+ }
38
+ called = true ;
39
+ return fn ( ...args ) ;
40
+ } ;
41
+ }
42
+
32
43
export class DestructibleInjector implements Injector {
33
44
private refs = new Set < any > ( ) ;
34
45
constructor ( private destructibleProviders : ProviderSet , private parent : Injector ) { }
@@ -71,6 +82,10 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
71
82
private isEmptyOutlet : boolean ;
72
83
private viewUtil : ViewUtil ;
73
84
private frame : Frame ;
85
+ // this function is used to clear the outlet cache (clear history)
86
+ // usually it's cleared in `navigatedTo`, but on quick navigation, the event will be fired after angular already added more things to the cache
87
+ // so now we call this if the component is detached or deactivated (meaning it's mid-navigation, before cache manipulation)
88
+ private postNavFunction : ( ) => void ;
74
89
75
90
attachEvents : EventEmitter < unknown > = new EventEmitter ( ) ;
76
91
detachEvents : EventEmitter < unknown > = new EventEmitter ( ) ;
@@ -209,6 +224,7 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
209
224
if ( ! this . isActivated ) {
210
225
return ;
211
226
}
227
+ this . postNavFunction ?.( ) ;
212
228
213
229
const c = this . activated . instance ;
214
230
destroyComponentRef ( this . activated ) ;
@@ -234,6 +250,8 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
234
250
NativeScriptDebug . routerLog ( `PageRouterOutlet.detach() - ${ routeToString ( this . _activatedRoute ) } ` ) ;
235
251
}
236
252
253
+ this . postNavFunction ?.( ) ;
254
+
237
255
// Detach from ChangeDetection
238
256
this . activated . hostView . detach ( ) ;
239
257
@@ -413,28 +431,43 @@ export class PageRouterOutlet implements OnDestroy, RouterOutletContract {
413
431
this . locationStrategy . _beginPageNavigation ( this . frame , navOptions ) ;
414
432
const isReplace = navOptions . replaceUrl && ! navOptions . clearHistory ;
415
433
434
+ const currentRoute = this . activatedRoute ;
416
435
// Clear refCache if navigation with clearHistory
417
436
if ( navOptions . clearHistory ) {
437
+ const wipeCache = callableOnce ( ( ) => {
438
+ if ( this . postNavFunction === wipeCache ) {
439
+ this . postNavFunction = null ;
440
+ }
441
+ if ( this . outlet && this . activatedRoute === currentRoute ) {
442
+ // potential alternative fix (only fix children of the current outlet)
443
+ // const nests = outletKey.split('/');
444
+ // this.outlet.outletKeys.filter((k) => k.split('/').length >= nests.length).forEach((key) => this.routeReuseStrategy.clearCache(key));
445
+ this . outlet . outletKeys . forEach ( ( key ) => this . routeReuseStrategy . clearCache ( key ) ) ;
446
+ }
447
+ } ) ;
448
+ this . postNavFunction = wipeCache ;
418
449
const clearCallback = ( ) =>
419
450
setTimeout ( ( ) => {
420
- if ( this . outlet ) {
421
- // potential alternative fix (only fix children of the current outlet)
422
- // const nests = outletKey.split('/');
423
- // this.outlet.outletKeys.filter((k) => k.split('/').length >= nests.length).forEach((key) => this.routeReuseStrategy.clearCache(key));
424
- this . outlet . outletKeys . forEach ( ( key ) => this . routeReuseStrategy . clearCache ( key ) ) ;
425
- }
451
+ wipeCache ( ) ;
426
452
} ) ;
427
453
428
454
page . once ( Page . navigatedToEvent , clearCallback ) ;
429
455
} else if ( navOptions . replaceUrl ) {
456
+ const popCache = callableOnce ( ( ) => {
457
+ if ( this . postNavFunction === popCache ) {
458
+ this . postNavFunction = null ;
459
+ }
460
+ if ( this . outlet && this . activatedRoute === currentRoute ) {
461
+ // potential alternative fix (only fix children of the current outlet)
462
+ // const nests = outletKey.split('/');
463
+ // this.outlet.outletKeys.filter((k) => k.split('/').length >= nests.length).forEach((key) => this.routeReuseStrategy.popCache(key));
464
+ this . outlet . outletKeys . forEach ( ( key ) => this . routeReuseStrategy . popCache ( key ) ) ;
465
+ }
466
+ } ) ;
467
+ this . postNavFunction = popCache ;
430
468
const clearCallback = ( ) =>
431
469
setTimeout ( ( ) => {
432
- if ( this . outlet ) {
433
- // potential alternative fix (only fix children of the current outlet)
434
- // const nests = outletKey.split('/');
435
- // this.outlet.outletKeys.filter((k) => k.split('/').length >= nests.length).forEach((key) => this.routeReuseStrategy.popCache(key));
436
- this . outlet . outletKeys . forEach ( ( key ) => this . routeReuseStrategy . popCache ( key ) ) ;
437
- }
470
+ popCache ( ) ;
438
471
} ) ;
439
472
440
473
page . once ( Page . navigatedToEvent , clearCallback ) ;
0 commit comments