fix(client/worker): incorrect uploading list filtering logic (#39)

This commit is contained in:
Hexxa 2021-02-02 12:02:32 +08:00 committed by GitHub
parent b0d1cdccfc
commit 76334b808d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 29 deletions

View file

@ -31,12 +31,8 @@ describe("upload.worker", () => {
); );
when(mockUploaderClass.stop()).thenCall(() => {}); when(mockUploaderClass.stop()).thenCall(() => {});
let currentUploader: FileUploader = undefined;
let uploaderFile: File = undefined;
let uploaderFilePath: string = undefined;
let uploaderStopFilePath: string = undefined;
interface TestCase { interface TestCase {
desc: string;
infos: Array<UploadEntry>; infos: Array<UploadEntry>;
expectedUploadingFile: string; expectedUploadingFile: string;
expectedUploaderStartInput: string; expectedUploaderStartInput: string;
@ -45,33 +41,41 @@ describe("upload.worker", () => {
const tcs: Array<TestCase> = [ const tcs: Array<TestCase> = [
{ {
desc: "add new uploadings when worker is in idle",
infos: [makeEntry("file1", true), makeEntry("file2", true)], infos: [makeEntry("file1", true), makeEntry("file2", true)],
currentFilePath: "",
expectedUploadingFile: "file1", expectedUploadingFile: "file1",
expectedUploaderStartInput: "file1", expectedUploaderStartInput: "file1",
currentFilePath: "",
}, },
{ {
desc: "add new uploadings when worker is in idle and skip some stopped files",
infos: [makeEntry("file1", false), makeEntry("file2", true)], infos: [makeEntry("file1", false), makeEntry("file2", true)],
currentFilePath: "",
expectedUploadingFile: "file2", expectedUploadingFile: "file2",
expectedUploaderStartInput: "file2", expectedUploaderStartInput: "file2",
currentFilePath: "",
}, },
{ {
infos: [makeEntry("file1", true), makeEntry("file0", true)], desc: "current file should be stopped and start new uploading",
infos: [makeEntry("file0", false), makeEntry("file1", true)],
currentFilePath: "file0",
expectedUploadingFile: "file1", expectedUploadingFile: "file1",
expectedUploaderStartInput: "file1", expectedUploaderStartInput: "file1",
currentFilePath: "file0",
}, },
{ {
desc: "uploader should keep uploading if the first uploadable file is not changed",
infos: [makeEntry("file1", true)], infos: [makeEntry("file1", true)],
expectedUploadingFile: "file1", expectedUploadingFile: "file1",
expectedUploaderStartInput: "file1", expectedUploaderStartInput: undefined,
currentFilePath: "file1", currentFilePath: "file1",
}, },
]; ];
for (let i = 0; i < tcs.length; i++) { for (let i = 0; i < tcs.length; i++) {
const uploadWorker = new UploadWorker(); const uploadWorker = new UploadWorker();
let currentUploader: FileUploader = undefined;
let uploaderFile: File = undefined;
let uploaderFilePath: string = undefined;
let uploaderStopFilePath: string = undefined;
uploadWorker.sendEvent = (_: FileWorkerResp) => {}; uploadWorker.sendEvent = (_: FileWorkerResp) => {};
uploadWorker.makeUploader = ( uploadWorker.makeUploader = (
file: File, file: File,
@ -82,7 +86,6 @@ describe("upload.worker", () => {
currentUploader = instance(mockUploaderClass); currentUploader = instance(mockUploaderClass);
return currentUploader; return currentUploader;
}; };
if (tcs[i].currentFilePath !== "") { if (tcs[i].currentFilePath !== "") {
uploadWorker.setFilePath(tcs[i].currentFilePath); uploadWorker.setFilePath(tcs[i].currentFilePath);
} }
@ -96,6 +99,8 @@ describe("upload.worker", () => {
data: req, data: req,
}) })
); );
console.log(tcs[i].desc);
expect(uploadWorker.getFilePath()).toEqual(tcs[i].expectedUploadingFile); expect(uploadWorker.getFilePath()).toEqual(tcs[i].expectedUploadingFile);
expect(uploaderFilePath).toEqual(tcs[i].expectedUploaderStartInput); expect(uploaderFilePath).toEqual(tcs[i].expectedUploaderStartInput);
} }

View file

@ -53,20 +53,18 @@ export class UploadWorker {
const infoArray = syncReq.infos; const infoArray = syncReq.infos;
for (let i = 0; i < infoArray.length; i++) { for (let i = 0; i < infoArray.length; i++) {
if ( if (infoArray[i].runnable) {
infoArray[i].runnable && if (infoArray[i].filePath === this.filePath) {
infoArray[i].uploaded < infoArray[i].size // in uploading, do nothing
) { } else {
// infoArray[i].filePath !== this.filePath, it may re-uploading a deleted file this.stopUploader();
// and it will stuck or the file will be renamed in the future this.startUploader(infoArray[i].file, infoArray[i].filePath);
this.stopUploader(); }
this.startUploader(infoArray[i].file, infoArray[i].filePath);
break; break;
} else if ( } else {
!infoArray[i].runnable && if (infoArray[i].filePath === this.filePath) {
infoArray[i].filePath == this.filePath this.stopUploader();
) { }
this.stopUploader();
} }
} }
break; break;

View file

@ -1,4 +1,4 @@
import { Map } from "immutable"; import { Map, OrderedMap } from "immutable";
import { import {
FileWorkerReq, FileWorkerReq,
@ -20,7 +20,7 @@ export interface IWorker {
} }
export class UploadMgr { export class UploadMgr {
private infos = Map<string, UploadEntry>(); private infos = OrderedMap<string, UploadEntry>();
private worker: IWorker; private worker: IWorker;
private intervalID: number; private intervalID: number;
private cycle: number = 500; private cycle: number = 500;
@ -44,7 +44,7 @@ export class UploadMgr {
win.clearInterval(this.intervalID); win.clearInterval(this.intervalID);
}; };
_setInfos = (infos: Map<string, UploadEntry>) => { _setInfos = (infos: OrderedMap<string, UploadEntry>) => {
this.infos = infos; this.infos = infos;
}; };
@ -98,7 +98,7 @@ export class UploadMgr {
this.infos = this.infos.delete(filePath); this.infos = this.infos.delete(filePath);
}; };
list = (): Map<string, UploadEntry> => { list = (): OrderedMap<string, UploadEntry> => {
return this.infos; return this.infos;
}; };
@ -128,7 +128,7 @@ export class UploadMgr {
} }
// call back to update the info // call back to update the info
this.statusCb(this.infos); this.statusCb(this.infos.toMap());
} else { } else {
// TODO: refine this // TODO: refine this
console.error( console.error(