diff --git a/apps/readest-app/src/__tests__/hooks/useTTSControl.test.tsx b/apps/readest-app/src/__tests__/hooks/useTTSControl.test.tsx
index 029ea3641..f663f38ea 100644
--- a/apps/readest-app/src/__tests__/hooks/useTTSControl.test.tsx
+++ b/apps/readest-app/src/__tests__/hooks/useTTSControl.test.tsx
@@ -449,6 +449,37 @@ describe('useTTSControl changing the voice', () => {
});
});
+describe('useTTSControl a start that fails', () => {
+ beforeEach(() => {
+ ttsControllerInstances.length = 0;
+ pendingInitResolvers.length = 0;
+ });
+
+ afterEach(() => {
+ cleanup();
+ });
+
+ it('closes the player sheet it may have opened early', async () => {
+ const onRequestHidePanel = vi.fn();
+ const FailingHarness = () => {
+ useTTSControl({ bookKey: 'book-1', onRequestHidePanel });
+ return null;
+ };
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ render();
+ await act(async () => {
+ const started = eventDispatcher.dispatch('tts-speak', { bookKey: 'book-1' });
+ for (let i = 0; i < 10; i++) await Promise.resolve();
+ (
+ ttsControllerInstances[0] as { initViewTTS: ReturnType }
+ ).initViewTTS.mockRejectedValueOnce(new Error('no text on this page'));
+ while (pendingInitResolvers.length > 0) pendingInitResolvers.shift()!();
+ await started;
+ });
+ expect(onRequestHidePanel).toHaveBeenCalled();
+ });
+});
+
describe('useTTSControl stopping while a start is still initializing', () => {
beforeEach(() => {
ttsControllerInstances.length = 0;
diff --git a/apps/readest-app/src/__tests__/hooks/useTTSDownloads.test.tsx b/apps/readest-app/src/__tests__/hooks/useTTSDownloads.test.tsx
index 2c4f52ea2..892e54204 100644
--- a/apps/readest-app/src/__tests__/hooks/useTTSDownloads.test.tsx
+++ b/apps/readest-app/src/__tests__/hooks/useTTSDownloads.test.tsx
@@ -82,7 +82,7 @@ describe('useTTSDownloads', () => {
},
});
const getController = () => controller;
- const { result } = renderHook(() => useTTSDownloads('bookhash', getController, false));
+ const { result } = renderHook(() => useTTSDownloads('bookhash', getController, false, true));
await waitFor(() =>
expect(result.current.chapters.map((c) => [c.label, c.startSection, c.endSection])).toEqual([
@@ -100,7 +100,7 @@ describe('useTTSDownloads', () => {
},
});
mocks.getBookData.mockReturnValue({ bookDoc: { toc: [{ label: 'Broken', href: '' }] } });
- const { result } = renderHook(() => useTTSDownloads('bookhash', () => controller, false));
+ const { result } = renderHook(() => useTTSDownloads('bookhash', () => controller, false, true));
await waitFor(() =>
expect(result.current.chapters[0]).toMatchObject({
key: 'section-0',
@@ -126,7 +126,7 @@ describe('useTTSDownloads', () => {
bookDoc: { toc: [{ label: key, href: '0' }] },
}));
const { result, rerender } = renderHook(
- ({ key, controller }) => useTTSDownloads(key, () => controller, false),
+ ({ key, controller }) => useTTSDownloads(key, () => controller, false, true),
{ initialProps: { key: 'old', controller: oldController } },
);
rerender({ key: 'new', controller: newController });
@@ -139,6 +139,23 @@ describe('useTTSDownloads', () => {
expect(result.current.chapters[0]?.label).toBe('new');
});
+ test('resumes queued downloads only once the session picked its engine', async () => {
+ const controller = makeController(() => new Map());
+ const getController = () => controller;
+ const { rerender } = renderHook(
+ ({ ready }) => useTTSDownloads('bookhash', getController, false, ready),
+ { initialProps: { ready: false } },
+ );
+ // Before the clients initialize, the controller would download with its
+ // default engine, not the voice the session is about to read with.
+ await new Promise((r) => setTimeout(r, 0));
+ expect(mocks.attachController).not.toHaveBeenCalled();
+ rerender({ ready: true });
+ await waitFor(() =>
+ expect(mocks.attachController).toHaveBeenCalledWith('bookhash', getController),
+ );
+ });
+
test('uses the stable book hash for persisted rows and every manager operation', async () => {
const controller = makeController(() => new Map());
const getController = () => controller;
@@ -154,7 +171,7 @@ describe('useTTSDownloads', () => {
);
const { result, unmount } = renderHook(() =>
- useTTSDownloads('bookhash-reader-session', getController, false),
+ useTTSDownloads('bookhash-reader-session', getController, false, true),
);
await waitFor(() =>
@@ -191,7 +208,7 @@ describe('useTTSDownloads', () => {
'bookhash',
);
- const { result } = renderHook(() => useTTSDownloads('bookhash', getController, true));
+ const { result } = renderHook(() => useTTSDownloads('bookhash', getController, true, true));
await waitFor(() => expect(controller.getSectionCacheStatuses).toHaveBeenCalledTimes(2));
await waitFor(() => expect(result.current.chapters).toHaveLength(1));
expect(result.current.statusOf(result.current.chapters[0]!)).toBe('none');
@@ -221,7 +238,7 @@ describe('useTTSDownloads', () => {
resolveNewer = resolve;
}),
);
- const { result } = renderHook(() => useTTSDownloads('bookhash', () => controller, false));
+ const { result } = renderHook(() => useTTSDownloads('bookhash', () => controller, false, true));
let older: Promise = Promise.resolve();
let newer: Promise = Promise.resolve();
@@ -244,7 +261,7 @@ describe('useTTSDownloads', () => {
const controller = makeController(() => new Map());
const getController = () => controller;
const { result } = renderHook(() =>
- useTTSDownloads('bookhash-reader-session', getController, false),
+ useTTSDownloads('bookhash-reader-session', getController, false, true),
);
let finishCancellation: () => void = () => {};
diff --git a/apps/readest-app/src/app/reader/components/tts/TTSControl.tsx b/apps/readest-app/src/app/reader/components/tts/TTSControl.tsx
index b90e05d3c..b9f967122 100644
--- a/apps/readest-app/src/app/reader/components/tts/TTSControl.tsx
+++ b/apps/readest-app/src/app/reader/components/tts/TTSControl.tsx
@@ -31,7 +31,12 @@ const TTSControl: React.FC = ({ bookKey, gridInsets }) => {
onRequestHidePanel: () => setShowPlayerSheet(false),
});
- const downloads = useTTSDownloads(bookKey, tts.getController, showPlayerSheet);
+ const downloads = useTTSDownloads(
+ bookKey,
+ tts.getController,
+ showPlayerSheet,
+ tts.ttsClientsInited,
+ );
const activeSectionIndex = tts.ttsSectionIndex;
const viewSettings = getViewSettings(bookKey);
diff --git a/apps/readest-app/src/app/reader/hooks/useTTSControl.ts b/apps/readest-app/src/app/reader/hooks/useTTSControl.ts
index 370371e31..8c9be167c 100644
--- a/apps/readest-app/src/app/reader/hooks/useTTSControl.ts
+++ b/apps/readest-app/src/app/reader/hooks/useTTSControl.ts
@@ -1080,6 +1080,9 @@ export const useTTSControl = ({ bookKey, onRequestHidePanel }: UseTTSControlProp
} catch (error) {
// A start the user cancelled must not touch the session after it.
if (startGenerationRef.current !== generation) return;
+ // The full player may already be open (it opens before init): close
+ // it with the mini player rather than leave dead controls up.
+ onRequestHidePanel?.();
setShowIndicator(false);
setIsPlaying(false);
eventDispatcher.dispatch('toast', {
diff --git a/apps/readest-app/src/app/reader/hooks/useTTSDownloads.ts b/apps/readest-app/src/app/reader/hooks/useTTSDownloads.ts
index 4f8bbc5d3..4c0f4e6bf 100644
--- a/apps/readest-app/src/app/reader/hooks/useTTSDownloads.ts
+++ b/apps/readest-app/src/app/reader/hooks/useTTSDownloads.ts
@@ -40,6 +40,9 @@ export const useTTSDownloads = (
bookKey: string,
getController: () => TTSController | null,
isOpen: boolean,
+ // The session's clients are initialized, so its engine and voice are
+ // chosen: a download started earlier would use the default engine.
+ clientsReady: boolean,
): UseTTSDownloadsResult => {
const _ = useTranslation();
const { getBookData } = useBookDataStore();
@@ -63,11 +66,11 @@ export const useTTSDownloads = (
// Bind the book to the manager while a download-capable session is live so
// queued items — including rows restored from a previous session — process.
useEffect(() => {
- if (!supported) return;
+ if (!supported || !clientsReady) return;
ttsDownloadManager.attachController(bookHash, getController);
return () => ttsDownloadManager.detachController(bookHash);
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [supported, bookHash]);
+ }, [supported, clientsReady, bookHash]);
useEffect(() => {
let cancelled = false;