refactor: derive availableModes from modeSources
This commit is contained in:
@@ -61,7 +61,6 @@ export const goToNextEpisode = async (): Promise<void> => {
|
|||||||
|
|
||||||
// update state with new episode data
|
// update state with new episode data
|
||||||
state.modeSources = data.mode_sources ?? {};
|
state.modeSources = data.mode_sources ?? {};
|
||||||
state.availableModes = data.available_modes ?? [];
|
|
||||||
|
|
||||||
const backendMode = typeof data.initial_mode === "string" ? data.initial_mode : "";
|
const backendMode = typeof data.initial_mode === "string" ? data.initial_mode : "";
|
||||||
const fallback = state.modeSources[backendMode]?.token
|
const fallback = state.modeSources[backendMode]?.token
|
||||||
|
|||||||
@@ -48,11 +48,6 @@ const alternateModeFor = (mode: string): "sub" | "dub" | null => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const mergeAvailableMode = (mode: string): void => {
|
|
||||||
if (state.availableModes.includes(mode)) return;
|
|
||||||
state.availableModes = [...state.availableModes, mode].sort();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const hydrateAlternateMode = async (signal?: AbortSignal): Promise<void> => {
|
export const hydrateAlternateMode = async (signal?: AbortSignal): Promise<void> => {
|
||||||
const alternateMode = alternateModeFor(state.currentMode);
|
const alternateMode = alternateModeFor(state.currentMode);
|
||||||
if (!alternateMode) return;
|
if (!alternateMode) return;
|
||||||
@@ -76,7 +71,6 @@ export const hydrateAlternateMode = async (signal?: AbortSignal): Promise<void>
|
|||||||
...state.modeSources,
|
...state.modeSources,
|
||||||
[alternateMode]: alternateSource,
|
[alternateMode]: alternateSource,
|
||||||
};
|
};
|
||||||
mergeAvailableMode(alternateMode);
|
|
||||||
|
|
||||||
updateSubtitleOptions();
|
updateSubtitleOptions();
|
||||||
updateQualityOptions();
|
updateQualityOptions();
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export interface PlayerState {
|
|||||||
timeDisplay: HTMLElement;
|
timeDisplay: HTMLElement;
|
||||||
durationDisplay: HTMLElement;
|
durationDisplay: HTMLElement;
|
||||||
modeSources: Record<string, ModeSource>;
|
modeSources: Record<string, ModeSource>;
|
||||||
availableModes: string[];
|
readonly availableModes: string[];
|
||||||
currentMode: string;
|
currentMode: string;
|
||||||
modeSwitchedFrom: string;
|
modeSwitchedFrom: string;
|
||||||
currentEpisode: string;
|
currentEpisode: string;
|
||||||
@@ -54,7 +54,9 @@ const createInitialState = (): PlayerState => ({
|
|||||||
timeDisplay: document.createElement("div"),
|
timeDisplay: document.createElement("div"),
|
||||||
durationDisplay: document.createElement("div"),
|
durationDisplay: document.createElement("div"),
|
||||||
modeSources: {},
|
modeSources: {},
|
||||||
availableModes: [],
|
get availableModes() {
|
||||||
|
return Object.keys(this.modeSources);
|
||||||
|
},
|
||||||
currentMode: "dub",
|
currentMode: "dub",
|
||||||
modeSwitchedFrom: "",
|
modeSwitchedFrom: "",
|
||||||
currentEpisode: "1",
|
currentEpisode: "1",
|
||||||
@@ -211,8 +213,6 @@ export const initState = (c: HTMLElement): boolean => {
|
|||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseAvailableModes = (v: unknown): string[] => (isStringArray(v) ? v : []);
|
|
||||||
|
|
||||||
const parseSegments = (v: unknown): SkipSegment[] => {
|
const parseSegments = (v: unknown): SkipSegment[] => {
|
||||||
if (!Array.isArray(v)) return [];
|
if (!Array.isArray(v)) return [];
|
||||||
const out: SkipSegment[] = [];
|
const out: SkipSegment[] = [];
|
||||||
@@ -230,7 +230,6 @@ export const initState = (c: HTMLElement): boolean => {
|
|||||||
|
|
||||||
// mode sources = { sub: { token, subtitles, qualities }, dub: { ... } }
|
// mode sources = { sub: { token, subtitles, qualities }, dub: { ... } }
|
||||||
state.modeSources = parseModeSources(safeJsonUnknown(dataset(c, "modeSources")));
|
state.modeSources = parseModeSources(safeJsonUnknown(dataset(c, "modeSources")));
|
||||||
state.availableModes = parseAvailableModes(safeJsonUnknown(dataset(c, "availableModes")));
|
|
||||||
|
|
||||||
// resolve initial mode: localStorage > backend default > first available > 'dub'
|
// resolve initial mode: localStorage > backend default > first available > 'dub'
|
||||||
const backendInitialMode = dataset(c, "initialMode") || "dub";
|
const backendInitialMode = dataset(c, "initialMode") || "dub";
|
||||||
|
|||||||
Reference in New Issue
Block a user