@@ -5,6 +5,9 @@ export class AnalysisManager {
55 this . engines = [ ] ;
66 this . _overlayMap = new Map ( ) ;
77 this . _statsCache = { } ;
8+ this . _analyzed = new Set ( ) ;
9+ this . _eagerEngines = new Set ( ) ;
10+ this . _storedArgs = null ;
811 }
912
1013 register ( engine ) {
@@ -14,14 +17,114 @@ export class AnalysisManager {
1417 }
1518 }
1619
20+ markEager ( engineName ) {
21+ this . _eagerEngines . add ( engineName ) ;
22+ }
23+
24+ analyzeEager ( layerMoves , profile ) {
25+ this . _statsCache = { } ;
26+ this . _storedArgs = { layerMoves, profile } ;
27+ for ( const engine of this . engines ) {
28+ if ( this . _eagerEngines . has ( engine . name ) ) {
29+ engine . analyze ( layerMoves , profile ) ;
30+ this . _analyzed . add ( engine . name ) ;
31+ }
32+ }
33+ }
34+
35+ ensureEngine ( engineName ) {
36+ if ( this . _analyzed . has ( engineName ) ) return ;
37+ if ( ! this . _storedArgs ) return ;
38+ const engine = this . engines . find ( e => e . name === engineName ) ;
39+ if ( ! engine ) return ;
40+ const { layerMoves, profile } = this . _storedArgs ;
41+ engine . analyze ( layerMoves , profile ) ;
42+ this . _analyzed . add ( engineName ) ;
43+ }
44+
45+ ensureAllAnalyzed ( ) {
46+ if ( ! this . _storedArgs ) return ;
47+ for ( const engine of this . engines ) {
48+ if ( ! this . _analyzed . has ( engine . name ) ) {
49+ const { layerMoves, profile } = this . _storedArgs ;
50+ engine . analyze ( layerMoves , profile ) ;
51+ this . _analyzed . add ( engine . name ) ;
52+ }
53+ }
54+ }
55+
56+ isAnalyzed ( engineName ) {
57+ return this . _analyzed . has ( engineName ) ;
58+ }
59+
60+ isAllAnalyzed ( ) {
61+ return this . engines . every ( e => this . _analyzed . has ( e . name ) ) ;
62+ }
63+
64+ async ensureEngineAsync ( engineName , onProgress ) {
65+ if ( this . _analyzed . has ( engineName ) ) return ;
66+ if ( ! this . _storedArgs ) return ;
67+ const engine = this . engines . find ( e => e . name === engineName ) ;
68+ if ( ! engine ) return ;
69+ const { layerMoves, profile } = this . _storedArgs ;
70+ if ( typeof engine . analyzeAsync === 'function' ) {
71+ await engine . analyzeAsync ( layerMoves , profile , onProgress ) ;
72+ } else {
73+ engine . analyze ( layerMoves , profile ) ;
74+ }
75+ this . _analyzed . add ( engineName ) ;
76+ }
77+
78+ async ensureAllAnalyzedAsync ( onProgress ) {
79+ if ( ! this . _storedArgs ) return ;
80+ const toRun = this . engines . filter ( e => ! this . _analyzed . has ( e . name ) ) ;
81+ if ( toRun . length === 0 ) return ;
82+ for ( let i = 0 ; i < toRun . length ; i ++ ) {
83+ const engine = toRun [ i ] ;
84+ const { layerMoves, profile } = this . _storedArgs ;
85+ const engineProgress = ( p ) => {
86+ if ( onProgress ) onProgress ( { engine : engine . name , engineProgress : p , overall : ( i + p ) / toRun . length } ) ;
87+ } ;
88+ if ( typeof engine . analyzeAsync === 'function' ) {
89+ await engine . analyzeAsync ( layerMoves , profile , engineProgress ) ;
90+ } else {
91+ engine . analyze ( layerMoves , profile ) ;
92+ }
93+ this . _analyzed . add ( engine . name ) ;
94+ if ( onProgress ) onProgress ( { engine : engine . name , engineProgress : 1 , overall : ( i + 1 ) / toRun . length } ) ;
95+ }
96+ }
97+
1798 analyzeAll ( layerMoves , profile ) {
1899 this . _statsCache = { } ;
100+ this . _storedArgs = { layerMoves, profile } ;
19101 for ( const engine of this . engines ) {
20102 engine . analyze ( layerMoves , profile ) ;
103+ this . _analyzed . add ( engine . name ) ;
104+ }
105+ }
106+
107+ async analyzeAllAsync ( layerMoves , profile , onProgress ) {
108+ this . _statsCache = { } ;
109+ this . _storedArgs = { layerMoves, profile } ;
110+ this . _analyzed . clear ( ) ;
111+ for ( let i = 0 ; i < this . engines . length ; i ++ ) {
112+ const engine = this . engines [ i ] ;
113+ const engineProgress = ( p ) => {
114+ if ( onProgress ) onProgress ( { engine : engine . name , engineProgress : p , overall : ( i + p ) / this . engines . length } ) ;
115+ } ;
116+ if ( typeof engine . analyzeAsync === 'function' ) {
117+ await engine . analyzeAsync ( layerMoves , profile , engineProgress ) ;
118+ } else {
119+ engine . analyze ( layerMoves , profile ) ;
120+ }
121+ this . _analyzed . add ( engine . name ) ;
122+ if ( onProgress ) onProgress ( { engine : engine . name , engineProgress : 1 , overall : ( i + 1 ) / this . engines . length } ) ;
21123 }
22124 }
23125
24126 getAllFindings ( ) {
127+ this . ensureAllAnalyzed ( ) ;
25128 const all = [ ] ;
26129 for ( const engine of this . engines ) {
27130 all . push ( ...engine . getFindings ( ) ) ;
@@ -31,6 +134,7 @@ export class AnalysisManager {
31134 }
32135
33136 getFindingsByEngine ( engineName ) {
137+ this . ensureEngine ( engineName ) ;
34138 const engine = this . engines . find ( e => e . name === engineName ) ;
35139 if ( ! engine ) return [ ] ;
36140 const findings = engine . getFindings ( ) ;
@@ -49,6 +153,7 @@ export class AnalysisManager {
49153 getOverlayValue ( overlayId , layerNum , moveIndex ) {
50154 const engine = this . _overlayMap . get ( overlayId ) ;
51155 if ( ! engine ) return 0 ;
156+ this . ensureEngine ( engine . name ) ;
52157 return engine . getOverlayData ( overlayId , layerNum , moveIndex ) ;
53158 }
54159
@@ -76,6 +181,8 @@ export class AnalysisManager {
76181
77182 clear ( ) {
78183 this . _statsCache = { } ;
184+ this . _analyzed . clear ( ) ;
185+ this . _storedArgs = null ;
79186 for ( const engine of this . engines ) {
80187 engine . clear ( ) ;
81188 }
0 commit comments