@@ -20,7 +20,7 @@ import type { KLineData, VisibleRangeData } from './common/Data'
2020import type VisibleRange from './common/VisibleRange'
2121import type Coordinate from './common/Coordinate'
2222import { getDefaultVisibleRange } from './common/VisibleRange'
23- import TaskScheduler , { generateTaskId } from './common/TaskScheduler'
23+ import TaskScheduler from './common/TaskScheduler'
2424import type Crosshair from './common/Crosshair'
2525import type BarSpace from './common/BarSpace'
2626import type { Period } from './common/Period'
@@ -308,7 +308,7 @@ export default class StoreImp implements Store {
308308 /**
309309 * Task scheduler
310310 */
311- private readonly _taskScheduler = new TaskScheduler ( )
311+ private readonly _taskScheduler : TaskScheduler
312312
313313 /**
314314 * Overlay
@@ -376,6 +376,13 @@ export default class StoreImp implements Store {
376376 if ( isValid ( decimalFold ) ) {
377377 this . setDecimalFold ( decimalFold )
378378 }
379+ this . _taskScheduler = new TaskScheduler ( ( ) => {
380+ this . _chart . layout ( {
381+ measureWidth : true ,
382+ update : true ,
383+ buildYAxisTick : true
384+ } )
385+ } )
379386 }
380387
381388 setStyles ( value : string | DeepPartial < Styles > ) : void {
@@ -577,14 +584,13 @@ export default class StoreImp implements Store {
577584 adjustFlag = true
578585 }
579586 }
580- if ( success ) {
581- if ( adjustFlag ) {
582- this . _adjustVisibleRange ( )
583- this . setCrosshair ( this . _crosshair , { notInvalidate : true } )
584- const filterIndicators = this . getIndicatorsByFilter ( { } )
585- filterIndicators . forEach ( indicator => {
586- this . _addIndicatorCalcTask ( indicator , type )
587- } )
587+ if ( success && adjustFlag ) {
588+ this . _adjustVisibleRange ( )
589+ this . setCrosshair ( this . _crosshair , { notInvalidate : true } )
590+ const filterIndicators = this . getIndicatorsByFilter ( { } )
591+ if ( filterIndicators . length > 0 ) {
592+ this . _calcIndicator ( filterIndicators )
593+ } else {
588594 this . _chart . layout ( {
589595 measureWidth : true ,
590596 update : true ,
@@ -699,7 +705,7 @@ export default class StoreImp implements Store {
699705 symbol : this . _symbol ,
700706 period : this . _period ,
701707 timestamp : null ,
702- callback : ( data : KLineData [ ] , more ?: boolean ) => {
708+ callback : ( data : KLineData [ ] , more ?: DataLoadMore ) => {
703709 this . _loading = false
704710 this . _addData ( data , type , more )
705711 if ( type === 'init' ) {
@@ -1123,38 +1129,16 @@ export default class StoreImp implements Store {
11231129 }
11241130 }
11251131
1126- private _addIndicatorCalcTask ( indicator : IndicatorImp , dataLoadType : DataLoadType ) : void {
1127- indicator . onDataStateChange ?.( {
1128- state : 'loading' ,
1129- type : dataLoadType ,
1130- indicator
1131- } )
1132- void this . _taskScheduler . add < boolean > ( {
1133- id : generateTaskId ( indicator . id ) ,
1134- handler : async ( ) => await indicator . calcImp ( this . _dataList ) . then ( result => result )
1135- } ) . then ( result => {
1136- if ( result ) {
1137- this . _chart . layout ( {
1138- measureWidth : true ,
1139- update : true ,
1140- buildYAxisTick : true ,
1141- cacheYAxisWidth : dataLoadType !== 'init'
1142- } )
1143- indicator . onDataStateChange ?.( {
1144- state : 'ready' ,
1145- type : dataLoadType ,
1146- indicator
1147- } )
1148- }
1149- } ) . catch ( ( e : unknown ) => {
1150- if ( e !== 'canceled' ) {
1151- indicator . onDataStateChange ?.( {
1152- state : 'error' ,
1153- type : dataLoadType ,
1154- indicator
1155- } )
1156- }
1157- } )
1132+ private _calcIndicator ( data : IndicatorImp | IndicatorImp [ ] ) : void {
1133+ let indicators : IndicatorImp [ ] = [ ]
1134+ indicators = indicators . concat ( data )
1135+ if ( indicators . length > 0 ) {
1136+ const tasks : Record < string , Promise < unknown > > = { }
1137+ indicators . forEach ( indicator => {
1138+ tasks [ indicator . id ] = indicator . calcImp ( this . _dataList )
1139+ } )
1140+ this . _taskScheduler . add ( tasks )
1141+ }
11581142 }
11591143
11601144 addIndicator ( create : PickRequired < IndicatorCreate , 'id' | 'name' > , paneId : string , isStack : boolean ) : boolean {
@@ -1177,7 +1161,7 @@ export default class StoreImp implements Store {
11771161 paneIndicators . push ( indicator )
11781162 this . _indicators . set ( paneId , paneIndicators )
11791163 this . _sortIndicators ( paneId )
1180- this . _addIndicatorCalcTask ( indicator , 'init' )
1164+ this . _calcIndicator ( indicator )
11811165 return true
11821166 }
11831167
@@ -1211,7 +1195,6 @@ export default class StoreImp implements Store {
12111195 const paneIndicators = this . getIndicatorsByPaneId ( indicator . paneId )
12121196 const index = paneIndicators . findIndex ( ins => ins . id === indicator . id )
12131197 if ( index > - 1 ) {
1214- this . _taskScheduler . remove ( generateTaskId ( indicator . id ) )
12151198 paneIndicators . splice ( index , 1 )
12161199 removed = true
12171200 }
@@ -1269,7 +1252,7 @@ export default class StoreImp implements Store {
12691252 sortFlag = true
12701253 }
12711254 if ( calc ) {
1272- this . _addIndicatorCalcTask ( indicator , 'update' )
1255+ this . _calcIndicator ( indicator )
12731256 } else {
12741257 if ( draw ) {
12751258 updateFlag = true
0 commit comments