11import { ApplicationRef , ComponentRef , inject , Injectable , ViewContainerRef } from '@angular/core' ;
22import { LoaderComponent } from '../../components' ;
33
4-
54@Injectable ( { providedIn : 'root' } )
65export class LoaderService {
7-
86 private componentRef : ComponentRef < LoaderComponent > | null ;
97 private readonly applicationRef : ApplicationRef ;
108
@@ -14,17 +12,23 @@ export class LoaderService {
1412 }
1513
1614 public start ( text ?: string ) : void {
17- const container = this . applicationRef . components [ 0 ] . injector . get ( ViewContainerRef ) ;
18- const loaderComponentRef = container . createComponent ( LoaderComponent ) ;
19- loaderComponentRef . instance . label = text ;
20- this . componentRef = loaderComponentRef ;
15+ const timeoutRef = setTimeout ( ( ) => {
16+ const container = this . applicationRef . components [ 0 ] . injector . get ( ViewContainerRef ) ;
17+ const loaderComponentRef = container . createComponent ( LoaderComponent ) ;
18+ loaderComponentRef . instance . label = text ;
19+ this . componentRef = loaderComponentRef ;
20+ clearTimeout ( timeoutRef ) ;
21+ } , 100 ) ;
2122 }
2223
2324 public stop ( ) : void {
24- if ( this . componentRef !== null ) {
25- this . componentRef ?. destroy ( ) ;
26- this . componentRef = null ;
27- }
25+ const timeoutRef = setTimeout ( ( ) => {
26+ if ( this . componentRef !== null ) {
27+ this . componentRef ?. destroy ( ) ;
28+ this . componentRef = null ;
29+ }
30+ clearTimeout ( timeoutRef ) ;
31+ } , 100 ) ;
2832 }
2933
3034 public startAndStop ( fn : ( ) => void , message ?: string ) {
0 commit comments