@@ -28,6 +28,7 @@ import {
2828 DECLARATION_COMPONENT_VIEW ,
2929 HEADER_OFFSET ,
3030 HYDRATION ,
31+ INJECTOR ,
3132 LView ,
3233 TVIEW ,
3334 TView ,
@@ -48,6 +49,8 @@ import {
4849 removeLViewFromLContainer ,
4950} from '../view/container' ;
5051import { declareNoDirectiveHostTemplate } from './template' ;
52+ import { removeFromAnimationQueue } from '../../animation/queue' ;
53+ import { allLeavingAnimations } from '../../animation/longest_animation' ;
5154
5255/**
5356 * Creates an LContainer for an ng-template representing a root node
@@ -419,10 +422,11 @@ class LiveCollectionLContainerImpl extends LiveCollection<
419422 index ,
420423 shouldAddViewToDom ( this . templateTNode , dehydratedView ) ,
421424 ) ;
425+ clearDetachAnimationList ( this . lContainer , index ) ;
422426 }
423- override detach ( index : number , skipLeaveAnimations ?: boolean ) : LView < RepeaterContext < unknown > > {
427+ override detach ( index : number ) : LView < RepeaterContext < unknown > > {
424428 this . needsIndexUpdate ||= index !== this . length - 1 ;
425- if ( skipLeaveAnimations ) setSkipLeaveAnimations ( this . lContainer , index ) ;
429+ maybeInitDetachAnimationList ( this . lContainer , index ) ;
426430 return detachExistingView < RepeaterContext < unknown > > ( this . lContainer , index ) ;
427431 }
428432 override create ( index : number , value : unknown ) : LView < RepeaterContext < unknown > > {
@@ -570,13 +574,39 @@ function getLContainer(lView: LView, index: number): LContainer {
570574 return lContainer ;
571575}
572576
573- function setSkipLeaveAnimations ( lContainer : LContainer , index : number ) : void {
577+ function clearDetachAnimationList ( lContainer : LContainer , index : number ) : void {
574578 if ( lContainer . length <= CONTAINER_HEADER_OFFSET ) return ;
575579
576580 const indexInContainer = CONTAINER_HEADER_OFFSET + index ;
577581 const viewToDetach = lContainer [ indexInContainer ] ;
578- if ( viewToDetach && viewToDetach [ ANIMATIONS ] ) {
579- ( viewToDetach [ ANIMATIONS ] as AnimationLViewData ) . skipLeaveAnimations = true ;
582+ const animations = viewToDetach
583+ ? ( viewToDetach [ ANIMATIONS ] as AnimationLViewData | undefined )
584+ : undefined ;
585+ if (
586+ viewToDetach &&
587+ animations &&
588+ animations . detachedLeaveAnimationFns &&
589+ animations . detachedLeaveAnimationFns . length > 0
590+ ) {
591+ const injector = viewToDetach [ INJECTOR ] ;
592+ removeFromAnimationQueue ( injector , animations ) ;
593+ allLeavingAnimations . delete ( viewToDetach ) ;
594+ animations . detachedLeaveAnimationFns = undefined ;
595+ }
596+ }
597+
598+ // Initialize the detach leave animation list for a view about to be detached, but only
599+ // if it has leave animations.
600+ function maybeInitDetachAnimationList ( lContainer : LContainer , index : number ) : void {
601+ if ( lContainer . length <= CONTAINER_HEADER_OFFSET ) return ;
602+
603+ const indexInContainer = CONTAINER_HEADER_OFFSET + index ;
604+ const viewToDetach = lContainer [ indexInContainer ] ;
605+ const animations = viewToDetach
606+ ? ( viewToDetach [ ANIMATIONS ] as AnimationLViewData | undefined )
607+ : undefined ;
608+ if ( animations && animations . leave && animations . leave . size > 0 ) {
609+ animations . detachedLeaveAnimationFns = [ ] ;
580610 }
581611}
582612
0 commit comments