import { tweened } from 'svelte/motion'; import { derived, get, type Readable, type Unsubscriber, writable, } from 'svelte/store'; export interface ProgressOptions { totalDuration: number; isPaused: Readable; onComplete?: () => unknown; } export interface PausableProgress { progress: Readable; unsubscribe: Unsubscriber; } export const pausableProgress = ({ totalDuration, isPaused, onComplete, }: ProgressOptions): PausableProgress => { const isPauseSet = writable(true); const progress = tweened(1, { duration: 0 }); const remainingDuration = derived(progress, ($progress) => Math.max($progress * totalDuration, 0) ); const pause = () => { if (!get(isPauseSet)) { isPauseSet.set(true); const currentProgress = get(progress); void progress.set(currentProgress, { duration: 0 }); } }; const resume = () => { if (get(isPauseSet)) { isPauseSet.set(false); const duration = get(remainingDuration); void progress.set(0, { duration }); } }; const unsubscribeFromIsPaused = isPaused.subscribe(($isPaused) => { if ($isPaused) { pause(); } else { resume(); } }); const unsubscribeFromProgress = progress.subscribe(($progress) => { if ($progress === 0) { onComplete?.(); unsubscribe(); } }); const unsubscribe = () => { unsubscribeFromProgress(); unsubscribeFromIsPaused(); }; return { progress, unsubscribe }; };