upload store private functions
This commit is contained in:
parent
10041b18ec
commit
e7e46c91db
@ -93,15 +93,14 @@ const sentPercent = computed(() =>
|
|||||||
);
|
);
|
||||||
|
|
||||||
const sentMbytes = computed(() => byteToMbyte(uploadStore.sentBytes));
|
const sentMbytes = computed(() => byteToMbyte(uploadStore.sentBytes));
|
||||||
|
|
||||||
const totalMbytes = computed(() => byteToMbyte(uploadStore.totalBytes));
|
const totalMbytes = computed(() => byteToMbyte(uploadStore.totalBytes));
|
||||||
|
|
||||||
const speedMbytes = computed(() => byteToMbyte(speed.value));
|
const speedMbytes = computed(() => byteToMbyte(speed.value));
|
||||||
|
|
||||||
let lastSpeedUpdate: number = 0;
|
let lastSpeedUpdate: number = 0;
|
||||||
let recentSpeeds: number[] = [];
|
let recentSpeeds: number[] = [];
|
||||||
|
|
||||||
const calculateSpeed = (sentBytes: number, oldSentBytes: number) => {
|
const calculateSpeed = (sentBytes: number, oldSentBytes: number) => {
|
||||||
|
// Reset the state when the uploads batch is complete
|
||||||
if (sentBytes === 0) {
|
if (sentBytes === 0) {
|
||||||
lastSpeedUpdate = 0;
|
lastSpeedUpdate = 0;
|
||||||
recentSpeeds = [];
|
recentSpeeds = [];
|
||||||
@ -124,12 +123,13 @@ const calculateSpeed = (sentBytes: number, oldSentBytes: number) => {
|
|||||||
const recentSpeedsAverage =
|
const recentSpeedsAverage =
|
||||||
recentSpeeds.reduce((acc, curr) => acc + curr) / recentSpeeds.length;
|
recentSpeeds.reduce((acc, curr) => acc + curr) / recentSpeeds.length;
|
||||||
|
|
||||||
|
// Use the current speed for the first update to avoid smoothing lag
|
||||||
if (recentSpeeds.length === 1) {
|
if (recentSpeeds.length === 1) {
|
||||||
speed.value = recentSpeedsAverage;
|
speed.value = currentSpeed;
|
||||||
} else {
|
|
||||||
speed.value = recentSpeedsAverage * 0.2 + speed.value * 0.8;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
speed.value = recentSpeedsAverage * 0.2 + speed.value * 0.8;
|
||||||
|
|
||||||
lastSpeedUpdate = Date.now();
|
lastSpeedUpdate = Date.now();
|
||||||
|
|
||||||
calculateEta();
|
calculateEta();
|
||||||
@ -151,10 +151,11 @@ const calculateEta = () => {
|
|||||||
watch(sentBytes, calculateSpeed);
|
watch(sentBytes, calculateSpeed);
|
||||||
|
|
||||||
watch(totalBytes, (totalBytes, oldTotalBytes) => {
|
watch(totalBytes, (totalBytes, oldTotalBytes) => {
|
||||||
if (oldTotalBytes > 0) {
|
if (oldTotalBytes !== 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark the start time of a new upload batch
|
||||||
lastSpeedUpdate = Date.now();
|
lastSpeedUpdate = Date.now();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -32,33 +32,6 @@ export const useUploadStore = defineStore("upload", () => {
|
|||||||
// ACTIONS
|
// ACTIONS
|
||||||
//
|
//
|
||||||
|
|
||||||
const reset = () => {
|
|
||||||
if (progressInterval !== null) {
|
|
||||||
clearInterval(progressInterval);
|
|
||||||
progressInterval = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
allUploads.value = [];
|
|
||||||
activeUploads.value = new Set();
|
|
||||||
lastUpload.value = -1;
|
|
||||||
totalBytes.value = 0;
|
|
||||||
sentBytes.value = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
const nextUpload = (): Upload => {
|
|
||||||
lastUpload.value++;
|
|
||||||
|
|
||||||
const upload = allUploads.value[lastUpload.value];
|
|
||||||
activeUploads.value.add(upload);
|
|
||||||
|
|
||||||
return upload;
|
|
||||||
};
|
|
||||||
|
|
||||||
const hasActiveUploads = () => activeUploads.value.size > 0;
|
|
||||||
|
|
||||||
const hasPendingUploads = () =>
|
|
||||||
allUploads.value.length > lastUpload.value + 1;
|
|
||||||
|
|
||||||
const upload = (
|
const upload = (
|
||||||
path: string,
|
path: string,
|
||||||
name: string,
|
name: string,
|
||||||
@ -79,6 +52,7 @@ export const useUploadStore = defineStore("upload", () => {
|
|||||||
type,
|
type,
|
||||||
totalBytes: file?.size ?? 0,
|
totalBytes: file?.size ?? 0,
|
||||||
sentBytes: 0,
|
sentBytes: 0,
|
||||||
|
// Stores rapidly changing sent bytes value without causing component re-renders
|
||||||
rawProgress: markRaw({
|
rawProgress: markRaw({
|
||||||
sentBytes: 0,
|
sentBytes: 0,
|
||||||
}),
|
}),
|
||||||
@ -90,14 +64,14 @@ export const useUploadStore = defineStore("upload", () => {
|
|||||||
processUploads();
|
processUploads();
|
||||||
};
|
};
|
||||||
|
|
||||||
const finishUpload = (upload: Upload) => {
|
//
|
||||||
sentBytes.value += upload.totalBytes - upload.sentBytes;
|
// PRIVATE FUNCTIONS
|
||||||
upload.sentBytes = upload.totalBytes;
|
//
|
||||||
upload.file = null;
|
|
||||||
|
|
||||||
activeUploads.value.delete(upload);
|
const hasActiveUploads = () => activeUploads.value.size > 0;
|
||||||
processUploads();
|
|
||||||
};
|
const hasPendingUploads = () =>
|
||||||
|
allUploads.value.length > lastUpload.value + 1;
|
||||||
|
|
||||||
const isActiveUploadsOnLimit = () => activeUploads.value.size < UPLOADS_LIMIT;
|
const isActiveUploadsOnLimit = () => activeUploads.value.size < UPLOADS_LIMIT;
|
||||||
|
|
||||||
@ -112,6 +86,7 @@ export const useUploadStore = defineStore("upload", () => {
|
|||||||
|
|
||||||
if (isActiveUploadsOnLimit() && hasPendingUploads()) {
|
if (isActiveUploadsOnLimit() && hasPendingUploads()) {
|
||||||
if (!hasActiveUploads()) {
|
if (!hasActiveUploads()) {
|
||||||
|
// Update the state in a fixed time interval
|
||||||
progressInterval = window.setInterval(syncState, 1000);
|
progressInterval = window.setInterval(syncState, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,6 +108,24 @@ export const useUploadStore = defineStore("upload", () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const nextUpload = (): Upload => {
|
||||||
|
lastUpload.value++;
|
||||||
|
|
||||||
|
const upload = allUploads.value[lastUpload.value];
|
||||||
|
activeUploads.value.add(upload);
|
||||||
|
|
||||||
|
return upload;
|
||||||
|
};
|
||||||
|
|
||||||
|
const finishUpload = (upload: Upload) => {
|
||||||
|
sentBytes.value += upload.totalBytes - upload.sentBytes;
|
||||||
|
upload.sentBytes = upload.totalBytes;
|
||||||
|
upload.file = null;
|
||||||
|
|
||||||
|
activeUploads.value.delete(upload);
|
||||||
|
processUploads();
|
||||||
|
};
|
||||||
|
|
||||||
const syncState = () => {
|
const syncState = () => {
|
||||||
for (const upload of activeUploads.value) {
|
for (const upload of activeUploads.value) {
|
||||||
sentBytes.value += upload.rawProgress.sentBytes - upload.sentBytes;
|
sentBytes.value += upload.rawProgress.sentBytes - upload.sentBytes;
|
||||||
@ -140,18 +133,26 @@ export const useUploadStore = defineStore("upload", () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
if (progressInterval !== null) {
|
||||||
|
clearInterval(progressInterval);
|
||||||
|
progressInterval = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
allUploads.value = [];
|
||||||
|
activeUploads.value = new Set();
|
||||||
|
lastUpload.value = -1;
|
||||||
|
totalBytes.value = 0;
|
||||||
|
sentBytes.value = 0;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// STATE
|
// STATE
|
||||||
allUploads,
|
|
||||||
activeUploads,
|
activeUploads,
|
||||||
lastUpload,
|
|
||||||
totalBytes,
|
totalBytes,
|
||||||
sentBytes,
|
sentBytes,
|
||||||
|
|
||||||
// ACTIONS
|
// ACTIONS
|
||||||
reset,
|
|
||||||
upload,
|
upload,
|
||||||
finishUpload,
|
|
||||||
processUploads,
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<div v-if="uploadStore.getProgress" class="progress">
|
<div v-if="uploadStore.totalBytes" class="progress">
|
||||||
<div v-bind:style="{ width: uploadStore.getProgress + '%' }"></div>
|
<div
|
||||||
|
v-bind:style="{
|
||||||
|
width: sentPercent + '%',
|
||||||
|
}"
|
||||||
|
></div>
|
||||||
</div>
|
</div>
|
||||||
<sidebar></sidebar>
|
<sidebar></sidebar>
|
||||||
<main>
|
<main>
|
||||||
@ -27,7 +31,7 @@ import Prompts from "@/components/prompts/Prompts.vue";
|
|||||||
import Shell from "@/components/Shell.vue";
|
import Shell from "@/components/Shell.vue";
|
||||||
import UploadFiles from "@/components/prompts/UploadFiles.vue";
|
import UploadFiles from "@/components/prompts/UploadFiles.vue";
|
||||||
import { enableExec } from "@/utils/constants";
|
import { enableExec } from "@/utils/constants";
|
||||||
import { watch } from "vue";
|
import { computed, watch } from "vue";
|
||||||
import { useRoute } from "vue-router";
|
import { useRoute } from "vue-router";
|
||||||
|
|
||||||
const layoutStore = useLayoutStore();
|
const layoutStore = useLayoutStore();
|
||||||
@ -36,6 +40,10 @@ const fileStore = useFileStore();
|
|||||||
const uploadStore = useUploadStore();
|
const uploadStore = useUploadStore();
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
|
|
||||||
|
const sentPercent = computed(() =>
|
||||||
|
((uploadStore.sentBytes / uploadStore.totalBytes) * 100).toFixed(2)
|
||||||
|
);
|
||||||
|
|
||||||
watch(route, () => {
|
watch(route, () => {
|
||||||
fileStore.selected = [];
|
fileStore.selected = [];
|
||||||
fileStore.multiple = false;
|
fileStore.multiple = false;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user