2023-07-01 06:50:47 +02:00
|
|
|
import { derived, writable } from 'svelte/store';
|
2022-06-19 15:16:35 +02:00
|
|
|
import type { UploadAsset } from '../models/upload-asset';
|
|
|
|
|
|
|
|
function createUploadStore() {
|
2023-07-01 06:50:47 +02:00
|
|
|
const uploadAssets = writable<Array<UploadAsset>>([]);
|
|
|
|
|
|
|
|
const { subscribe } = uploadAssets;
|
|
|
|
|
|
|
|
const isUploading = derived(uploadAssets, ($uploadAssets) => {
|
|
|
|
return $uploadAssets.length > 0 ? true : false;
|
|
|
|
});
|
|
|
|
|
|
|
|
const addNewUploadAsset = (newAsset: UploadAsset) => {
|
|
|
|
uploadAssets.update((currentSet) => [...currentSet, newAsset]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateProgress = (id: string, progress: number) => {
|
|
|
|
uploadAssets.update((uploadingAssets) => {
|
|
|
|
return uploadingAssets.map((asset) => {
|
|
|
|
if (asset.id == id) {
|
|
|
|
return {
|
|
|
|
...asset,
|
|
|
|
progress: progress,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return asset;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeUploadAsset = (id: string) => {
|
|
|
|
uploadAssets.update((uploadingAsset) => uploadingAsset.filter((a) => a.id != id));
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
isUploading,
|
|
|
|
addNewUploadAsset,
|
|
|
|
updateProgress,
|
|
|
|
removeUploadAsset,
|
|
|
|
};
|
2022-06-19 15:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const uploadAssetsStore = createUploadStore();
|