@@ -88,9 +88,6 @@ export class Uploader {
8888 }
8989 this . destination = destinationFolder
9090
91- // Reset when upload queue is done
92- this . _jobQueue . addListener ( 'idle' , ( ) => this . reset ( ) )
93-
9491 logger . debug ( 'Upload workspace initialized' , {
9592 destination : this . destination ,
9693 root : this . root ,
@@ -157,12 +154,18 @@ export class Uploader {
157154 }
158155
159156 private reset ( ) {
157+ // If there is no upload in the queue and no job in the queue
158+ if ( this . _uploadQueue . length === 0 && this . _jobQueue . size === 0 ) {
159+ return
160+ }
161+
160162 // Reset upload queue but keep the reference
161163 this . _uploadQueue . splice ( 0 , this . _uploadQueue . length )
162164 this . _jobQueue . clear ( )
163165 this . _queueSize = 0
164166 this . _queueProgress = 0
165167 this . _queueStatus = Status . IDLE
168+ logger . debug ( 'Uploader state reset' )
166169 }
167170
168171 /**
@@ -595,6 +598,16 @@ export class Uploader {
595598 this . _jobQueue . add ( request )
596599 this . updateStats ( )
597600 }
601+
602+ // Reset when upload queue is done
603+ // Only when we know we're closing on the last chunks
604+ // and/or assembling we can reset the uploader.
605+ // Otherwise he queue might be idle for a short time
606+ // and clear the Upload queue before we're done.
607+ this . _jobQueue . onIdle ( )
608+ . then ( ( ) => this . reset ( ) )
609+
610+ // Finally return the Upload
598611 return upload
599612 } ) as PCancelable < Upload >
600613
0 commit comments