import { Injectable , OnDestroy } from '@angular/core' ; import { Observable , Subject } from 'rxjs' ; import { AlainChartConfig , AlainConfigService } from '@delon/util/config' ; import { LazyService } from '@delon/util/other' ; @ Injectable ( { providedIn : 'root' } ) export class ChartEChartsService implements OnDestroy { private _cog ! : AlainChartConfig ; private loading = false ; private loaded = false ; private notify$ = new Subject < void > ( ) ; get cog ( ) : AlainChartConfig { return this . _cog ; } set cog ( val ) { this . _cog = this . cogSrv . merge ( 'chart' , { theme : '' , echartsLib : 'https://cdnjs.cloudflare.com/ajax/libs/echarts/5.1.0/echarts.min.js' } as AlainChartConfig , val ) ! ; } constructor ( private cogSrv , private lazySrv ) { this . cog = { theme : '' } ; } libLoad ( ) { if ( this . loading ) { if ( this . loaded ) { this . notify$ . next ( ) ; } return this ; } this . loading = true ; this . lazySrv . load ( this . cog . echartsLib ! ) . then ( ( ) => { const extensions = this . cog . echartsExtensions ; if ( Array . isArray ( extensions ) && extensions . length > 0 ) { return this . lazySrv . load ( extensions ) . then ( ( ) => true ) ; } return Promise . resolve ( true ) ; } ) . then ( ( ) => { this . loaded = true ; this . notify$ . next ( ) ; } ) ; return this ; } get notify ( ) : Observable < void > { return this . notify$ . asObservable ( ) ; } ngOnDestroy ( ) { this . notify$ . unsubscribe ( ) ; } }