|
1 | | -import { get, writable } from "svelte/store"; |
| 1 | +import { get, readable, writable } from "svelte/store"; |
2 | 2 | import type PodNotes from "src/main"; |
3 | 3 | import type { Episode } from "src/types/Episode"; |
4 | 4 | import type { PlayedEpisode } from "src/types/PlayedEpisode"; |
@@ -102,6 +102,199 @@ export const savedFeeds = writable<{ [podcastName: string]: PodcastFeed }>({}); |
102 | 102 |
|
103 | 103 | export const episodeCache = writable<{ [podcastName: string]: Episode[] }>({}); |
104 | 104 |
|
| 105 | +const LATEST_EPISODES_PER_FEED = 10; |
| 106 | + |
| 107 | +type LatestEpisodesByFeed = Map<string, Episode[]>; |
| 108 | +type FeedEpisodeSources = Map<string, Episode[]>; |
| 109 | +type LatestEpisodePointer = { |
| 110 | + feedTitle: string; |
| 111 | + index: number; |
| 112 | + episode: Episode; |
| 113 | +}; |
| 114 | + |
| 115 | +function getEpisodeTimestamp(episode?: Episode): number { |
| 116 | + if (!episode?.episodeDate) return 0; |
| 117 | + |
| 118 | + return Number(episode.episodeDate); |
| 119 | +} |
| 120 | + |
| 121 | +function getLatestEpisodesForFeed(episodes: Episode[]): Episode[] { |
| 122 | + if (!episodes?.length) return []; |
| 123 | + |
| 124 | + return episodes |
| 125 | + .slice(0, LATEST_EPISODES_PER_FEED) |
| 126 | + .sort((a, b) => getEpisodeTimestamp(b) - getEpisodeTimestamp(a)); |
| 127 | +} |
| 128 | + |
| 129 | +function shallowEqualEpisodes(a?: Episode[], b?: Episode[]): boolean { |
| 130 | + if (!a || !b || a.length !== b.length) return false; |
| 131 | + |
| 132 | + for (let i = 0; i < a.length; i += 1) { |
| 133 | + if (a[i] !== b[i]) return false; |
| 134 | + } |
| 135 | + |
| 136 | + return true; |
| 137 | +} |
| 138 | + |
| 139 | +function pushEpisodePointer( |
| 140 | + heap: LatestEpisodePointer[], |
| 141 | + pointer: LatestEpisodePointer, |
| 142 | +): void { |
| 143 | + heap.push(pointer); |
| 144 | + let idx = heap.length - 1; |
| 145 | + |
| 146 | + while (idx > 0) { |
| 147 | + const parent = Math.floor((idx - 1) / 2); |
| 148 | + if ( |
| 149 | + getEpisodeTimestamp(heap[parent].episode) >= |
| 150 | + getEpisodeTimestamp(heap[idx].episode) |
| 151 | + ) { |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + heap[idx] = heap[parent]; |
| 156 | + heap[parent] = pointer; |
| 157 | + idx = parent; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +function popEpisodePointer( |
| 162 | + heap: LatestEpisodePointer[], |
| 163 | +): LatestEpisodePointer | undefined { |
| 164 | + if (heap.length === 0) return undefined; |
| 165 | + |
| 166 | + const top = heap[0]; |
| 167 | + const last = heap.pop(); |
| 168 | + |
| 169 | + if (last && heap.length > 0) { |
| 170 | + heap[0] = last; |
| 171 | + let idx = 0; |
| 172 | + |
| 173 | + while (true) { |
| 174 | + const left = idx * 2 + 1; |
| 175 | + const right = idx * 2 + 2; |
| 176 | + let largest = idx; |
| 177 | + |
| 178 | + if ( |
| 179 | + left < heap.length && |
| 180 | + getEpisodeTimestamp(heap[left].episode) > |
| 181 | + getEpisodeTimestamp(heap[largest].episode) |
| 182 | + ) { |
| 183 | + largest = left; |
| 184 | + } |
| 185 | + |
| 186 | + if ( |
| 187 | + right < heap.length && |
| 188 | + getEpisodeTimestamp(heap[right].episode) > |
| 189 | + getEpisodeTimestamp(heap[largest].episode) |
| 190 | + ) { |
| 191 | + largest = right; |
| 192 | + } |
| 193 | + |
| 194 | + if (largest === idx) break; |
| 195 | + |
| 196 | + const temp = heap[idx]; |
| 197 | + heap[idx] = heap[largest]; |
| 198 | + heap[largest] = temp; |
| 199 | + idx = largest; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return top; |
| 204 | +} |
| 205 | + |
| 206 | +// Use a max-heap to merge the latest episodes from each feed without |
| 207 | +// resorting the entire cache every time a single feed updates. |
| 208 | +function mergeLatestEpisodes(latestByFeed: LatestEpisodesByFeed): Episode[] { |
| 209 | + const heap: LatestEpisodePointer[] = []; |
| 210 | + |
| 211 | + for (const [feedTitle, episodes] of latestByFeed.entries()) { |
| 212 | + if (!episodes.length) continue; |
| 213 | + |
| 214 | + pushEpisodePointer(heap, { |
| 215 | + feedTitle, |
| 216 | + index: 0, |
| 217 | + episode: episodes[0], |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | + const merged: Episode[] = []; |
| 222 | + while (heap.length > 0) { |
| 223 | + const pointer = popEpisodePointer(heap); |
| 224 | + if (!pointer) break; |
| 225 | + |
| 226 | + merged.push(pointer.episode); |
| 227 | + |
| 228 | + const feedEpisodes = latestByFeed.get(pointer.feedTitle); |
| 229 | + const nextIndex = pointer.index + 1; |
| 230 | + if (feedEpisodes && nextIndex < feedEpisodes.length) { |
| 231 | + pushEpisodePointer(heap, { |
| 232 | + feedTitle: pointer.feedTitle, |
| 233 | + index: nextIndex, |
| 234 | + episode: feedEpisodes[nextIndex], |
| 235 | + }); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return merged; |
| 240 | +} |
| 241 | + |
| 242 | +export const latestEpisodes = readable<Episode[]>([], (set) => { |
| 243 | + let latestByFeed: LatestEpisodesByFeed = new Map(); |
| 244 | + let feedSources: FeedEpisodeSources = new Map(); |
| 245 | + |
| 246 | + const unsubscribe = episodeCache.subscribe((cache) => { |
| 247 | + let changed = false; |
| 248 | + const nextSources: FeedEpisodeSources = new Map(); |
| 249 | + const nextLatestByFeed: LatestEpisodesByFeed = new Map(); |
| 250 | + |
| 251 | + for (const [feedTitle, episodes] of Object.entries(cache)) { |
| 252 | + nextSources.set(feedTitle, episodes); |
| 253 | + const previousSource = feedSources.get(feedTitle); |
| 254 | + const previousLatest = latestByFeed.get(feedTitle); |
| 255 | + |
| 256 | + if (previousSource === episodes && previousLatest) { |
| 257 | + nextLatestByFeed.set(feedTitle, previousLatest); |
| 258 | + continue; |
| 259 | + } |
| 260 | + |
| 261 | + const latestForFeed = getLatestEpisodesForFeed(episodes); |
| 262 | + nextLatestByFeed.set(feedTitle, latestForFeed); |
| 263 | + |
| 264 | + if (!changed) { |
| 265 | + changed = |
| 266 | + !previousLatest || |
| 267 | + !shallowEqualEpisodes(previousLatest, latestForFeed); |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + if (!changed) { |
| 272 | + for (const feedTitle of feedSources.keys()) { |
| 273 | + if (!nextSources.has(feedTitle)) { |
| 274 | + changed = true; |
| 275 | + break; |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + feedSources = nextSources; |
| 281 | + |
| 282 | + if (!changed && nextLatestByFeed.size === latestByFeed.size) { |
| 283 | + latestByFeed = nextLatestByFeed; |
| 284 | + return; |
| 285 | + } |
| 286 | + |
| 287 | + latestByFeed = nextLatestByFeed; |
| 288 | + set(mergeLatestEpisodes(latestByFeed)); |
| 289 | + }); |
| 290 | + |
| 291 | + return () => { |
| 292 | + latestByFeed.clear(); |
| 293 | + feedSources.clear(); |
| 294 | + unsubscribe(); |
| 295 | + }; |
| 296 | +}); |
| 297 | + |
105 | 298 | export const downloadedEpisodes = (() => { |
106 | 299 | const store = writable<{ [podcastName: string]: DownloadedEpisode[] }>({}); |
107 | 300 | const { subscribe, update, set } = store; |
|
0 commit comments