2024-05-02 23:26:13 +02:00
|
|
|
import { derived, get, writable } from 'svelte/store';
|
2023-09-01 18:00:51 +02:00
|
|
|
import { UploadState, type UploadAsset } from '../models/upload-asset';
|
2022-06-19 15:16:35 +02:00
|
|
|
|
|
|
|
function createUploadStore() {
|
2023-07-01 06:50:47 +02:00
|
|
|
const uploadAssets = writable<Array<UploadAsset>>([]);
|
2023-09-01 18:00:51 +02:00
|
|
|
|
2023-08-10 03:11:26 +02:00
|
|
|
const duplicateCounter = writable(0);
|
2023-09-01 18:00:51 +02:00
|
|
|
const successCounter = writable(0);
|
|
|
|
const totalUploadCounter = writable(0);
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
const { subscribe } = uploadAssets;
|
|
|
|
|
|
|
|
const isUploading = derived(uploadAssets, ($uploadAssets) => {
|
2023-09-01 18:00:51 +02:00
|
|
|
return $uploadAssets.length > 0;
|
2023-07-01 06:50:47 +02:00
|
|
|
});
|
2023-09-01 18:00:51 +02:00
|
|
|
const errorsAssets = derived(uploadAssets, (a) => a.filter((e) => e.state === UploadState.ERROR));
|
|
|
|
const errorCounter = derived(errorsAssets, (values) => values.length);
|
|
|
|
const hasError = derived(errorCounter, (values) => values > 0);
|
|
|
|
const remainingUploads = derived(
|
|
|
|
uploadAssets,
|
|
|
|
(values) => values.filter((a) => a.state === UploadState.PENDING || a.state === UploadState.STARTED).length,
|
|
|
|
);
|
2023-07-01 06:50:47 +02:00
|
|
|
|
|
|
|
const addNewUploadAsset = (newAsset: UploadAsset) => {
|
2024-05-02 23:26:13 +02:00
|
|
|
const assets = get(uploadAssets);
|
|
|
|
const duplicate = assets.find((asset) => asset.id === newAsset.id);
|
|
|
|
if (duplicate) {
|
|
|
|
uploadAssets.update((assets) => assets.map((asset) => (asset.id === newAsset.id ? newAsset : asset)));
|
|
|
|
} else {
|
|
|
|
totalUploadCounter.update((c) => c + 1);
|
|
|
|
uploadAssets.update((assets) => [
|
|
|
|
...assets,
|
|
|
|
{
|
|
|
|
...newAsset,
|
|
|
|
speed: 0,
|
|
|
|
state: UploadState.PENDING,
|
|
|
|
progress: 0,
|
|
|
|
eta: 0,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
2023-09-01 18:00:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const updateProgress = (id: string, loaded: number, total: number) => {
|
|
|
|
updateAssetMap(id, (v) => {
|
|
|
|
const uploadSpeed = v.startDate ? loaded / ((Date.now() - v.startDate) / 1000) : 0;
|
|
|
|
return {
|
|
|
|
...v,
|
|
|
|
progress: Math.floor((loaded / total) * 100),
|
|
|
|
speed: uploadSpeed,
|
|
|
|
eta: Math.ceil((total - loaded) / uploadSpeed),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const markStarted = (id: string) => {
|
|
|
|
updateAsset(id, {
|
|
|
|
state: UploadState.STARTED,
|
|
|
|
startDate: Date.now(),
|
|
|
|
});
|
2023-07-01 06:50:47 +02:00
|
|
|
};
|
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
const updateAssetMap = (id: string, mapper: (assets: UploadAsset) => UploadAsset) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
uploadAssets.update((uploadingAssets) => {
|
|
|
|
return uploadingAssets.map((asset) => {
|
|
|
|
if (asset.id == id) {
|
2023-09-01 18:00:51 +02:00
|
|
|
return mapper(asset);
|
2023-07-01 06:50:47 +02:00
|
|
|
}
|
|
|
|
return asset;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
const updateAsset = (id: string, partialObject: Partial<UploadAsset>) => {
|
|
|
|
updateAssetMap(id, (v) => ({ ...v, ...partialObject }));
|
2023-09-01 18:00:51 +02:00
|
|
|
};
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
const removeUploadAsset = (id: string) => {
|
|
|
|
uploadAssets.update((uploadingAsset) => uploadingAsset.filter((a) => a.id != id));
|
|
|
|
};
|
|
|
|
|
2023-09-01 18:00:51 +02:00
|
|
|
const dismissErrors = () => uploadAssets.update((value) => value.filter((e) => e.state !== UploadState.ERROR));
|
|
|
|
|
|
|
|
const resetStore = () => {
|
|
|
|
uploadAssets.set([]);
|
|
|
|
duplicateCounter.set(0);
|
|
|
|
successCounter.set(0);
|
|
|
|
totalUploadCounter.set(0);
|
|
|
|
};
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
return {
|
|
|
|
subscribe,
|
2023-08-10 15:38:29 +02:00
|
|
|
errorCounter,
|
2023-08-10 03:11:26 +02:00
|
|
|
duplicateCounter,
|
2023-09-01 18:00:51 +02:00
|
|
|
successCounter,
|
|
|
|
totalUploadCounter,
|
|
|
|
remainingUploads,
|
|
|
|
hasError,
|
|
|
|
dismissErrors,
|
2023-07-01 06:50:47 +02:00
|
|
|
isUploading,
|
2023-09-01 18:00:51 +02:00
|
|
|
resetStore,
|
2023-07-01 06:50:47 +02:00
|
|
|
addNewUploadAsset,
|
2023-09-01 18:00:51 +02:00
|
|
|
markStarted,
|
2023-07-01 06:50:47 +02:00
|
|
|
updateProgress,
|
2023-09-01 18:00:51 +02:00
|
|
|
updateAsset,
|
2023-07-01 06:50:47 +02:00
|
|
|
removeUploadAsset,
|
|
|
|
};
|
2022-06-19 15:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const uploadAssetsStore = createUploadStore();
|