vomebook commited on
Commit
3b2d149
·
1 Parent(s): 929f779

Unify unfinished speech job cleanup and drop the redundant clip epoch

Browse files
scripts/complexity-budget.json CHANGED
@@ -2,7 +2,7 @@
2
  "version": 1,
3
  "scope": "site",
4
  "limit": 12,
5
- "branches": 17569,
6
  "files": {
7
  "backend/app.py": {
8
  "peak": 19,
@@ -494,7 +494,7 @@
494
  },
495
  "static/tts-client.js": {
496
  "peak": 51,
497
- "excess": 204
498
  }
499
  }
500
  }
 
2
  "version": 1,
3
  "scope": "site",
4
  "limit": 12,
5
+ "branches": 17561,
6
  "files": {
7
  "backend/app.py": {
8
  "peak": 19,
 
494
  },
495
  "static/tts-client.js": {
496
  "peak": 51,
497
+ "excess": 197
498
  }
499
  }
500
  }
static/tts-client.js CHANGED
@@ -370,7 +370,7 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
370
  const CLIP_RETRY_MS = 2000;
371
  /** @type {WeakMap<SpeechJob, number[]>} */
372
  const clipRetryAt = new WeakMap();
373
- let clipBytes = 0, clipEpoch = 0, clipLoads = 0;
374
 
375
  /** @param {string} text */
376
  function notice(text) { status.textContent = text; }
@@ -397,7 +397,7 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
397
  entry.controller?.abort();
398
  if (entry.url) { URL.revokeObjectURL(entry.url); clipBytes -= entry.bytes; }
399
  }
400
- function clearClips() { clipEpoch += 1; for (const key of Array.from(clips.keys())) dropClip(key); }
401
  /** @param {string} key */
