vomebook commited on
Commit ·
3b2d149
1
Parent(s): 929f779
Unify unfinished speech job cleanup and drop the redundant clip epoch
Browse filesFiles changed (2)
1 unmodified line2345678485 unmodified lines4944954964974984995001 unmodified line"version": 1,"scope": "site","limit": 12,"branches": 17569,"files": {"backend/app.py": {"peak": 19,485 unmodified lines},"static/tts-client.js": {"peak": 51,"excess": 204}}}1 unmodified line2345678485 unmodified lines4944954964974984995001 unmodified line"version": 1,"scope": "site","limit": 12,"branches": 17561,"files": {"backend/app.py": {"peak": 19,485 unmodified lines},"static/tts-client.js": {"peak": 51,"excess": 197}}}
369 unmodified lines37037137237337437537620 unmodified lines39739839940040140240314 unmodified lines4184194204214224234246 unmodified lines43143243343443543643735 unmodified lines4734744754764774788 unmodified lines48748848949049149249398 unmodified lines592593594595596597598487 unmodified lines1086108710881089109010911092109310941095369 unmodified linesconst CLIP_RETRY_MS = 2000;/** @type {WeakMap<SpeechJob, number[]>} */const clipRetryAt = new WeakMap();let clipBytes = 0, clipEpoch = 0, clipLoads = 0;/** @param {string} text */function notice(text) { status.textContent = text; }20 unmodified linesentry.controller?.abort();if (entry.url) { URL.revokeObjectURL(entry.url); clipBytes -= entry.bytes; }}function clearClips() { clipEpoch += 1; for (const key of Array.from(clips.keys())) dropClip(key); }/** @param {string} key */function clipUrl(key) {const entry = clips.get(key);14 unmodified linesconst key = clipKey(value, job, part);if (clips.has(key) || clipLoads >= CLIP_LOADS || !part.audio_url || (clipRetryAt.get(job)?.[part.index] ?? 0) > performance.now()) return;/** @type {SpeechClip} */const entry = { url: "", bytes: 0, controller: new AbortController() }, epoch = clipEpoch;const miss = () => {if (clips.get(key) !== entry) return; // Aborted or superseded preloads don't count.// Both the completion refill and the playback pump honor this; advance still uses the audio URL.6 unmodified linesfetch(ORIGIN + part.audio_url, { credentials: "omit", signal: /** @type {AbortController} */ (entry.controller).signal }).then(response => response.ok && (response.headers.get("Content-Type") || "").startsWith("audio/mpeg") ? response.blob() : null).then(blob => {if (epoch !== clipEpoch || clips.get(key) !== entry) return;entry.controller = null;if (!blob || blob.size !== part.bytes) miss();else storeClip(key, entry, blob);35 unmodified linesvalue.jobs.delete(job.batch.start);release(job.id);}function playbackSettings() { return { rate: Number(speed.value), timer: timer.value, deadline: sleepDeadline }; }function clearSleep() {clearTimeout(sleepTimer); sleepDeadline = 0; finishSentence = false; timer.value = "off";8 unmodified linesfinishSentence = timer.value === "clip";if (finishSentence && owner) {cancelHandoff(); clearPreload();for (const job of Array.from(owner.jobs.values())) if (job.batch.start !== owner.batch.start && job.state !== "ready") discard(owner, job);}function tickSleep() { if (!checkSleep() && sleepDeadline) sleepTimer = setTimeout(tickSleep, Math.min(1000, sleepDeadline - Date.now())); }tickSleep();98 unmodified linesif (!owner) { if (restart) { retryStart = restart; notice("已暂停"); controls(); } return; }owner.wantsPlay = false;clearPreload();for (const job of Array.from(owner.jobs.values())) if (job.batch.start !== owner.batch.start && job.state !== "ready") discard(owner, job);/** @type {HTMLAudioElement} */ (audio).pause(); cancelHandoff(); if (retiring) retire(); notice("已暂停"); controls();if ("mediaSession" in navigator) navigator.mediaSession.playbackState = "paused";}487 unmodified linesvalue.position = offset; value.batch = job.batch; value.part = index; value.wantsPlay = playing;value.blocked = false;// Do not let synthesis of a now irrelevant batch hold up the target job.for (const other of Array.from(value.jobs.values())) {if (other !== job && other.batch.start !== job.batch.end && other.state !== "ready") discard(value, other);else if (!playing && other !== job && other.state !== "ready") discard(value, other);}const player = /** @type {HTMLAudioElement} */ (audio);if (current?.job === job && current.part.index === index && player.readyState >= 1 && !player.error) {player.currentTime = 0;369 unmodified lines37037137237337437537620 unmodified lines39739839940040140240314 unmodified lines4184194204214224234246 unmodified lines43143243343443543643743835 unmodified lines4744754764774784794804814824834844854868 unmodified lines49549649749849950050198 unmodified lines600601602603604605606487 unmodified lines1094109510961097109810991100369 unmodified linesconst CLIP_RETRY_MS = 2000;/** @type {WeakMap<SpeechJob, number[]>} */const clipRetryAt = new WeakMap();let clipBytes = 0, clipLoads = 0;/** @param {string} text */function notice(text) { status.textContent = text; }20 unmodified linesentry.controller?.abort();if (entry.url) { URL.revokeObjectURL(entry.url); clipBytes -= entry.bytes; }}function clearClips() { for (const key of Array.from(clips.keys())) dropClip(key); }/** @param {string} key */function clipUrl(key) {const entry = clips.get(key);14 unmodified linesconst key = clipKey(value, job, part);if (clips.has(key) || clipLoads >= CLIP_LOADS || !part.audio_url || (clipRetryAt.get(job)?.[part.index] ?? 0) > performance.now()) return;/** @type {SpeechClip} */const entry = { url: "", bytes: 0, controller: new AbortController() };const miss = () => {if (clips.get(key) !== entry) return; // Aborted or superseded preloads don't count.// Both the completion refill and the playback pump honor this; advance still uses the audio URL.6 unmodified linesfetch(ORIGIN + part.audio_url, { credentials: "omit", signal: /** @type {AbortController} */ (entry.controller).signal }).then(response => response.ok && (response.headers.get("Content-Type") || "").startsWith("audio/mpeg") ? response.blob() : null).then(blob => {// Clearing removes the entry; a new fetch for the same key has a different entry.if (clips.get(key) !== entry) return;entry.controller = null;if (!blob || blob.size !== part.bytes) miss();else storeClip(key, entry, blob);35 unmodified linesvalue.jobs.delete(job.batch.start);release(job.id);}/** @param {SpeechOwner} value */function discardUnready(value, keepFollowing = false) {const { start, end } = value.batch;for (const job of Array.from(value.jobs.values())) {if (job.batch.start !== start && (!keepFollowing || job.batch.start !== end) && job.state !== "ready") discard(value, job);}}function playbackSettings() { return { rate: Number(speed.value), timer: timer.value, deadline: sleepDeadline }; }function clearSleep() {clearTimeout(sleepTimer); sleepDeadline = 0; finishSentence = false; timer.value = "off";8 unmodified linesfinishSentence = timer.value === "clip";if (finishSentence && owner) {cancelHandoff(); clearPreload();discardUnready(owner);}function tickSleep() { if (!checkSleep() && sleepDeadline) sleepTimer = setTimeout(tickSleep, Math.min(1000, sleepDeadline - Date.now())); }tickSleep();98 unmodified linesif (!owner) { if (restart) { retryStart = restart; notice("已暂停"); controls(); } return; }owner.wantsPlay = false;clearPreload();discardUnready(owner);/** @type {HTMLAudioElement} */ (audio).pause(); cancelHandoff(); if (retiring) retire(); notice("已暂停"); controls();if ("mediaSession" in navigator) navigator.mediaSession.playbackState = "paused";}487 unmodified linesvalue.position = offset; value.batch = job.batch; value.part = index; value.wantsPlay = playing;value.blocked = false;// Do not let synthesis of a now irrelevant batch hold up the target job.discardUnready(value, playing);const player = /** @type {HTMLAudioElement} */ (audio);if (current?.job === job && current.part.index === index && player.readyState >= 1 && !player.error) {player.currentTime = 0;
scripts/complexity-budget.json+2-2
static/tts-client.js+15-10