improves handling of user data

This commit is contained in:
Ollie Taylor
2023-02-25 18:25:59 +00:00
parent 4be36ba047
commit 7cad6ee1d6
23 changed files with 154 additions and 226 deletions
+27
View File
@@ -0,0 +1,27 @@
import { episode_audio, episode_progress } from '$lib/audio';
import type { UserProgress } from '$lib/types';
import { get_pathname_from_url } from '$lib/utility/get-pathname-from-url';
import { info } from '$lib/utility/package/log';
import { persisted } from 'svelte-local-storage-store';
import { get } from 'svelte/store';
const _default_user_progress = {} satisfies UserProgress;
const _user_progress = persisted<UserProgress>('USER_PROGRESS', _default_user_progress);
const save = () => {
const audio = get(episode_audio);
if (!audio?.src) return;
info('saving progress: ', audio.src);
const pathname = get_pathname_from_url(audio.src);
const current_time = get(episode_progress).current_time;
_user_progress.update((prev) => ({ ...prev, [pathname]: current_time }));
};
export const user_progress = {
subscribe: _user_progress.subscribe,
get: (src: string) => get(_user_progress)[get_pathname_from_url(src)],
save,
clear: () => _user_progress.set(_default_user_progress),
};