Skip to content

Commit

Permalink
fix: upload more than the concurrent limit
Browse files Browse the repository at this point in the history
  • Loading branch information
wil92 committed Feb 25, 2019
1 parent 6b6bb62 commit 750ea5a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/service-code-way/service-code-way.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
</div>
</div>


</div>
<div class="json">
<span>{{numberOfCopies}}/{{options.concurrency}}</span>
<pre>{{state | async | json}}</pre>
</div>
5 changes: 5 additions & 0 deletions src/app/service-code-way/service-code-way.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { takeUntil } from 'rxjs/operators';
import { UploadxOptions, UploadState, UploadxService, UploadItem } from '../../uploadx';
import { environment } from '../../environments/environment';
import { Ufile } from '../ufile';
import {Uploader} from '../../uploadx/src/uploader';
import {UploadStatus} from '../../uploadx/src/interfaces';

@Component({
selector: 'app-service-way',
Expand All @@ -23,6 +25,7 @@ export class ServiceCodeWayComponent implements OnDestroy, OnInit {
chunkSize: 1024 * 256 * 8
};
private ngUnsubscribe: Subject<any> = new Subject();
numberOfCopies = 0;

@ViewChild('file', { read: ElementRef }) fileInput: ElementRef;

Expand Down Expand Up @@ -72,6 +75,8 @@ export class ServiceCodeWayComponent implements OnDestroy, OnInit {
onUpload(uploadsOutStream: Observable<UploadState>) {
this.state = uploadsOutStream;
uploadsOutStream.pipe(takeUntil(this.ngUnsubscribe)).subscribe((item: UploadState) => {
this.numberOfCopies = this.uploadService.queue
.filter((uploader: Uploader) => uploader.status === 'uploading' as UploadStatus).length;
const index = this.uploads.findIndex(f => f.uploadId === item.uploadId);
if (item.status === 'added') {
const cfg: UploadItem = {
Expand Down
14 changes: 8 additions & 6 deletions src/uploadx/src/uploadx.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ export class UploadxService {
*/
private async autoUploadFiles() {
if (this.autoUpload) {
for (const upload of this.queue) {
await upload.upload();
}
this.processQueue();
}
}
Expand All @@ -92,7 +89,8 @@ export class UploadxService {
break;
case 'upload':
const uploadId = event.uploadId || event.itemOptions.uploadId;
this.queue.find(f => f.uploadId === uploadId).upload(event.itemOptions);
// noinspection TsLint
(this.concurrency - this.runningProcess() > 0) && this.queue.find(f => f.uploadId === uploadId).upload(event.itemOptions);
this.processQueue();
break;
case 'cancel':
Expand All @@ -109,13 +107,17 @@ export class UploadxService {
* Queue management
*/
private processQueue() {
const running = this.queue.filter((uploader: Uploader) => uploader.status === 'uploading');
const running = this.runningProcess();

this.queue
.filter((uploader: Uploader) => uploader.status === 'queue')
.slice(0, Math.max(this.concurrency - running.length, 0))
.slice(0, Math.max(this.concurrency - running, 0))
.forEach((uploader: Uploader) => {
uploader.upload();
});
}

runningProcess(): number {
return this.queue.filter((uploader: Uploader) => uploader.status === 'uploading').length;
}
}

0 comments on commit 750ea5a

Please sign in to comment.