@@ -5,10 +5,12 @@ import {
55 Input ,
66 NgZone ,
77 OnChanges ,
8+ OnDestroy ,
89 SimpleChanges ,
910 TemplateRef ,
1011 ViewContainerRef ,
1112} from '@angular/core' ;
13+ import { FComponentsStore } from '../f-storage' ;
1214
1315interface FVirtualContext < T > {
1416 $implicit : T ;
@@ -19,15 +21,18 @@ interface FVirtualContext<T> {
1921 selector : '[fVirtualFor][fVirtualForOf]' ,
2022 standalone : true ,
2123} )
22- export class FVirtualFor < T > implements OnChanges {
24+ export class FVirtualFor < T > implements OnChanges , OnDestroy {
2325 @Input ( ) fVirtualForOf : readonly T [ ] = [ ] ;
2426 @Input ( ) fVirtualForTrackBy ?: ( index : number , item : T ) => unknown ;
2527
2628 private readonly _vc = inject ( ViewContainerRef ) ;
2729 private readonly _tpl = inject < TemplateRef < FVirtualContext < T > > > ( TemplateRef ) ;
2830 private readonly _zone = inject ( NgZone ) ;
31+ private readonly _componentsStore = inject ( FComponentsStore , { optional : true } ) ;
2932
3033 private _views : EmbeddedViewRef < FVirtualContext < T > > [ ] = [ ] ;
34+ private _rafId : number | null = null ;
35+ private _isProgressiveRenderActive = false ;
3136
3237 ngOnChanges ( changes : SimpleChanges ) : void {
3338 if ( changes [ 'fVirtualForOf' ] ) {
@@ -36,14 +41,25 @@ export class FVirtualFor<T> implements OnChanges {
3641 }
3742 }
3843
44+ ngOnDestroy ( ) : void {
45+ this . _reset ( ) ;
46+ }
47+
3948 private _reset ( ) : void {
49+ if ( this . _rafId !== null ) {
50+ cancelAnimationFrame ( this . _rafId ) ;
51+ this . _rafId = null ;
52+ }
53+
54+ this . _finishProgressiveRender ( ) ;
4055 this . _vc . clear ( ) ;
4156 this . _views . length = 0 ;
4257 }
4358
4459 private _renderProgressively ( ) : void {
4560 const FRAME_BUDGET = 10 ; // ms
4661 let index = 0 ;
62+ this . _startProgressiveRender ( ) ;
4763
4864 this . _zone . runOutsideAngular ( ( ) => {
4965 const pump = ( ) => {
@@ -62,11 +78,34 @@ export class FVirtualFor<T> implements OnChanges {
6278 }
6379
6480 if ( index < this . fVirtualForOf . length ) {
65- requestAnimationFrame ( pump ) ;
81+ this . _rafId = requestAnimationFrame ( pump ) ;
82+
83+ return ;
6684 }
85+
86+ this . _rafId = null ;
87+ this . _finishProgressiveRender ( ) ;
6788 } ;
6889
69- requestAnimationFrame ( pump ) ;
90+ this . _rafId = requestAnimationFrame ( pump ) ;
7091 } ) ;
7192 }
93+
94+ private _startProgressiveRender ( ) : void {
95+ if ( this . _isProgressiveRenderActive ) {
96+ return ;
97+ }
98+
99+ this . _isProgressiveRenderActive = true ;
100+ this . _componentsStore ?. beginProgressiveRender ( ) ;
101+ }
102+
103+ private _finishProgressiveRender ( ) : void {
104+ if ( ! this . _isProgressiveRenderActive ) {
105+ return ;
106+ }
107+
108+ this . _isProgressiveRenderActive = false ;
109+ this . _componentsStore ?. endProgressiveRender ( ) ;
110+ }
72111}
0 commit comments