Spaces:
Running
Running
| import { writable } from "svelte/store"; | |
| interface LoadingState { | |
| isLoading: boolean; | |
| progress: number; | |
| } | |
| function createLoadingStore() { | |
| const { subscribe, set, update } = writable<LoadingState>({ | |
| isLoading: true, | |
| progress: 0, | |
| }); | |
| let progressInterval: number | null = null; | |
| return { | |
| subscribe, | |
| setProgress: (progress: number) => { | |
| update((state) => ({ | |
| ...state, | |
| progress: Math.min(100, Math.max(0, progress)), | |
| })); | |
| }, | |
| startLoading: () => { | |
| set({ isLoading: true, progress: 0 }); | |
| let currentProgress = 0; | |
| progressInterval = window.setInterval(() => { | |
| currentProgress += Math.random() * 15; | |
| if (currentProgress >= 90) { | |
| if (progressInterval) { | |
| clearInterval(progressInterval); | |
| progressInterval = null; | |
| } | |
| currentProgress = 90; | |
| } | |
| update((state) => ({ ...state, progress: currentProgress })); | |
| }, 200); | |
| }, | |
| finishLoading: () => { | |
| if (progressInterval) { | |
| clearInterval(progressInterval); | |
| progressInterval = null; | |
| } | |
| update((state) => ({ ...state, progress: 100 })); | |
| setTimeout(() => { | |
| set({ isLoading: false, progress: 100 }); | |
| }, 300); | |
| }, | |
| }; | |
| } | |
| export const loadingStore = createLoadingStore(); | |