Skip to content

Commit d7d0906

Browse files
committed
fix(panel): simplify show fetching
1 parent efd6e1f commit d7d0906

File tree

2 files changed

+69
-98
lines changed

2 files changed

+69
-98
lines changed

src/components/views/panel/ShowPanel.vue

+44-63
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
<script setup lang="ts">
22
import { NFlex, NSkeleton } from 'naive-ui';
3-
import { computed, onMounted, onUnmounted, ref, toRefs, watch } from 'vue';
4-
5-
import type {
6-
TraktEpisodeExtended,
7-
TraktEpisodeShort,
8-
} from '~/models/trakt/trakt-episode.model';
9-
import type { TraktShowExtended } from '~/models/trakt/trakt-show.model';
3+
import { computed, onMounted, toRefs, watch } from 'vue';
104
115
import TitleLink from '~/components/common/buttons/TitleLink.vue';
126
import PanelPoster from '~/components/views/panel/PanelPoster.vue';
@@ -16,7 +10,7 @@ import ShowPanelOverview from '~/components/views/panel/ShowPanelOverview.vue';
1610
import ShowPanelPicker from '~/components/views/panel/ShowPanelPicker.vue';
1711
import { ResolveExternalLinks } from '~/settings/external.links';
1812
import { useListsStoreRefs, useListStore } from '~/stores/data/list.store';
19-
import { type ShowSeasons, useShowStore } from '~/stores/data/show.store';
13+
import { useShowStore } from '~/stores/data/show.store';
2014
import { useExtensionSettingsStore } from '~/stores/settings/extension.store';
2115
import { useI18n } from '~/utils';
2216
import { deCapitalise } from '~/utils/string.utils';
@@ -36,14 +30,17 @@ const props = defineProps({
3630
},
3731
});
3832
39-
const show = ref<TraktShowExtended>();
40-
const seasons = ref<ShowSeasons>();
41-
const episodes = ref<TraktEpisodeShort[]>();
42-
const episode = ref<TraktEpisodeExtended>();
43-
4433
const { showId, seasonNumber, episodeNumber } = toRefs(props);
4534
4635
const {
36+
getShow,
37+
fetchShow,
38+
getShowSeasons,
39+
fetchShowSeasons,
40+
getShowSeasonEpisodes,
41+
fetchShowSeasonEpisodes,
42+
getShowEpisode,
43+
fetchShowEpisode,
4744
getShowWatchedProgress,
4845
getShowCollectionProgress,
4946
getShowProgressLoading,
@@ -92,6 +89,31 @@ const panelType = computed<'show' | 'season' | 'episode'>(() => {
9289
return 'show';
9390
});
9491
92+
const show = computed(() => {
93+
if (!showId?.value) return;
94+
return getShow(showId.value).value;
95+
});
96+
97+
const seasons = computed(() => {
98+
if (!showId?.value) return;
99+
return getShowSeasons(showId.value).value;
100+
});
101+
102+
const episodes = computed(() => {
103+
if (!showId?.value || seasonNb?.value === undefined) return;
104+
return getShowSeasonEpisodes(showId.value, seasonNb.value).value;
105+
});
106+
107+
const episode = computed(() => {
108+
if (!showId?.value || seasonNb?.value === undefined || episodeNb?.value === undefined)
109+
return;
110+
return getShowEpisode({
111+
id: showId.value,
112+
season: seasonNb.value,
113+
episode: episodeNb.value,
114+
}).value;
115+
});
116+
95117
const season = computed(() => {
96118
if (seasonNb?.value === undefined) return;
97119
return seasons.value?.[seasonNb.value];
@@ -162,66 +184,25 @@ const titleUrl = computed(() => {
162184
});
163185
});
164186
165-
const subscriptions = new Set<() => void>();
166-
167-
const { getShowRef, getShowSeasonsRef, getShowSeasonEpisodesRef, getShowEpisodeRef } =
168-
useShowStore();
169-
170-
const watchData = () =>
187+
onMounted(() => {
171188
watch(
172189
[showId, seasonNb, episodeNb],
173-
(next, prev) => {
174-
// show changes
175-
if (next.at(0) !== prev?.at(0)) {
176-
show.value = undefined;
177-
seasons.value = undefined;
178-
episodes.value = undefined;
179-
episode.value = undefined;
180-
}
181-
// season changes
182-
else if (next.at(1) !== prev?.at(1)) {
183-
episode.value = undefined;
184-
episodes.value = undefined;
185-
}
186-
// episode changes
187-
else if (next.at(2) !== prev?.at(2)) {
188-
episode.value = undefined;
189-
}
190+
([_showId, _seasonNb, _episodeNb]) => {
191+
if (_showId !== undefined) {
192+
fetchShow(_showId);
193+
fetchShowSeasons(_showId);
190194
191-
if (showId?.value) {
192-
subscriptions.add(getShowRef(showId.value, show).unsub);
193-
subscriptions.add(getShowSeasonsRef(showId.value, seasons).unsub);
194-
195-
if (seasonNb?.value !== undefined) {
196-
subscriptions.add(
197-
getShowSeasonEpisodesRef(showId.value, seasonNb?.value, episodes).unsub,
198-
);
195+
if (_seasonNb !== undefined) {
196+
fetchShowSeasonEpisodes(_showId, _seasonNb);
199197
}
200198
201-
if (seasonNb?.value !== undefined && episodeNb?.value !== undefined) {
202-
subscriptions.add(
203-
getShowEpisodeRef(
204-
{
205-
id: showId.value,
206-
season: seasonNb.value,
207-
episode: episodeNb.value,
208-
},
209-
episode,
210-
).unsub,
211-
);
199+
if (_seasonNb !== undefined && _episodeNb !== undefined) {
200+
fetchShowEpisode(_showId, _seasonNb, _episodeNb);
212201
}
213202
}
214203
},
215204
{ immediate: true },
216205
);
217-
218-
onMounted(() => {
219-
subscriptions.add(watchData());
220-
});
221-
222-
onUnmounted(() => {
223-
subscriptions.forEach(unsub => unsub());
224-
subscriptions.clear();
225206
});
226207
227208
const { openTab } = useExtensionSettingsStore();

src/stores/data/show.store.ts

+25-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineStore, storeToRefs } from 'pinia';
22

3-
import { computed, reactive, ref } from 'vue';
3+
import { computed, reactive } from 'vue';
44

55
import type { TraktEpisodeExtended, TraktEpisodeShort } from '~/models/trakt/trakt-episode.model';
66
import type { TraktCollectionProgress, TraktWatchedProgress } from '~/models/trakt/trakt-progress.model';
@@ -10,7 +10,6 @@ import type { TraktShowExtended } from '~/models/trakt/trakt-show.model';
1010
import { type ShowProgress, ShowProgressType } from '~/models/list-scroll.model';
1111
import { NotificationService } from '~/services/notification.service';
1212
import { TraktService } from '~/services/trakt.service';
13-
import { asyncRefGetter } from '~/utils/vue.utils';
1413

1514
export type ShowSeasons = Record<number, TraktSeasonExtended>;
1615

@@ -206,42 +205,33 @@ export const useShowStore = defineStore('data.show', () => {
206205
};
207206

208207
const getShowLoading = (id: number | string) => computed(() => showsLoading[id.toString()]);
209-
const getShow = async (id: number | string) => {
210-
if (!shows[id.toString()] && !showsLoading[id.toString()]) await fetchShow(id.toString());
211-
return shows[id.toString()];
212-
};
213-
const getShowRef = (id: number | string, response = ref<TraktShowExtended>()) => asyncRefGetter(() => getShow(id), getShowLoading(id), response);
208+
const getShow = (id: number | string) =>
209+
computed(() => {
210+
if (showsLoading[id.toString()]) return undefined;
211+
return shows[id.toString()];
212+
});
214213

215214
const getSeasonsLoading = (id: number | string) => computed(() => showsSeasonsLoading[id.toString()]);
216-
const getShowSeasons = async (id: number | string) => {
217-
if (!showsSeasons[id.toString()] && !showsSeasonsLoading[id.toString()]) await fetchShowSeasons(id.toString());
218-
return showsSeasons[id.toString()];
219-
};
220-
const getShowSeasonsRef = (id: number | string, response = ref<ShowSeasons>()) =>
221-
asyncRefGetter(() => getShowSeasons(id), getSeasonsLoading(id), response);
215+
const getShowSeasons = (id: number | string) =>
216+
computed(() => {
217+
if (showsSeasonsLoading[id.toString()]) return undefined;
218+
return showsSeasons[id.toString()];
219+
});
222220

223221
const getSeasonEpisodesLoading = (id: number | string, season: number) => computed(() => showsSeasonEpisodesLoading[id.toString()]?.[season]);
224-
const getShowSeasonEpisodes = async (id: number | string, season: number) => {
225-
if (!showsSeasonEpisodes[id.toString()]?.[season] && !showsSeasonEpisodesLoading[id.toString()]?.[season]) {
226-
await fetchShowSeasonEpisodes(id.toString(), season);
227-
}
228-
return showsSeasonEpisodes[id.toString()]?.[season];
229-
};
230-
const getShowSeasonEpisodesRef = (id: number | string, season: number, response = ref<TraktEpisodeShort[]>()) =>
231-
asyncRefGetter(() => getShowSeasonEpisodes(id, season), getSeasonEpisodesLoading(id, season), response);
222+
const getShowSeasonEpisodes = (id: number | string, season: number) =>
223+
computed(() => {
224+
if (showsSeasonEpisodesLoading[id.toString()]?.[season]) return undefined;
225+
return showsSeasonEpisodes[id.toString()]?.[season];
226+
});
232227

233228
const getEpisodesLoading = (id: number | string, season: number, episode: number) =>
234229
computed(() => showsEpisodesLoading[id.toString()]?.[season]?.[episode]);
235-
const getShowEpisode = async ({ id, season, episode }: { id: number | string; season: number; episode: number }) => {
236-
if (!showsEpisodes[id.toString()]?.[season]?.[episode] && !showsEpisodesLoading[id.toString()]?.[season]?.[episode]) {
237-
await fetchShowEpisode(id.toString(), season, episode);
238-
}
239-
return showsEpisodes[id.toString()]?.[season]?.[episode];
240-
};
241-
const getShowEpisodeRef = (
242-
{ id, season, episode }: { id: number | string; season: number; episode: number },
243-
response = ref<TraktEpisodeExtended>(),
244-
) => asyncRefGetter(() => getShowEpisode({ id, season, episode }), getEpisodesLoading(id, season, episode), response);
230+
const getShowEpisode = ({ id, season, episode }: { id: number | string; season: number; episode: number }) =>
231+
computed(() => {
232+
if (showsEpisodesLoading[id.toString()]?.[season]?.[episode]) return undefined;
233+
return showsEpisodes[id.toString()]?.[season]?.[episode];
234+
});
245235

246236
const getShowProgressLoading = (id: number | string) => computed(() => showWatchedProgressLoading[id.toString()]);
247237
const getShowWatchedProgress = (id: number | string) => {
@@ -267,16 +257,16 @@ export const useShowStore = defineStore('data.show', () => {
267257
return {
268258
clearState,
269259
getShow,
270-
getShowRef,
260+
fetchShow,
271261
getShowLoading,
272262
getShowSeasons,
273-
getShowSeasonsRef,
263+
fetchShowSeasons,
274264
getSeasonsLoading,
275265
getShowSeasonEpisodes,
276-
getShowSeasonEpisodesRef,
266+
fetchShowSeasonEpisodes,
277267
getSeasonEpisodesLoading,
278268
getShowEpisode,
279-
getShowEpisodeRef,
269+
fetchShowEpisode,
280270
getEpisodesLoading,
281271
getShowWatchedProgress,
282272
getShowProgressLoading,

0 commit comments

Comments
 (0)