11import * as d from '../../declarations' ;
2- import { onBuildLog , onBuildResults } from './build-events' ;
2+ import { onBuildLog , onBuildResults , onBuildStatus } from './build-events' ;
33
44
55export function initBuildProgress ( win : d . DevClientWindow , doc : Document ) {
6- const PROGRESS_BAR_ID = `dev-server-progress` ;
76 const barColor = `#5851ff` ;
87 const errorColor = `#b70c19` ;
9- let removeTimerId : any ;
8+ let addBarTimerId : any ;
9+ let removeBarTimerId : any ;
1010 let opacityTimerId : any ;
1111 let incIntervalId : any ;
1212 let progressIncrease : number ;
1313 let currentProgress = 0 ;
1414
1515 function update ( ) {
1616 clearTimeout ( opacityTimerId ) ;
17- clearTimeout ( removeTimerId ) ;
17+ clearTimeout ( removeBarTimerId ) ;
1818
19- let progressBar = doc . getElementById ( PROGRESS_BAR_ID ) ;
19+ const progressBar = getProgressBar ( ) ;
2020 if ( ! progressBar ) {
21- progressBar = doc . createElement ( 'div' ) ;
22- progressBar . id = PROGRESS_BAR_ID ;
23- progressBar . style . position = `absolute` ;
24- progressBar . style . top = `0` ;
25- progressBar . style . left = `0` ;
26- progressBar . style . zIndex = `100001` ;
27- progressBar . style . width = `100%` ;
28- progressBar . style . height = `2px` ;
29- progressBar . style . transform = `scaleX(0)` ;
30- progressBar . style . opacity = `1` ;
31- progressBar . style . background = barColor ;
32- progressBar . style . transformOrigin = `left center` ;
33- progressBar . style . transition = `transform .1s ease-in-out, opacity .5s ease-in` ;
34- ( progressBar . style as any ) . contain = `strict` ;
35- doc . body . appendChild ( progressBar ) ;
36- requestAnimationFrame ( update ) ;
21+ createProgressBar ( ) ;
22+ addBarTimerId = setTimeout ( update , 16 ) ;
3723 return ;
3824 }
3925 progressBar . style . background = barColor ;
@@ -57,23 +43,30 @@ export function initBuildProgress(win: d.DevClientWindow, doc: Document) {
5743 progressIncrease = 0.05 ;
5844 incIntervalId = null ;
5945 clearTimeout ( opacityTimerId ) ;
60- clearTimeout ( removeTimerId ) ;
46+ clearTimeout ( addBarTimerId ) ;
47+ clearTimeout ( removeBarTimerId ) ;
6148
62- const progressBar = doc . getElementById ( PROGRESS_BAR_ID ) ;
49+ const progressBar = getProgressBar ( ) ;
6350 if ( progressBar ) {
6451 if ( currentProgress >= 1 ) {
6552 progressBar . style . transform = `scaleX(1)` ;
6653 }
6754
6855 opacityTimerId = setTimeout ( ( ) => {
6956 try {
70- progressBar . style . opacity = `0` ;
57+ const progressBar = getProgressBar ( ) ;
58+ if ( progressBar ) {
59+ progressBar . style . opacity = `0` ;
60+ }
7161 } catch ( e ) { }
7262 } , 150 ) ;
7363
74- removeTimerId = setTimeout ( ( ) => {
64+ removeBarTimerId = setTimeout ( ( ) => {
7565 try {
76- progressBar . parentNode . removeChild ( progressBar ) ;
66+ const progressBar = getProgressBar ( ) ;
67+ if ( progressBar ) {
68+ progressBar . parentNode . removeChild ( progressBar ) ;
69+ }
7770 } catch ( e ) { }
7871 } , 1000 ) ;
7972 }
@@ -98,7 +91,7 @@ export function initBuildProgress(win: d.DevClientWindow, doc: Document) {
9891
9992 onBuildResults ( win , buildResults => {
10093 if ( buildResults . hasError ) {
101- const progressBar = doc . getElementById ( PROGRESS_BAR_ID ) ;
94+ const progressBar = getProgressBar ( ) ;
10295 if ( progressBar ) {
10396 progressBar . style . transform = `scaleX(1)` ;
10497 progressBar . style . background = errorColor ;
@@ -107,7 +100,38 @@ export function initBuildProgress(win: d.DevClientWindow, doc: Document) {
107100 reset ( ) ;
108101 } ) ;
109102
103+ onBuildStatus ( win , buildStatus => {
104+ if ( buildStatus === 'disabled' ) {
105+ reset ( ) ;
106+ }
107+ } ) ;
108+
110109 if ( doc . head . dataset . tmpl === 'tmpl-initial-load' ) {
111110 update ( ) ;
112111 }
112+
113+ const PROGRESS_BAR_ID = `dev-server-progress-bar` ;
114+
115+ function getProgressBar ( ) {
116+ return doc . getElementById ( PROGRESS_BAR_ID ) ;
117+ }
118+
119+ function createProgressBar ( ) {
120+ const progressBar = doc . createElement ( 'div' ) ;
121+ progressBar . id = PROGRESS_BAR_ID ;
122+ progressBar . style . position = `absolute` ;
123+ progressBar . style . top = `0` ;
124+ progressBar . style . left = `0` ;
125+ progressBar . style . zIndex = `100001` ;
126+ progressBar . style . width = `100%` ;
127+ progressBar . style . height = `2px` ;
128+ progressBar . style . transform = `scaleX(0)` ;
129+ progressBar . style . opacity = `1` ;
130+ progressBar . style . background = barColor ;
131+ progressBar . style . transformOrigin = `left center` ;
132+ progressBar . style . transition = `transform .1s ease-in-out, opacity .5s ease-in` ;
133+ ( progressBar . style as any ) . contain = `strict` ;
134+ doc . body . appendChild ( progressBar ) ;
135+ }
136+
113137}
0 commit comments