Skip to content

Commit

Permalink
feat: cache episodes in feed
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jul 8, 2022
1 parent c2cc5ca commit 138de8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export const isPaused = writable<boolean>(true);
export const playedEpisodes = writable<{
[key: string]: PlayedEpisode;
}>({});
export const savedFeeds = writable<{[podcastName: string]: PodcastFeed}>({});
export const savedFeeds = writable<{[podcastName: string]: PodcastFeed}>({});

export const episodeCache = writable<{[podcastName: string]: Episode[]}>({});
28 changes: 22 additions & 6 deletions src/ui/PodcastView/PodcastView.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { PodcastFeed } from "src/types/PodcastFeed";
import FeedGrid from "./PodcastGrid.svelte";
import { currentEpisode, savedFeeds } from "src/store";
import { currentEpisode, savedFeeds, episodeCache } from "src/store";
import EpisodePlayer from "./EpisodePlayer.svelte";
import EpisodeList from "./EpisodeList.svelte";
import { Episode } from "src/types/Episode";
Expand All @@ -20,13 +20,26 @@
feeds = Object.values(storeValue);
});
function handleclickPodcast(event: CustomEvent<{ feed: PodcastFeed }>) {
async function fetchEpisodes(feed: PodcastFeed): Promise<Episode[]> {
return await (new FeedParser(feed).parse(feed.url));
}
function handleClickPodcast(event: CustomEvent<{ feed: PodcastFeed }>) {
episodeList = [];
const { feed } = event.detail;
selectedFeed = feed;
new FeedParser(feed).parse(feed.url).then((episodes) => {
episodeList = episodes;
});
const cachedEpisodesInFeed = $episodeCache[feed.title];
if (cachedEpisodesInFeed && cachedEpisodesInFeed.length > 0) {
episodeList = cachedEpisodesInFeed;
} else {
fetchEpisodes(feed).then(episodes => {
episodeList = episodes;
episodeCache.update(cache => ({ ...cache, [feed.title]: episodes }));
});
}
viewState = ViewState.EpisodeList;
}
Expand Down Expand Up @@ -57,7 +70,10 @@
on:clickEpisode={handleClickEpisode}
/>
{:else if viewState === ViewState.PodcastGrid}
<FeedGrid feeds={feeds} on:clickPodcast={handleclickPodcast} />
<FeedGrid
feeds={feeds}
on:clickPodcast={handleClickPodcast}
/>
{/if}
</div>

Expand Down

0 comments on commit 138de8d

Please sign in to comment.