Fix playback, account, download and library bugs found in review
All checks were successful
Validate Kaleido Desktop / validate (push) Successful in 1m22s

- Clear cached library data when the account or profile behind the
  server connection changes, not only on Home user switches.
- TitleArt falls back to the title when there is no server, so the
  player no longer crashes offline.
- Playback keeps the server it started on: progress, watched state and
  transcodes never go to a server selected later, a download only
  reports to its own server, and switching account or server ends a
  stream from the old one.
- Closing or replacing playback during setup can no longer start the
  file afterwards.
- Transcodes save the chosen audio/subtitle tracks on the server before
  starting (and name them in the request), apply the language and
  subtitle preferences, and keep the current tracks across quality
  changes.
- Offline playback saves its resume point and watched count in the
  download's metadata.
- Download tasks take over from a previous task for the same title only
  after it exits, and pause/remove stay wired to the current task.
- Downloads prefer a single-file version and refuse split ones instead
  of saving only part one.
- Download artwork is named per file, so episodes in one season folder
  no longer overwrite or delete each other's images.
- Library grids refetch loaded pages when the cache is invalidated.
- Request timeouts and cancellation also cover the response body.
- Hero rotation resumes after being scrolled back into view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-04 22:41:12 -07:00
parent c70aeede74
commit 8755d16735
9 changed files with 399 additions and 99 deletions

View file

@ -121,23 +121,41 @@ export async function plexRequest<T = any>(url: string, token: string | null | u
const timer = setTimeout(() => controller.abort(), opts.timeout ?? 30000);
const onAbort = () => controller.abort();
opts.signal?.addEventListener('abort', onAbort);
// The timeout and the caller's cancel cover the body as well as the headers: a reply
// that stalls part way must still end. (Rejects only once aborted.)
const aborted = new Promise<never>((_, reject) =>
controller.signal.addEventListener('abort', () => reject(opts.signal?.aborted ? opts.signal.reason : new PlexError('The server took too long to respond.', 0)), { once: true }),
);
aborted.catch(() => {});
if (opts.signal?.aborted) controller.abort();
let response: Response;
let text: string;
try {
response = await httpFetch(withParams(url, opts.params), {
method: opts.method ?? 'GET',
headers: { ...plexHeaders(token), ...opts.headers },
body: opts.body ?? undefined,
signal: controller.signal,
});
} catch (e: any) {
if (opts.signal?.aborted) throw e;
throw new PlexError(controller.signal.aborted ? 'The server took too long to respond.' : 'Could not reach the server.', 0);
try {
response = await Promise.race([
httpFetch(withParams(url, opts.params), {
method: opts.method ?? 'GET',
headers: { ...plexHeaders(token), ...opts.headers },
body: opts.body ?? undefined,
signal: controller.signal,
}),
aborted,
]);
} catch (e: any) {
if (opts.signal?.aborted) throw e;
throw new PlexError(controller.signal.aborted ? 'The server took too long to respond.' : 'Could not reach the server.', 0);
}
if (!response.ok) throw new PlexError(statusMessage(response.status), response.status);
try {
text = await Promise.race([response.text(), aborted]);
} catch (e: any) {
if (opts.signal?.aborted || e instanceof PlexError) throw e;
throw new PlexError('The connection to the server was lost.', 0);
}
} finally {
clearTimeout(timer);
opts.signal?.removeEventListener('abort', onAbort);
}
if (!response.ok) throw new PlexError(statusMessage(response.status), response.status);
const text = await response.text();
if (opts.text) return text as unknown as T;
if (!text.trim()) return {} as T;
try {

View file

@ -39,6 +39,10 @@ export function transcodeUrl(
path: `/library/metadata/${item.id}`,
mediaIndex: opts.mediaIndex,
partIndex: 0,
// Plex converts the part's selected streams; callers save the selection first and
// name it here as well.
audioStreamID: opts.audioStreamID,
subtitleStreamID: opts.subtitleStreamID,
protocol: 'hls',
session: opts.session,
offset: (opts.offsetMs / 1000).toFixed(1),