feat: limit the number of concurrent uploads (#98)
This commit is contained in:
parent
76e967fa59
commit
05dbcfb2df
1 changed files with 30 additions and 3 deletions
|
@ -53,7 +53,7 @@ class Uploader {
|
||||||
}
|
}
|
||||||
|
|
||||||
upload() {
|
upload() {
|
||||||
const { file, idx, name } = this;
|
const { idx, name } = this;
|
||||||
const url = getUrl(name);
|
const url = getUrl(name);
|
||||||
const encodedUrl = encodedStr(url);
|
const encodedUrl = encodedStr(url);
|
||||||
const encodedName = encodedStr(name);
|
const encodedName = encodedStr(name);
|
||||||
|
@ -70,8 +70,15 @@ class Uploader {
|
||||||
$uploadersTable.classList.remove("hidden");
|
$uploadersTable.classList.remove("hidden");
|
||||||
$emptyFolder.classList.add("hidden");
|
$emptyFolder.classList.add("hidden");
|
||||||
this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
|
this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
|
||||||
this.lastUptime = Date.now();
|
this.$uploadStatus.innerHTML = '-';
|
||||||
|
Uploader.queues.push(this);
|
||||||
|
Uploader.runQueue();
|
||||||
|
}
|
||||||
|
|
||||||
|
ajax() {
|
||||||
|
Uploader.runings += 1;
|
||||||
|
const url = getUrl(this.name);
|
||||||
|
this.lastUptime = Date.now();
|
||||||
const ajax = new XMLHttpRequest();
|
const ajax = new XMLHttpRequest();
|
||||||
ajax.upload.addEventListener("progress", e => this.progress(e), false);
|
ajax.upload.addEventListener("progress", e => this.progress(e), false);
|
||||||
ajax.addEventListener("readystatechange", () => {
|
ajax.addEventListener("readystatechange", () => {
|
||||||
|
@ -86,8 +93,9 @@ class Uploader {
|
||||||
ajax.addEventListener("error", () => this.fail(), false);
|
ajax.addEventListener("error", () => this.fail(), false);
|
||||||
ajax.addEventListener("abort", () => this.fail(), false);
|
ajax.addEventListener("abort", () => this.fail(), false);
|
||||||
ajax.open("PUT", url);
|
ajax.open("PUT", url);
|
||||||
ajax.send(file);
|
ajax.send(this.file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
progress(event) {
|
progress(event) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
@ -103,15 +111,34 @@ class Uploader {
|
||||||
|
|
||||||
complete() {
|
complete() {
|
||||||
this.$uploadStatus.innerHTML = `✓`;
|
this.$uploadStatus.innerHTML = `✓`;
|
||||||
|
Uploader.runings -= 1;
|
||||||
|
Uploader.runQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
fail() {
|
fail() {
|
||||||
this.$uploadStatus.innerHTML = `✗`;
|
this.$uploadStatus.innerHTML = `✗`;
|
||||||
|
Uploader.runings -= 1;
|
||||||
|
Uploader.runQueue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Uploader.globalIdx = 0;
|
Uploader.globalIdx = 0;
|
||||||
|
|
||||||
|
Uploader.runings = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type Uploader[]
|
||||||
|
*/
|
||||||
|
Uploader.queues = [];
|
||||||
|
|
||||||
|
|
||||||
|
Uploader.runQueue = () => {
|
||||||
|
if (Uploader.runings > 2) return;
|
||||||
|
let uploader = Uploader.queues.shift();
|
||||||
|
if (!uploader) return;
|
||||||
|
uploader.ajax();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add breadcrumb
|
* Add breadcrumb
|
||||||
* @param {string} href
|
* @param {string} href
|
||||||
|
|
Loading…
Reference in a new issue