This commit is contained in:
2026-06-16 10:05:23 -06:00
parent fb0b8d11c9
commit 33d5bfd8e9
2 changed files with 12 additions and 4 deletions

View File

@@ -227,8 +227,15 @@ export function useTeachingBook(bookId: string): TeachingBookStore {
}
}
const abortController = new AbortController()
async function runWorker(): Promise<void> {
while (!firstError && !options.isCancelled?.()) {
while (!firstError) {
if (options.isCancelled?.()) {
abortController.abort()
return
}
const index = nextStartIndex
if (index >= topics.length) return
@@ -237,12 +244,13 @@ export function useTeachingBook(bookId: string): TeachingBookStore {
options.onTopicStart?.(topic)
try {
const result = await booksApi.generateLesson(topic)
const result = await booksApi.generateLesson(topic, abortController.signal)
results[index] = removeGeneratedAdditionalContent(
parseTeachingDesign(result.filename, result.markdown),
)
appendReadyLessons()
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') return
firstError = error instanceof Error ? error.message : '生成失败。'
}
}

View File

@@ -50,8 +50,8 @@ export function deleteBook(id: string): Promise<{ ok: true }> {
return authedFetch(`/api/books/${id}`, { method: 'DELETE' })
}
export function generateLesson(topic: string): Promise<GenerateResult> {
return authedFetch('/api/generate', { method: 'POST', body: JSON.stringify({ topic }) })
export function generateLesson(topic: string, signal?: AbortSignal): Promise<GenerateResult> {
return authedFetch('/api/generate', { method: 'POST', body: JSON.stringify({ topic }), signal })
}
export function generateOutline(theme: string): Promise<{ titles: string[] }> {