Skip to content

Commit

Permalink
fix(): clear fileList after after already being handled (closes bleen…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Jul 30, 2017
1 parent 139ed58 commit 335dbc0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-uploader",
"version": "3.3.2",
"version": "3.3.3",
"main": "bundles/ngx-uploader.umd.js",
"module": "index.js",
"esnext": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/components/app-home/app-home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AppHomeComponent {
if (this.formData.autoUpload) {
const event: UploadInput = {
type: 'uploadAll',
url: '/upload',
url: 'http://ngx-uploader.com/upload',
method: 'POST',
data: { foo: 'bar' },
concurrency: this.formData.concurrency
Expand Down
44 changes: 24 additions & 20 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface UploadFile {
type: string;
progress: UploadProgress;
response?: any;
sub?: Subscription | any;
nativeFile?: File;
}

export interface UploadOutput {
Expand Down Expand Up @@ -75,7 +77,6 @@ export class NgUploaderService {
constructor() {
this.files = [];
this.serviceEvents = new EventEmitter<any>();
this.uploads = [];
}

handleFiles(files: FileList): void {
Expand All @@ -98,12 +99,12 @@ export class NgUploaderService {
endTime: null
}
},
lastModifiedDate: file.lastModifiedDate
lastModifiedDate: file.lastModifiedDate,
sub: Subscription,
nativeFile: file
};

this.serviceEvents.emit({ type: 'addedToQueue', file: uploadFile, nativeFile: file });
this.uploads.push({ file: uploadFile, sub: { instance: null } });

this.serviceEvents.emit({ type: 'addedToQueue', file: uploadFile });
return uploadFile;
}));

Expand All @@ -114,16 +115,17 @@ export class NgUploaderService {
input.subscribe((event: UploadInput) => {
switch (event.type) {
case 'uploadFile':
const uploadFileIndex = this.uploads.findIndex(upload => upload.file === event.file);
const uploadFileIndex = this.files.findIndex(file => file === event.file);
if (uploadFileIndex !== -1) {
this.uploads[uploadFileIndex].sub.instance = this.uploadFile(event.file, event).subscribe(data => {
this.files[uploadFileIndex].sub = this.uploadFile(event.file, event).subscribe(data => {
this.serviceEvents.emit(data);
});
}
break;
case 'uploadAll':
const concurrency = event.concurrency > 0 ? event.concurrency : Number.POSITIVE_INFINITY;
Observable.from(this.files.map(file => this.uploadFile(file, event)))
const files = this.files.filter(file => file.progress.status !== UploadStatus.Done);
Observable.from(files.map(file => this.uploadFile(file, event)))
.mergeAll(concurrency)
.subscribe((data: UploadOutput) => this.serviceEvents.emit(data));
break;
Expand All @@ -133,23 +135,24 @@ export class NgUploaderService {
return;
}

const index = this.uploads.findIndex(upload => upload.file.id === id);
const index = this.files.findIndex(file => file.id === id);
if (index !== -1) {
if (this.uploads[index].sub && this.uploads[index].sub.instance) {
this.uploads[index].sub.instance.unsubscribe();
if (this.files[index].sub) {
this.files[index].sub.unsubscribe();
}

this.serviceEvents.emit({ type: 'cancelled', file: this.uploads[index].file });
this.uploads[index].file.progress.status = UploadStatus.Canceled;
this.serviceEvents.emit({ type: 'cancelled', file: this.files[index] });
this.files[index].progress.status = UploadStatus.Canceled;
}
break;
case 'cancelAll':
this.uploads.forEach(upload => {
if (upload.sub && upload.sub.instance) {
upload.sub.instance.unsubscribe();
this.files.forEach(file => {
if (file.sub) {
file.sub.unsubscribe();
}
upload.file.progress.status = UploadStatus.Canceled;
this.serviceEvents.emit({ type: 'cancelled', file: upload.file });

file.progress.status = UploadStatus.Canceled;
this.serviceEvents.emit({ type: 'cancelled', file: file });
});
break;
case 'remove':
Expand Down Expand Up @@ -243,8 +246,9 @@ export class NgUploaderService {
const form = new FormData();
try {
const uploadFile = this.fileList.item(file.fileIndex);
const uploadIndex = this.uploads.findIndex(upload => upload.file.size === uploadFile.size);
if (this.uploads[uploadIndex].file.progress.status === UploadStatus.Canceled) {
const uploadIndex = this.files.findIndex(file => file.nativeFile === uploadFile);

if (this.files[uploadIndex].progress.status === UploadStatus.Canceled) {
observer.complete();
}

Expand Down
6 changes: 3 additions & 3 deletions src/ngx-uploader/directives/ng-file-select.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ export class NgFileSelectDirective implements OnInit, OnDestroy {

ngOnDestroy() {
this.el.removeEventListener('change', this.fileListener, false);

if (this.uploadInput)
{

if (this.uploadInput) {
this.uploadInput.unsubscribe();
}
}

fileListener = () => {
this.upload.handleFiles(this.el.files);
this.el.value = '';
}
}

0 comments on commit 335dbc0

Please sign in to comment.