From 34f708b1fa5cbbb234bab6dcebfa4c86a50e656f Mon Sep 17 00:00:00 2001 From: Ollie Taylor <13766232+OllieJT@users.noreply.github.com> Date: Fri, 24 Feb 2023 23:27:04 +0000 Subject: [PATCH 01/13] restructures stores and removes combined derived store --- src/lib/context/audio-metadata.ts | 4 -- src/lib/context/audio.ts | 89 ++++------------------------- src/lib/context/episode-progress.ts | 14 ++--- src/lib/index.ts | 1 + src/lib/stores.ts | 39 +++++++++++++ 5 files changed, 59 insertions(+), 88 deletions(-) delete mode 100644 src/lib/context/audio-metadata.ts create mode 100644 src/lib/stores.ts diff --git a/src/lib/context/audio-metadata.ts b/src/lib/context/audio-metadata.ts deleted file mode 100644 index 8fd43b1..0000000 --- a/src/lib/context/audio-metadata.ts +++ /dev/null @@ -1,4 +0,0 @@ -import type { AudioMetadata } from '$lib/types'; -import { writable } from 'svelte/store'; - -export const audio_metadata = writable(null); diff --git a/src/lib/context/audio.ts b/src/lib/context/audio.ts index 6a7f22f..8740ee3 100644 --- a/src/lib/context/audio.ts +++ b/src/lib/context/audio.ts @@ -1,72 +1,19 @@ import { audio_autoplay, - audio_current_time, - audio_duration, audio_element, - audio_ended, - audio_loading, audio_muted, - audio_paused, audio_src, audio_start_at, } from '$lib/context/audio-internals'; -import { audio_metadata } from '$lib/context/audio-metadata'; import { episode_progress } from '$lib/context/episode-progress'; -import { user_preferences } from '$lib/context/user-preferences'; +import { metadata } from '$lib/stores'; import type { AudioLoadData, AudioLoadOptions } from '$lib/types'; -import { secondsToTimestamp } from '$lib/utility/seconds-to-timestamp'; import { info, warn } from '$pkg/log'; import clamp from 'just-clamp'; -import { derived } from 'svelte/store'; - -const audio_state = derived( - [ - audio_current_time, - audio_duration, - audio_ended, - audio_loading, - audio_paused, - audio_start_at, - audio_autoplay, - audio_muted, - audio_src, - audio_metadata, - user_preferences, - ], - ([ - $current_time, - $duration, - $ended, - $loading, - $paused, - $start_at, - $autoplay, - $muted, - $src, - $metadata, - $user_preferences, - ]) => { - return { - current_time: $current_time, - duration: $duration, - ended: $ended, - loading: $loading, - paused: $paused, - start_at: $start_at, - autoplay: $autoplay, - muted: $muted, - src: $src, - metadata: $metadata, - playback_rate: $user_preferences.playback_rate, - volume: $user_preferences.volume, - timestamp: secondsToTimestamp($current_time), - }; - }, -); type HandleType = 'toggle' | 'set'; -function play(type: HandleType = 'set') { +export function play(type: HandleType = 'set') { info('play: ', type); return audio_element.subscribe((el) => { if (!el) return warn('no audio element'); @@ -77,7 +24,7 @@ function play(type: HandleType = 'set') { } })(); } -function pause(type: HandleType = 'set') { +export function pause(type: HandleType = 'set') { info('pause: ', type); return audio_element.subscribe((el) => { if (!el) return warn('no audio element'); @@ -89,7 +36,7 @@ function pause(type: HandleType = 'set') { })(); } -function mute(type: HandleType = 'set') { +export function mute(type: HandleType = 'set') { info('mute: ', type); if (type === 'toggle') { audio_muted.update((muted) => !muted); @@ -97,7 +44,7 @@ function mute(type: HandleType = 'set') { audio_muted.set(true); } } -function unmute(type: HandleType = 'set') { +export function unmute(type: HandleType = 'set') { info('unmute: ', type); if (type === 'toggle') { audio_muted.update((muted) => !muted); @@ -106,10 +53,10 @@ function unmute(type: HandleType = 'set') { } } -const load = (data: AudioLoadData, opts: AudioLoadOptions) => { +export const load = (data: AudioLoadData, opts: AudioLoadOptions) => { episode_progress.stash(); - const { src, ...metadata } = data; + const { src, ...meta } = data; const progress = episode_progress.use(src); const start_at = progress?.start_at || opts.start_at || 0; @@ -118,22 +65,22 @@ const load = (data: AudioLoadData, opts: AudioLoadOptions) => { info('load: ', src); audio_autoplay.set(opts.autoplay); audio_src.set(src); - audio_metadata.set(metadata); + metadata.set(meta); audio_start_at.set(start_at); // opts.current_time ? seek(opts.current_time) : null; //opts.autoplay && play(); }; -function unload() { +export function unload() { episode_progress.stash(); info('unload: '); pause(); audio_autoplay.set(false); audio_src.set(''); - audio_metadata.set(null); + metadata.set(null); audio_start_at.set(0); } -function seek(seconds: number, from: 'from-start' | 'from-end' = 'from-start') { +export function seek(seconds: number, from: 'from-start' | 'from-end' = 'from-start') { info('seek: ', seconds, from); return audio_element.subscribe((el) => { if (!el) return warn('no audio element'); @@ -145,7 +92,7 @@ function seek(seconds: number, from: 'from-start' | 'from-end' = 'from-start') { })(); } -function skip(seconds: number, type: 'forward' | 'backward' = 'forward') { +export function skip(seconds: number, type: 'forward' | 'backward' = 'forward') { info('skip: ', seconds, type); return audio_element.subscribe((el) => { if (!el) return warn('no audio element'); @@ -156,15 +103,3 @@ function skip(seconds: number, type: 'forward' | 'backward' = 'forward') { } })(); } - -export const audio = { - subscribe: audio_state.subscribe, - play, - pause, - mute, - unmute, - load, - unload, - seek, - skip, -}; diff --git a/src/lib/context/episode-progress.ts b/src/lib/context/episode-progress.ts index 7bdf2ff..fce8bbb 100644 --- a/src/lib/context/episode-progress.ts +++ b/src/lib/context/episode-progress.ts @@ -1,17 +1,17 @@ import { browser } from '$app/environment'; -import { audio } from '$lib/context/audio'; +import { audio_current_time, audio_src } from '$lib/context/audio-internals'; import type { AudioLoadOptions, EpisodeProgress } from '$lib/types'; import { error, info, warn } from '$lib/utility/package/log'; +import { get } from 'svelte/store'; const episode_progress_map = new Map(); function stash_episode() { - return audio.subscribe((state) => { - info('saving progress: ', state.src); - if (!state.src) return; - episode_progress_map.set(state.src, { current_time: state.current_time }); - save_all(); - })(); + const src = get(audio_src); + if (!src) return; + info('saving progress: ', src); + episode_progress_map.set(src, { current_time: get(audio_current_time) }); + save_all(); } function use_episode(src: string): Pick | null { diff --git a/src/lib/index.ts b/src/lib/index.ts index 4b9d6c9..94d0b89 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -2,4 +2,5 @@ export { default as AudioLoader } from '$lib/components/audio-loader.svelte'; export * from '$lib/context/audio'; export * from '$lib/context/episode-progress'; export * from '$lib/context/user-preferences'; +export * from '$lib/stores'; export * from '$lib/utility'; diff --git a/src/lib/stores.ts b/src/lib/stores.ts new file mode 100644 index 0000000..10f1156 --- /dev/null +++ b/src/lib/stores.ts @@ -0,0 +1,39 @@ +import { + audio_autoplay, + audio_current_time, + audio_duration, + audio_ended, + audio_loading, + audio_muted, + audio_paused, + audio_src, + audio_start_at, +} from '$lib/context/audio-internals'; +import { user_preferences } from '$lib/context/user-preferences'; +import type { AudioMetadata } from '$lib/types'; +import { secondsToTimestamp } from '$lib/utility/seconds-to-timestamp'; +import { derived, writable } from 'svelte/store'; + +export const time = derived([audio_current_time, audio_ended], ([$seconds, $ended]) => ({ + current_time: $seconds, + timestamp: secondsToTimestamp($seconds), + ended: $ended, +})); + +export const metadata = writable(null); + +export const data = derived([audio_src, metadata], ([$src, $metadata]) => ({ + ...$metadata, + src: $src, +})); + +export const options = derived([audio_start_at, audio_autoplay], ([$start_at, $autoplay]) => ({ + start_at: $start_at, + autoplay: $autoplay, +})); + +export const duration = { subscribe: audio_duration.subscribe }; +export const loading = { subscribe: audio_loading.subscribe }; +export const paused = { subscribe: audio_paused.subscribe }; +export const muted = { subscribe: audio_muted.subscribe }; +export const references = { subscribe: user_preferences.subscribe }; From b73cfe49bc8ae85f56db411365e883ca3fdadcfb Mon Sep 17 00:00:00 2001 From: Ollie Taylor <13766232+OllieJT@users.noreply.github.com> Date: Fri, 24 Feb 2023 23:29:41 +0000 Subject: [PATCH 02/13] removes requirements for AudioMetadata --- src/lib/stores.ts | 26 +++++++++++++++++--------- src/lib/types.ts | 4 ++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/lib/stores.ts b/src/lib/stores.ts index 10f1156..1ffcef3 100644 --- a/src/lib/stores.ts +++ b/src/lib/stores.ts @@ -10,7 +10,7 @@ import { audio_start_at, } from '$lib/context/audio-internals'; import { user_preferences } from '$lib/context/user-preferences'; -import type { AudioMetadata } from '$lib/types'; +import type { AudioLoadData, AudioLoadOptions, AudioMetadata } from '$lib/types'; import { secondsToTimestamp } from '$lib/utility/seconds-to-timestamp'; import { derived, writable } from 'svelte/store'; @@ -22,15 +22,23 @@ export const time = derived([audio_current_time, audio_ended], ([$seconds, $ende export const metadata = writable(null); -export const data = derived([audio_src, metadata], ([$src, $metadata]) => ({ - ...$metadata, - src: $src, -})); +export const data = derived( + [audio_src, metadata], + ([$src, $metadata]) => + ({ + ...$metadata, + src: $src, + } satisfies AudioLoadData), +); -export const options = derived([audio_start_at, audio_autoplay], ([$start_at, $autoplay]) => ({ - start_at: $start_at, - autoplay: $autoplay, -})); +export const options = derived( + [audio_start_at, audio_autoplay], + ([$start_at, $autoplay]) => + ({ + start_at: $start_at, + autoplay: $autoplay, + } satisfies AudioLoadOptions), +); export const duration = { subscribe: audio_duration.subscribe }; export const loading = { subscribe: audio_loading.subscribe }; diff --git a/src/lib/types.ts b/src/lib/types.ts index 1dfad20..e40f61c 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,9 +1,9 @@ export type AudioPlayerElement = HTMLAudioElement | undefined; -export interface AudioMetadata { +export type AudioMetadata = Partial<{ title: string; artwork: string; -} +}>; export interface AudioLoadData extends AudioMetadata { src: string; From 6c849b7a60abf2804d94824cfc50779b0e1109ba Mon Sep 17 00:00:00 2001 From: Ollie Taylor <13766232+OllieJT@users.noreply.github.com> Date: Fri, 24 Feb 2023 23:31:43 +0000 Subject: [PATCH 03/13] add changeset --- .changeset/violet-meals-worry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/violet-meals-worry.md diff --git a/.changeset/violet-meals-worry.md b/.changeset/violet-meals-worry.md new file mode 100644 index 0000000..c6fd62a --- /dev/null +++ b/.changeset/violet-meals-worry.md @@ -0,0 +1,5 @@ +--- +'svelte-podcast': patch +--- + +Fixes derived store causing exessive updates From 0b84bc65f9b76db5fbebe01642f2725a2f4eec11 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 24 Feb 2023 23:40:28 +0000 Subject: [PATCH 04/13] Version Packages --- .changeset/violet-meals-worry.md | 5 ----- CHANGELOG.md | 6 ++++++ package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/violet-meals-worry.md diff --git a/.changeset/violet-meals-worry.md b/.changeset/violet-meals-worry.md deleted file mode 100644 index c6fd62a..0000000 --- a/.changeset/violet-meals-worry.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'svelte-podcast': patch ---- - -Fixes derived store causing exessive updates diff --git a/CHANGELOG.md b/CHANGELOG.md index d3c739c..03e7da6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # svelte-podcast +## 0.3.2 + +### Patch Changes + +- 6c849b7: Fixes derived store causing exessive updates + ## 0.3.1 ### Patch Changes diff --git a/package.json b/package.json index f4a9ffb..eef403e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte-podcast", - "version": "0.3.1", + "version": "0.3.2", "license": "MIT", "author": "Ollie Taylor", "homepage": "https://github.com/OllieJT/svelte-podcast/blob/main/README.md", From 03e95aa81b114f74a000b5ede32abcbb14308d71 Mon Sep 17 00:00:00 2001 From: Ollie Taylor <13766232+OllieJT@users.noreply.github.com> Date: Fri, 24 Feb 2023 23:45:25 +0000 Subject: [PATCH 05/13] Update .eslintignore --- .eslintignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintignore b/.eslintignore index 3897265..1d4b507 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,6 +3,7 @@ node_modules /build /.svelte-kit /package +/dist .env .env.* !.env.example From 198cdfd3e0dbedad96cb290d6c37758345beac82 Mon Sep 17 00:00:00 2001 From: Ollie Taylor <13766232+OllieJT@users.noreply.github.com> Date: Sat, 25 Feb 2023 01:04:08 +0000 Subject: [PATCH 06/13] simplify exports --- .github/workflows/main.yml | 6 +- src/lib/audio/_private.ts | 17 ++ .../{components => audio}/audio-loader.svelte | 56 +++---- src/lib/audio/index.ts | 147 ++++++++++++++++++ src/lib/context/audio-internals.ts | 16 -- src/lib/context/audio.ts | 105 ------------- src/lib/context/episode-progress.ts | 87 ----------- src/lib/context/user-preferences.ts | 66 -------- src/lib/index.ts | 8 +- src/lib/preferences/_private.ts | 9 ++ src/lib/preferences/index.ts | 54 +++++++ src/lib/progress/_private.ts | 1 + src/lib/progress/index.ts | 81 ++++++++++ src/lib/stores.ts | 47 ------ src/lib/types.ts | 4 - src/lib/utility/use-state.ts | 12 +- src/routes/+page.svelte | 70 ++++++--- src/routes/another-page/+page.svelte | 29 +++- 18 files changed, 422 insertions(+), 393 deletions(-) create mode 100644 src/lib/audio/_private.ts rename src/lib/{components => audio}/audio-loader.svelte (60%) create mode 100644 src/lib/audio/index.ts delete mode 100644 src/lib/context/audio-internals.ts delete mode 100644 src/lib/context/audio.ts delete mode 100644 src/lib/context/episode-progress.ts delete mode 100644 src/lib/context/user-preferences.ts create mode 100644 src/lib/preferences/_private.ts create mode 100644 src/lib/preferences/index.ts create mode 100644 src/lib/progress/_private.ts create mode 100644 src/lib/progress/index.ts delete mode 100644 src/lib/stores.ts diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1a1ead7..f6d6dbf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,8 +1,8 @@ name: 'CI' on: - push: - branches-ignore: - - 'main' + pull_request: + branches: + - '**' jobs: build: runs-on: ubuntu-latest diff --git a/src/lib/audio/_private.ts b/src/lib/audio/_private.ts new file mode 100644 index 0000000..efbb9e2 --- /dev/null +++ b/src/lib/audio/_private.ts @@ -0,0 +1,17 @@ +import type { AudioMetadata, AudioPlayerElement } from '$lib/types'; +import { writable } from 'svelte/store'; + +// readonly - stores will update when bindings change +export const _current_time = writable(0); +export const _element = writable(); +export const _duration = writable(0); +export const _ended = writable(false); +export const _loading = writable(false); +export const _paused = writable(true); + +// handlers - when store value changes, the binding will update the audio element +export const _start_at = writable(0); +export const _autoplay = writable(false); +export const _muted = writable(false); +export const _src = writable(''); +export const _metadata = writable(null); diff --git a/src/lib/components/audio-loader.svelte b/src/lib/audio/audio-loader.svelte similarity index 60% rename from src/lib/components/audio-loader.svelte rename to src/lib/audio/audio-loader.svelte index b4228fa..ba26eb2 100644 --- a/src/lib/components/audio-loader.svelte +++ b/src/lib/audio/audio-loader.svelte @@ -1,53 +1,53 @@