refactor: 重构作品播放页性能、媒体同步与内存管理

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-04 01:31:10 +08:00
co-authored by Cursor
parent 990e1b1804
commit 10f2520279
19 changed files with 1351 additions and 813 deletions
+56
View File
@@ -91,3 +91,59 @@ export function formatAbsoluteUTC(date: string | Date): string {
const s = String(d.getUTCSeconds()).padStart(2, "0");
return `${Y}-${M}-${D} ${h}:${m}:${s} UTC`;
}
// 主动释放 HTMLMediaElement 解码缓冲与网络流,防止 SPA 连续切页导致内存/硬件解码器泄漏
export function releaseMediaElement(
el: HTMLMediaElement | null | undefined,
): void {
if (!el) return;
try {
el.pause();
el.removeAttribute("src");
el.load();
} catch {}
}
const PROGRESS_STORAGE_PREFIX = "aweme_progress_";
const PROGRESS_TTL_MS = 5 * 60 * 1000;
const MAX_PROGRESS_ENTRIES = 30;
// 清理过期或超量的视频播放进度缓存,避免长期浏览堆积 localStorage
export function cleanupExpiredProgressStorage(): void {
if (typeof window === "undefined") return;
try {
const now = Date.now();
const validEntries: { key: string; timestamp: number }[] = [];
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i);
if (!key || !key.startsWith(PROGRESS_STORAGE_PREFIX)) continue;
const raw = localStorage.getItem(key);
if (!raw) {
localStorage.removeItem(key);
continue;
}
try {
const parsed = JSON.parse(raw) as { timestamp?: unknown };
const timestamp =
typeof parsed.timestamp === "number" ? parsed.timestamp : 0;
if (now - timestamp >= PROGRESS_TTL_MS) {
localStorage.removeItem(key);
} else {
validEntries.push({ key, timestamp });
}
} catch {
localStorage.removeItem(key);
}
}
if (validEntries.length > MAX_PROGRESS_ENTRIES) {
validEntries.sort((a, b) => b.timestamp - a.timestamp);
for (let i = MAX_PROGRESS_ENTRIES; i < validEntries.length; i++) {
localStorage.removeItem(validEntries[i].key);
}
}
} catch {}
}