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

View file

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

View file

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