402
  function clipUrl(key) {
403
  const entry = clips.get(key);
@@ -418,7 +418,7 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
418
  const key = clipKey(value, job, part);
419
  if (clips.has(key) || clipLoads >= CLIP_LOADS || !part.audio_url || (clipRetryAt.get(job)?.[part.index] ?? 0) > performance.now()) return;
420
  /** @type {SpeechClip} */
421
- const entry = { url: "", bytes: 0, controller: new AbortController() }, epoch = clipEpoch;
422
  const miss = () => {
423
  if (clips.get(key) !== entry) return; // Aborted or superseded preloads don't count.
424
  // Both the completion refill and the playback pump honor this; advance still uses the audio URL.
@@ -431,7 +431,8 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
431
  fetch(ORIGIN + part.audio_url, { credentials: "omit", signal: /** @type {AbortController} */ (entry.controller).signal })
432
  .then(response => response.ok && (response.headers.get("Content-Type") || "").startsWith("audio/mpeg") ? response.blob() : null)
433
  .then(blob => {
434
- if (epoch !== clipEpoch || clips.get(key) !== entry) return;
 
435
  entry.controller = null;
436
  if (!blob || blob.size !== part.bytes) miss();
437
  else storeClip(key, entry, blob);
@@ -473,6 +474,13 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
473
  value.jobs.delete(job.batch.start);
474
  release(job.id);
475
  }
 
 
 
 
 
 
 
476
  function playbackSettings() { return { rate: Number(speed.value), timer: timer.value, deadline: sleepDeadline }; }
477
  function clearSleep() {
478
  clearTimeout(sleepTimer); sleepDeadline = 0; finishSentence = false; timer.value = "off";
@@ -487,7 +495,7 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
487
  finishSentence = timer.value === "clip";
488
  if (finishSentence && owner) {
489
  cancelHandoff(); clearPreload();
490
- for (const job of Array.from(owner.jobs.values())) if (job.batch.start !== owner.batch.start && job.state !== "ready") discard(owner, job);
491
  }
492
  function tickSleep() { if (!checkSleep() && sleepDeadline) sleepTimer = setTimeout(tickSleep, Math.min(1000, sleepDeadline - Date.now())); }
493
  tickSleep();
@@ -592,7 +600,7 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
592
  if (!owner) { if (restart) { retryStart = restart; notice("已暂停"); controls(); } return; }
593
  owner.wantsPlay = false;
594
  clearPreload();
595
- for (const job of Array.from(owner.jobs.values())) if (job.batch.start !== owner.batch.start && job.state !== "ready") discard(owner, job);
596
  /** @type {HTMLAudioElement} */ (audio).pause(); cancelHandoff(); if (retiring) retire(); notice("已暂停"); controls();
597
  if ("mediaSession" in navigator) navigator.mediaSession.playbackState = "paused";
598
  }
@@ -1086,10 +1094,7 @@ if (typeof document !== "undefined") /** @type {Window & {CCRDSpeech?: unknown}}
1086
  value.position = offset; value.batch = job.batch; value.part = index; value.wantsPlay = playing;
1087
  value.blocked = false;
1088
  // Do not let synthesis of a now irrelevant batch hold up the target job.
1089
- for (const other of Array.from(value.jobs.values())) {
1090
- if (other !== job && other.batch.start !== job.batch.end && other.state !== "ready") discard(value, other);
1091
- else if (!playing && other !== job && other.state !== "ready") discard(value, other);
1092
- }
1093
  const player = /** @type {HTMLAudioElement} */ (audio);
1094
  if (current?.job === job && current.part.index === index && player.readyState >= 1 && !player.error) {
1095
  player.currentTime = 0;
 
370
  const CLIP_RETRY_MS = 2000;
371
  /** @type {WeakMap<SpeechJob, number[]>} */
372
  const clipRetryAt = new WeakMap();
373
+ let clipBytes = 0, clipLoads = 0;
374
 
375
  /** @param {string} text */
376
  function notice(text) { status.textContent = text; }
 
397
  entry.controller?.abort();
398
  if (entry.url) { URL.revokeObjectURL(entry.url); clipBytes -= entry.bytes; }
399
  }
400
+ function clearClips() { for (const key of Array.from(clips.keys())) dropClip(key); }
401
  /** @param {string} key */
402
  function clipUrl(key) {
403
  const entry = clips.get(key);
 
418
  const key = clipKey(value, job, part);
419
  if (clips.has(key) || clipLoads >= CLIP_LOADS || !part.audio_url || (clipRetryAt.get(job)?.[part.index] ?? 0) > performance.now()) return;
420
  /** @type {SpeechClip} */
421
+ const entry = { url: "", bytes: 0, controller: new AbortController() };
422
  const miss = () => {
423
  if (clips.get(key) !== entry) return; // Aborted or superseded preloads don't count.
424
  // Both the completion refill and the playback pump honor this; advance still uses the audio URL.
 
431
  fetch(ORIGIN + part.audio_url, { credentials: "omit", signal: /** @type {AbortController} */ (entry.controller).signal })
432
  .then(response => response.ok && (response.headers.get("Content-Type") || "").startsWith("audio/mpeg") ? response.blob() : null)
433
  .then(blob => {
434
+ // Clearing removes the entry; a new fetch for the same key has a different entry.
435
+ if (clips.get(key) !== entry) return;
436
  entry.controller = null;
437
  if (!blob || blob.size !== part.bytes) miss();
438
  else storeClip(key, entry, blob);
 
474
  value.jobs.delete(job.batch.start);
475
  release(job.id);
476
  }
477
+ /** @param {SpeechOwner} value */
478
+ function discardUnready(value, keepFollowing = false) {
479
+ const { start, end } = value.batch;
480
+ for (const job of Array.from(value.jobs.values())) {
481
+ if (job.batch.start !== start && (!keepFollowing || job.batch.start !== end) && job.state !== "ready") discard(value, job);
482
+ }
483
+ }
484
  function playbackSettings() { return { rate: Number(speed.value), timer: timer.value, deadline: sleepDeadline }; }
485
  function clearSleep() {
486
  clearTimeout(sleepTimer); sleepDeadline = 0; finishSentence = false; timer.value = "off";
 
495
  finishSentence = timer.value === "clip";
496
  if (finishSentence && owner) {
497
  cancelHandoff(); clearPreload();
498
+ discardUnready(owner);
499
  }
500
  function tickSleep() { if (!checkSleep() && sleepDeadline) sleepTimer = setTimeout(tickSleep, Math.min(1000, sleepDeadline - Date.now())); }
501
  tickSleep();
 
600
  if (!owner) { if (restart) { retryStart = restart; notice("已暂停"); controls(); } return; }
601
  owner.wantsPlay = false;
602
  clearPreload();
603
+ discardUnready(owner);
604
  /** @type {HTMLAudioElement} */ (audio).pause(); cancelHandoff(); if (retiring) retire(); notice("已暂停"); controls();
605
  if ("mediaSession" in navigator) navigator.mediaSession.playbackState = "paused";
606
  }
 
1094
  value.position = offset; value.batch = job.batch; value.part = index; value.wantsPlay = playing;
1095
  value.blocked = false;
1096
  // Do not let synthesis of a now irrelevant batch hold up the target job.
1097
+ discardUnready(value, playing);
 
 
 
1098
  const player = /** @type {HTMLAudioElement} */ (audio);
1099
  if (current?.job === job && current.part.index === index && player.readyState >= 1 && !player.error) {
1100
  player.currentTime = 0;