Skip to content
This repository was archived by the owner on May 11, 2020. It is now read-only.

Commit ee31a4e

Browse files
cipchkvthinkxie
authored andcommitted
perf(module:upload): optimize invalid parameter warning (NG-ZORRO#2363)
close NG-ZORRO#2322
1 parent 04cf7aa commit ee31a4e

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

components/upload/nz-upload-btn.component.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
OnDestroy,
1010
OnInit,
1111
Optional,
12-
SimpleChange,
13-
SimpleChanges,
1412
ViewChild
1513
} from '@angular/core';
1614
import { Observable, Subscription } from 'rxjs';
@@ -179,10 +177,10 @@ export class NzUploadBtnComponent implements OnInit, OnChanges, OnDestroy {
179177
const { uid } = file;
180178
let { data, headers } = opt;
181179
if (typeof data === 'function') {
182-
data = data(file);
180+
data = (data as (file: UploadFile) => {})(file);
183181
}
184182
if (typeof headers === 'function') {
185-
headers = headers(file);
183+
headers = (headers as (file: UploadFile) => {})(file);
186184
}
187185
const args: UploadXHRArgs = {
188186
action : opt.action,
@@ -195,15 +193,19 @@ export class NzUploadBtnComponent implements OnInit, OnChanges, OnDestroy {
195193
opt.onProgress(e, file);
196194
} : null,
197195
onSuccess : (ret, xhr) => {
198-
delete this.reqs[ uid ];
196+
this.clean(uid);
199197
opt.onSuccess(ret, file, xhr);
200198
},
201199
onError : (xhr) => {
202-
delete this.reqs[ uid ];
200+
this.clean(uid);
203201
opt.onError(xhr, file);
204202
}
205203
};
206-
this.reqs[ uid ] = (opt.customRequest || this.xhr).call(this, args);
204+
const req$ = (opt.customRequest || this.xhr).call(this, args);
205+
if (!(req$ instanceof Subscription)) {
206+
console.warn(`Must return Subscription type in '[nzCustomRequest]' property`);
207+
}
208+
this.reqs[ uid ] = req$;
207209
opt.onStart(file);
208210
}
209211

@@ -245,18 +247,19 @@ export class NzUploadBtnComponent implements OnInit, OnChanges, OnDestroy {
245247
});
246248
}
247249

250+
private clean(uid: string): void {
251+
const req$ = this.reqs[ uid ];
252+
if (req$ instanceof Subscription) {
253+
req$.unsubscribe();
254+
}
255+
delete this.reqs[ uid ];
256+
}
257+
248258
abort(file?: UploadFile): void {
249259
if (file) {
250-
const uid = file && file.uid;
251-
if (this.reqs[ uid ]) {
252-
this.reqs[ uid ].unsubscribe();
253-
delete this.reqs[ uid ];
254-
}
260+
this.clean(file && file.uid);
255261
} else {
256-
Object.keys(this.reqs).forEach((uid) => {
257-
this.reqs[ uid ].unsubscribe();
258-
delete this.reqs[ uid ];
259-
});
262+
Object.keys(this.reqs).forEach((uid) => this.clean(uid));
260263
}
261264
}
262265

@@ -285,7 +288,7 @@ export class NzUploadBtnComponent implements OnInit, OnChanges, OnDestroy {
285288
this.setClassMap();
286289
}
287290

288-
ngOnChanges(changes: { [P in keyof this]?: SimpleChange } & SimpleChanges): void {
291+
ngOnChanges(): void {
289292
if (this.inited) {
290293
this.setClassMap();
291294
}

components/upload/upload.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { NzIconModule } from '../icon/nz-icon.module';
1515
import { NzProgressModule } from '../progress/nz-progress.module';
1616
import { NzToolTipModule } from '../tooltip/nz-tooltip.module';
1717

18-
import { ShowUploadListInterface, UploadChangeParam, UploadFile, UploadFilter, UploadListType, UploadType, ZipButtonOptions } from './interface';
18+
import { ShowUploadListInterface, UploadChangeParam, UploadFile, UploadFilter, UploadListType, UploadType, UploadXHRArgs, ZipButtonOptions } from './interface';
1919
import { NzUploadBtnComponent } from './nz-upload-btn.component';
2020
import { NzUploadListComponent } from './nz-upload-list.component';
2121
import { NzUploadComponent } from './nz-upload.component';
@@ -503,6 +503,15 @@ describe('upload', () => {
503503
});
504504
});
505505

506+
describe('[test boundary]', () => {
507+
it('clean a not exists request', () => {
508+
instance.comp.upload.reqs.test = null;
509+
instance.show = false;
510+
fixture.detectChanges();
511+
expect(true).toBe(true);
512+
});
513+
});
514+
506515
class NzUploadPageObject {
507516
private files: any;
508517
constructor() {
@@ -966,13 +975,21 @@ describe('upload', () => {
966975
comp.onChange(PNGSMALL as any);
967976
expect(comp.options.customRequest).toHaveBeenCalled();
968977
});
978+
979+
it('should be warn "Must return Subscription type in [nzCustomRequest] property"', () => {
980+
let warnMsg = '';
981+
console.warn = jasmine.createSpy().and.callFake(res => warnMsg = res);
982+
comp.options.customRequest = ((item: UploadXHRArgs) => { }) as any;
983+
comp.onChange(PNGSMALL as any);
984+
expect(warnMsg).toContain(`Must return Subscription type`);
985+
});
969986
});
970987
});
971988
});
972989

973990
@Component({
974991
template: `
975-
<nz-upload #upload
992+
<nz-upload #upload *ngIf="show"
976993
[nzType]="nzType"
977994
[nzLimit]="nzLimit"
978995
[nzSize]="nzSize"
@@ -1005,6 +1022,7 @@ describe('upload', () => {
10051022
})
10061023
class TestUploadComponent {
10071024
@ViewChild('upload') comp: NzUploadComponent;
1025+
show = true;
10081026
nzType: UploadType = 'select';
10091027
nzLimit = 0;
10101028
nzSize = 0;

0 commit comments

Comments
 (0)