dsh-folia 4.23.11 → 4.23.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +414 -36
- package/lib/routes/diag.js +27 -0
- package/lib/routes/site-media.js +113 -16
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -20152,13 +20152,10 @@ window.__ModuleLoader__.load({
|
|
|
20152
20152
|
try { warmVideoForPointer(ev, true); } catch { /* 预热是增强 */ }
|
|
20153
20153
|
},
|
|
20154
20154
|
onClick: (ev) => {
|
|
20155
|
-
// 连点防护(G7
|
|
20156
|
-
//
|
|
20157
|
-
//
|
|
20158
|
-
|
|
20159
|
-
try { lfSay(weT("正在切换,请稍候"), true); } catch { /* ignore */ }
|
|
20160
|
-
return;
|
|
20161
|
-
}
|
|
20155
|
+
// 连点防护(G7)在 4.23.12 改成了 **last-click-wins**:不再"忽略这一次",
|
|
20156
|
+
// 而是让 `weSwitchBegin()` 收口上一次(记 superseded)后立刻开始新的。
|
|
20157
|
+
// 旧写法 `if (weSwitchBusy()) { toast; return; }` 是用户实测"点了没反应"的直接来源:
|
|
20158
|
+
// 慢视频的首帧要 6–9 秒,这段时间里点「本机」里的场景壁纸会被整片吞掉。
|
|
20162
20159
|
// 状态机与 applySelection 同生共死:先建上下文(拿 pressed/计时),再真正落地。
|
|
20163
20160
|
try { weSwitchBegin(ev && ev.currentTarget); } catch { /* 反馈是增强 */ }
|
|
20164
20161
|
applySelection(w.id, { fromManual: true });
|
|
@@ -20324,10 +20321,8 @@ window.__ModuleLoader__.load({
|
|
|
20324
20321
|
try { warmVideoForPointer(ev, true); } catch { /* 预热是增强 */ }
|
|
20325
20322
|
},
|
|
20326
20323
|
onClick: (ev) => {
|
|
20327
|
-
if (weSwitchBusy())
|
|
20328
|
-
|
|
20329
|
-
return;
|
|
20330
|
-
}
|
|
20324
|
+
// **last-click-wins**(4.23.12,P0):这里不再有 `if (weSwitchBusy()) return;`。
|
|
20325
|
+
// `weSwitchBegin()` 会把上一次未结算的切换**收口**(记 superseded)再开新的一轮。
|
|
20331
20326
|
try { weSwitchBegin(ev && ev.currentTarget); } catch { /* 反馈是增强 */ }
|
|
20332
20327
|
applySelection(w.id, { fromManual: true });
|
|
20333
20328
|
},
|
|
@@ -21180,17 +21175,11 @@ window.__ModuleLoader__.load({
|
|
|
21180
21175
|
const useCached = Boolean(rc && rc.url && rc.fps === selection.fpsCap && rc.token === tok);
|
|
21181
21176
|
if ((!prepared && !warmed)
|
|
21182
21177
|
|| (useCached && media.dataset.weTranscoded !== String(rc.fps))) {
|
|
21183
|
-
//
|
|
21184
|
-
//
|
|
21185
|
-
//
|
|
21186
|
-
//
|
|
21187
|
-
//
|
|
21188
|
-
media.src = useCached ? rc.url : sel.url;
|
|
21189
|
-
if (useCached) {
|
|
21190
|
-
try { media.dataset.weTranscoded = String(rc.fps); } catch { /* ignore */ }
|
|
21191
|
-
selection.transcodeState = "ready";
|
|
21192
|
-
selection.transcodeProgress = null;
|
|
21193
|
-
}
|
|
21178
|
+
// ⚠️ **poster 必须先于 src**(4.23.12 / 需求 A):`src` 一赋上,浏览器就开始资源选择,
|
|
21179
|
+
// 到首帧之间屏上是**空元素**(视频档通常没有原生海报)。先挂 poster 的话,这张缩略图
|
|
21180
|
+
// (站点 10 KB 级的 webp/jpg)几乎立刻就能画出来 —— 用户点下去 <=50ms 看到的是"这张壁纸的图",
|
|
21181
|
+
// 而不是"什么都没有"。顺序反了这条收益就没了(实测:先 src 后 poster 时海报要等资源选择让位)。
|
|
21182
|
+
//
|
|
21194
21183
|
// poster=预览图:覆盖初始加载与抽帧转码 swap 的空窗(原黑屏闪烁点)。
|
|
21195
21184
|
// 视频类壁纸**不设 WE 自带的 `preview.gif`** —— 动图当 poster 会"先播预览、再停首帧、
|
|
21196
21185
|
// 最后进正片";改为**只认插件自己抽的静态缩略图**(`/video-preview/…` 是 ffmpeg 抽的一帧
|
|
@@ -21210,6 +21199,18 @@ window.__ModuleLoader__.load({
|
|
|
21210
21199
|
const stillPreview = !!sel.previewUrl
|
|
21211
21200
|
&& (sel.type !== "video" || /\/video-preview\//.test(sel.previewUrl) || siteStaticThumb);
|
|
21212
21201
|
if (stillPreview) media.poster = sel.previewUrl;
|
|
21202
|
+
// 首帧之前就能看见的那张图先就位,然后才让浏览器去取流。
|
|
21203
|
+
media.src = useCached ? rc.url : sel.url;
|
|
21204
|
+
// 已经转好的抽帧版(上一次在"已上屏"状态下就绪、刻意没换源的那一份):
|
|
21205
|
+
// **建层时就用它当 src** —— 这样整个生命周期里一次换源都不发生(换源 = 清掉当前帧 = 纯色)。
|
|
21206
|
+
// 判据只看"这份抽帧版是不是当前上限的":**不看原生可解性** —— 帧率上限的意义就是压解码
|
|
21207
|
+
// 占用,原生可解的源照样可能帧率超标(4K120 的 H.264),拿它当免转条件会让上限在实际
|
|
21208
|
+
// 在用的 mp4 上完全失效(见 capNeedsTranscode 的注释)。
|
|
21209
|
+
if (useCached) {
|
|
21210
|
+
try { media.dataset.weTranscoded = String(rc.fps); } catch { /* ignore */ }
|
|
21211
|
+
selection.transcodeState = "ready";
|
|
21212
|
+
selection.transcodeProgress = null;
|
|
21213
|
+
}
|
|
21213
21214
|
}
|
|
21214
21215
|
// 池元素:把"装载去重 + 预热到首帧"交给池。上面那条 `media.src = …` 已经赋过地址,
|
|
21215
21216
|
// `weSwitchArm` 据此**不会重复 load**(需求 A5:同一 src 已加载 ⇒ 直接 play);
|
|
@@ -21219,6 +21220,9 @@ window.__ModuleLoader__.load({
|
|
|
21219
21220
|
weSwitchAttachPlayer(media, armedSrc, {
|
|
21220
21221
|
is1080p: Boolean(useCached),
|
|
21221
21222
|
cacheFamilyHit: sel.cacheFamilyHit === true,
|
|
21223
|
+
// 4.23.12:类型与站点原始地址 —— 度量按类型分簇、下载进度与"切本地副本"都靠它们。
|
|
21224
|
+
kind: "video",
|
|
21225
|
+
originUrl: sel.originUrl || "",
|
|
21222
21226
|
});
|
|
21223
21227
|
weSwitchArm(media, armedSrc, null);
|
|
21224
21228
|
}
|
|
@@ -22977,9 +22981,18 @@ window.__ModuleLoader__.load({
|
|
|
22977
22981
|
// 交叉淡入时长:150–250ms 是需求区间,取中值。短于此观感是"跳变",长于此会被
|
|
22978
22982
|
// 当成"又卡了一下"——两个方向都坏。
|
|
22979
22983
|
const WE_SWITCH_FADE_MS = 200;
|
|
22980
|
-
//
|
|
22981
|
-
//
|
|
22982
|
-
|
|
22984
|
+
// 缓冲提示阈值(4.23.12:1500 → **300**)。
|
|
22985
|
+
// 用户口径改了:**300ms 没出帧就算"慢"**,必须当场告诉他"在缓冲,别以为没点上"。
|
|
22986
|
+
// 1.5s 的旧值意味着一次 8 秒的首帧里有 1.5 秒是**完全静默**的 —— 那正是"点了没反应"。
|
|
22987
|
+
const WE_SWITCH_BUFFER_HINT_MS = 300;
|
|
22988
|
+
// 缓冲进度的轮询间隔(读 `/folia/downloads/list` 的 received/total)。
|
|
22989
|
+
// 500ms:既能让"x%(a/b MB)"看起来在动,又不会把宿主的列表接口打成热点。
|
|
22990
|
+
const WE_SWITCH_PROGRESS_POLL_MS = 500;
|
|
22991
|
+
// **终态保证**(4.23.12):点击后这么久还停在 priming/preloading 且**没有装上任何播放器**
|
|
22992
|
+
// ⇒ 说明这条分支根本没有结算腿(最典型:场景/网页壁纸走了 `applySelection` 的早退,
|
|
22993
|
+
// 而非视频的结算点就写在早退之后)。这时必须自己收口,否则 `weSwitchBusy()` 恒真,
|
|
22994
|
+
// **之后每一次点击都被连点防护吞掉** —— 用户看到的就是"场景壁纸点了完全没反应"。
|
|
22995
|
+
const WE_SWITCH_SETTLE_GUARD_MS = 2500;
|
|
22983
22996
|
// 度量环形缓冲:只留最近 20 次(需求 D9)。
|
|
22984
22997
|
const WE_SWITCH_STATS_MAX = 20;
|
|
22985
22998
|
// 掉帧判定:< 24fps 且持续 2s(需求 D10)。
|
|
@@ -22994,7 +23007,12 @@ window.__ModuleLoader__.load({
|
|
|
22994
23007
|
const WE_SWITCH_PRESS_MS = 1200;
|
|
22995
23008
|
// 状态机硬上限:真正的"永远等不到"必须收口,否则连点会被永久挡住。
|
|
22996
23009
|
// 取值远大于缓冲阈值,让缓冲提示有充分的展示窗口。
|
|
22997
|
-
|
|
23010
|
+
// 状态机硬上限(4.23.12:15000 → **5000**)。
|
|
23011
|
+
// 为什么收短:这是"结算腿断了"的最后一道兜底,而 `weSwitchBusy()` 在这段时间里会挡住/影响
|
|
23012
|
+
// 后续点击的观感。15s 的上限意味着一次漏结算 = 用户 15 秒内点哪都没反应;
|
|
23013
|
+
// 5s 与实测的慢首帧(4K60 ≈ 6–9s)仍然同量级,但**不会**把"点了没反应"放大成一个半屏的沉默。
|
|
23014
|
+
// 收口记 `reason:'busy-timeout'`,便于在 `/folia/diag/switch-stats` 里一眼认出这一类。
|
|
23015
|
+
const WE_SWITCH_HARD_MS = 5000;
|
|
22998
23016
|
|
|
22999
23017
|
// ── 池与状态 ──────────────────────────────────────────────────────────────────
|
|
23000
23018
|
const weSwitchPool = []; // [{ id, el }] 长度恒 ≤ WE_SWITCH_POOL_SIZE
|
|
@@ -23220,6 +23238,71 @@ window.__ModuleLoader__.load({
|
|
|
23220
23238
|
if (el) { try { el.dataset.wePlayerId = "we-detached"; } catch { /* ignore */ } }
|
|
23221
23239
|
return el;
|
|
23222
23240
|
}
|
|
23241
|
+
// ── 缓冲提示:文案(纯函数)+ 覆盖层 ──────────────────────────────────────────
|
|
23242
|
+
/**
|
|
23243
|
+
* 缓冲提示文案。**纯函数**(判据直接喂数字断言,不碰 DOM)。
|
|
23244
|
+
*
|
|
23245
|
+
* 格式(需求 B):`缓冲中 x%(a/b MB · 约剩 N 秒)`
|
|
23246
|
+
* · 总量未知 ⇒ 只给「缓冲中…」(**绝不编数字**);
|
|
23247
|
+
* · 没有速度样本(`etaSec <= 0`)⇒ 省掉"约剩 N 秒"那一截(同上)。
|
|
23248
|
+
*/
|
|
23249
|
+
function weSwitchBufferHintText(recv, total, etaSec) {
|
|
23250
|
+
const r = Math.max(0, Number(recv) || 0);
|
|
23251
|
+
const t = Math.max(0, Number(total) || 0);
|
|
23252
|
+
const pct = t > 0 ? Math.max(0, Math.min(100, Math.round((r / t) * 100))) : 0;
|
|
23253
|
+
if (!(total > 0)) return "缓冲中…";
|
|
23254
|
+
const a = (r / 1048576).toFixed(1);
|
|
23255
|
+
const b = (t / 1048576).toFixed(1);
|
|
23256
|
+
const s = Math.max(0, Math.round(Number(etaSec) || 0));
|
|
23257
|
+
return s > 0
|
|
23258
|
+
? "缓冲中 " + pct + "%(" + a + "/" + b + " MB · 约剩 " + s + " 秒)"
|
|
23259
|
+
: "缓冲中 " + pct + "%(" + a + "/" + b + " MB)";
|
|
23260
|
+
}
|
|
23261
|
+
|
|
23262
|
+
/** 覆盖层元素(惰性创建;`document` 不可用时全程 null,所有函数退化成 no-op)。 */
|
|
23263
|
+
let weSwitchOverlayEl = null;
|
|
23264
|
+
|
|
23265
|
+
/** 取/建覆盖层。挂在 `body` 上,`pointer-events:none`(不许挡住播放控制)。 */
|
|
23266
|
+
function weSwitchOverlayEnsure() {
|
|
23267
|
+
if (weSwitchOverlayEl) return weSwitchOverlayEl;
|
|
23268
|
+
if (typeof document === "undefined" || !document.body) return null;
|
|
23269
|
+
if (typeof document.createElement !== "function") return null;
|
|
23270
|
+
let el;
|
|
23271
|
+
try { el = document.createElement("div"); } catch { return null; }
|
|
23272
|
+
if (!el) return null;
|
|
23273
|
+
try { el.className = "we-switch-hint"; } catch { /* 精简 DOM */ }
|
|
23274
|
+
try {
|
|
23275
|
+
el.setAttribute("role", "status");
|
|
23276
|
+
el.setAttribute("aria-live", "polite");
|
|
23277
|
+
// 只放"看得见的一块胶囊":不抢焦点、不挡鼠标;颜色走 CSS 变量,主题跟随既有机制。
|
|
23278
|
+
el.style.cssText = "position:fixed;left:50%;bottom:14%;transform:translateX(-50%);"
|
|
23279
|
+
+ "padding:8px 16px;border-radius:999px;font-size:13px;line-height:1.4;"
|
|
23280
|
+
+ "pointer-events:none;z-index:2147483000;color:#fff;"
|
|
23281
|
+
+ "background:rgba(0,0,0,.62);backdrop-filter:blur(6px);display:none;";
|
|
23282
|
+
} catch { /* ignore */ }
|
|
23283
|
+
try { document.body.appendChild(el); } catch { return null; }
|
|
23284
|
+
weSwitchOverlayEl = el;
|
|
23285
|
+
return el;
|
|
23286
|
+
}
|
|
23287
|
+
|
|
23288
|
+
/** 显示/刷新缓冲提示(幂等;`text` 为空 ⇒ 隐藏)。 */
|
|
23289
|
+
function weSwitchOverlaySet(text) {
|
|
23290
|
+
const el = weSwitchOverlayEnsure();
|
|
23291
|
+
if (!el) return;
|
|
23292
|
+
const s = String(text || "");
|
|
23293
|
+
try {
|
|
23294
|
+
if (!s) { el.style.display = "none"; return; }
|
|
23295
|
+
el.textContent = s;
|
|
23296
|
+
el.style.display = "block";
|
|
23297
|
+
} catch { /* ignore */ }
|
|
23298
|
+
}
|
|
23299
|
+
|
|
23300
|
+
/** 收起覆盖层(结算/失败/成功都要调;幂等)。 */
|
|
23301
|
+
function weSwitchOverlayHide() {
|
|
23302
|
+
if (!weSwitchOverlayEl) return;
|
|
23303
|
+
try { weSwitchOverlayEl.style.display = "none"; } catch { /* ignore */ }
|
|
23304
|
+
}
|
|
23305
|
+
|
|
23223
23306
|
// ── 切换度量 ──────────────────────────────────────────────────────────────────
|
|
23224
23307
|
|
|
23225
23308
|
function weSwitchNow() {
|
|
@@ -23297,6 +23380,20 @@ window.__ModuleLoader__.load({
|
|
|
23297
23380
|
* · 记 `clickFeedbackMs` = 到"第一处视觉变化"的耗时(同步 ⇒ 实测 < 1ms)。
|
|
23298
23381
|
*/
|
|
23299
23382
|
function weSwitchBegin(el) {
|
|
23383
|
+
// **last-click-wins(4.23.12,P0 的关键那一半)**:上一次切换还没结算时又来一次点击,
|
|
23384
|
+
// 说明用户已经不想等前一张了 —— **立刻把它收口**(记 `superseded`),然后开新的一轮。
|
|
23385
|
+
//
|
|
23386
|
+
// 为什么不是"忽略这一次"(旧实现):忽略的前提是"前一次必然会在可接受的时间内结算"。
|
|
23387
|
+
// 实测否证了这个前提 —— 慢视频的首帧要 6–9 秒,而这一段时间里用户去点「本机」里的场景壁纸
|
|
23388
|
+
// 就被判成"正在切换,请稍候"而**完全没反应**。用户的结论是"连本机都不能点了"。
|
|
23389
|
+
// 取消式没有死锁风险:旧上下文被置为 failed/settled 之后,busy 立刻变假。
|
|
23390
|
+
const prev = weSwitchCtx;
|
|
23391
|
+
if (prev && !prev.settled) {
|
|
23392
|
+
try { weSwitchFail("superseded"); } catch { /* 收口失败也要继续开新的 */ }
|
|
23393
|
+
}
|
|
23394
|
+
if (weSwitchPending) weSwitchPending = null; // 旧实现的"补切最后一个"不再参与(取消式取代)
|
|
23395
|
+
// 新一轮切换开始:上一张的"等本地副本"到此为止(它已经不在屏上了)。
|
|
23396
|
+
weSwitchStopLocalWatch();
|
|
23300
23397
|
// 复用 ptrdown 那一枪的视觉反馈(如果有):同一次点击只该量一段"点击 → 第一处视觉变化"。
|
|
23301
23398
|
const pressed = weSwitchConsumePress();
|
|
23302
23399
|
const t0 = (pressed && pressed.at) || weSwitchNow();
|
|
@@ -23310,30 +23407,99 @@ window.__ModuleLoader__.load({
|
|
|
23310
23407
|
const feedbackMs = (pressed && pressed.ms) || 0;
|
|
23311
23408
|
const ctx = {
|
|
23312
23409
|
startedAt: t0, feedbackMs: (pressed && pressed.ms) || 0, state: "priming",
|
|
23313
|
-
cardEl: target, pendingEl: null, bufferTimer: 0, pressTimer: 0, hardTimer: 0,
|
|
23410
|
+
cardEl: target, pendingEl: null, bufferTimer: 0, pressTimer: 0, hardTimer: 0, settleGuard: 0,
|
|
23314
23411
|
slug: "-", cacheFamilyHit: false, is1080p: false, settled: false,
|
|
23412
|
+
// 4.23.12:这次点的是哪一类(video / scene / web / image)。度量与排障都要它 ——
|
|
23413
|
+
// "某一类点了没反应"在延迟直方图上只表现为"恰好 15s",没有 kind 就分不出是谁。
|
|
23414
|
+
kind: "unknown",
|
|
23415
|
+
// 4.23.12:本条壁纸的**站点原始地址**(下载/进度都用它,不是播放代理地址)。
|
|
23416
|
+
originUrl: "", bytesRecv: 0, bytesTotal: 0, etaSec: 0, progressPoll: 0, progressLast: null,
|
|
23315
23417
|
};
|
|
23316
23418
|
weSwitchCtx = ctx;
|
|
23317
23419
|
if (typeof setTimeout === "function") {
|
|
23318
23420
|
// 视觉兜底:只摘 pressed / aria-busy,**不结算**(见 WE_SWITCH_PRESS_MS 的注释)。
|
|
23319
23421
|
ctx.pressTimer = setTimeout(() => { weSwitchResetCard(ctx); }, WE_SWITCH_PRESS_MS);
|
|
23320
|
-
//
|
|
23422
|
+
// 缓冲提示:**300ms** 还没进到可播状态 ⇒ 转 buffering、出「缓冲中 x%」并开始取进度。
|
|
23321
23423
|
ctx.bufferTimer = setTimeout(() => {
|
|
23322
23424
|
if (ctx.settled || weSwitchCtx !== ctx) return;
|
|
23323
23425
|
if (ctx.state === "priming" || ctx.state === "preloading") {
|
|
23324
23426
|
weSwitchSetState(ctx, "buffering");
|
|
23427
|
+
weSwitchOverlaySet(weSwitchBufferHintText(ctx.bytesRecv, ctx.bytesTotal, ctx.etaSec));
|
|
23428
|
+
weSwitchStartProgressPoll(ctx);
|
|
23325
23429
|
weSwitchListens(ctx);
|
|
23326
23430
|
}
|
|
23327
23431
|
}, WE_SWITCH_BUFFER_HINT_MS);
|
|
23432
|
+
// **终态保证**(4.23.12,P0):到点仍停在 priming 且**没有任何播放器被装上** ⇒
|
|
23433
|
+
// 这条分支没有结算腿(早退 / 异常 / 未知类型)。自己收口,绝不让"切换中"永久为真。
|
|
23434
|
+
// 已经有播放器(`pendingEl`)时不动它:视频档由首帧结算,最差由 15s 硬上限兜底。
|
|
23435
|
+
ctx.settleGuard = setTimeout(() => {
|
|
23436
|
+
if (ctx.settled || weSwitchCtx !== ctx) return;
|
|
23437
|
+
if (ctx.pendingEl) return;
|
|
23438
|
+
// priming(还没装)/ preloading(装了但没有元素)/ buffering(300ms 后转过来的那个)
|
|
23439
|
+
// 三种"没有任何播放器在等"的形态都必须收口。有元素时交给首帧与 5s 硬上限。
|
|
23440
|
+
if (ctx.state !== "priming" && ctx.state !== "preloading" && ctx.state !== "buffering") return;
|
|
23441
|
+
weSwitchSetState(ctx, "playing");
|
|
23442
|
+
weSwitchFinish(ctx, true, "no-media-branch");
|
|
23443
|
+
}, WE_SWITCH_SETTLE_GUARD_MS);
|
|
23328
23444
|
// 硬上限:真正的"永远等不到"收口。
|
|
23329
23445
|
ctx.hardTimer = setTimeout(() => {
|
|
23330
23446
|
if (ctx.settled || weSwitchCtx !== ctx) return;
|
|
23331
|
-
weSwitchFail("timeout");
|
|
23447
|
+
weSwitchFail("busy-timeout");
|
|
23332
23448
|
}, WE_SWITCH_HARD_MS);
|
|
23333
23449
|
}
|
|
23334
23450
|
return ctx;
|
|
23335
23451
|
}
|
|
23336
23452
|
|
|
23453
|
+
/**
|
|
23454
|
+
* 缓冲期间的进度来源:`/folia/downloads/list` 里与 `originUrl` 匹配的那条 job。
|
|
23455
|
+
*
|
|
23456
|
+
* 为什么读队列而不是读 `<video>.buffered`:`buffered` 只回答"解码器手上有什么",
|
|
23457
|
+
* 用户问的是"还要多久"——那是**下载**的事实(received / total),只有队列知道。
|
|
23458
|
+
* 取不到就保持"缓冲中…"(绝不编百分比)。
|
|
23459
|
+
*/
|
|
23460
|
+
function weSwitchStartProgressPoll(ctx) {
|
|
23461
|
+
if (!ctx || ctx.progressPoll) return;
|
|
23462
|
+
if (typeof setInterval !== "function") return;
|
|
23463
|
+
if (typeof apiJson !== "function") return;
|
|
23464
|
+
const tick = () => {
|
|
23465
|
+
if (ctx.settled || weSwitchCtx !== ctx) { weSwitchStopProgressPoll(ctx); return; }
|
|
23466
|
+
if (!ctx.originUrl) return;
|
|
23467
|
+
Promise.resolve()
|
|
23468
|
+
.then(() => apiJson("/downloads/list"))
|
|
23469
|
+
.then((res) => {
|
|
23470
|
+
const doc = (res && res.data) || res || {};
|
|
23471
|
+
const jobs = Array.isArray(doc.jobs) ? doc.jobs : [];
|
|
23472
|
+
const job = jobs.find((j) => j && String(j.url || "") === String(ctx.originUrl)) || null;
|
|
23473
|
+
if (!job) return;
|
|
23474
|
+
const recv = Math.max(0, Number(job.received) || 0);
|
|
23475
|
+
const total = Math.max(0, Number(job.total) || 0);
|
|
23476
|
+
// 速度样本:两次采样的字节差 / 时间差 ⇒ 约剩秒数(样本不足就不给)。
|
|
23477
|
+
const now = Date.now();
|
|
23478
|
+
const last = ctx.progressLast;
|
|
23479
|
+
if (last && now > last.at && recv > last.recv) {
|
|
23480
|
+
const bps = (recv - last.recv) / ((now - last.at) / 1000);
|
|
23481
|
+
if (bps > 0 && total > recv) ctx.etaSec = Math.round((total - recv) / bps);
|
|
23482
|
+
}
|
|
23483
|
+
ctx.progressLast = { at: now, recv };
|
|
23484
|
+
ctx.bytesRecv = recv;
|
|
23485
|
+
ctx.bytesTotal = total;
|
|
23486
|
+
weSwitchOverlaySet(weSwitchBufferHintText(recv, total, ctx.etaSec));
|
|
23487
|
+
weSwitchListens(ctx);
|
|
23488
|
+
})
|
|
23489
|
+
.catch(() => { /* 进度是增强:拿不到就保持上一帧文案 */ });
|
|
23490
|
+
};
|
|
23491
|
+
ctx.progressPoll = setInterval(tick, WE_SWITCH_PROGRESS_POLL_MS);
|
|
23492
|
+
try { if (ctx.progressPoll && typeof ctx.progressPoll.unref === "function") ctx.progressPoll.unref(); } catch { /* ignore */ }
|
|
23493
|
+
tick();
|
|
23494
|
+
}
|
|
23495
|
+
|
|
23496
|
+
/** 停进度轮询(幂等:结算、失败、离开 buffering 都调)。 */
|
|
23497
|
+
function weSwitchStopProgressPoll(ctx) {
|
|
23498
|
+
if (!ctx || !ctx.progressPoll) return;
|
|
23499
|
+
if (typeof clearInterval === "function") { try { clearInterval(ctx.progressPoll); } catch { /* ignore */ } }
|
|
23500
|
+
ctx.progressPoll = 0;
|
|
23501
|
+
}
|
|
23502
|
+
|
|
23337
23503
|
/** 摘掉卡片的 pressed / aria-busy(视觉复位;幂等,结算与兜底都调它)。 */
|
|
23338
23504
|
function weSwitchResetCard(ctx) {
|
|
23339
23505
|
if (!ctx || !ctx.cardEl) return;
|
|
@@ -23361,7 +23527,13 @@ window.__ModuleLoader__.load({
|
|
|
23361
23527
|
const ev = {
|
|
23362
23528
|
state: ctx.state,
|
|
23363
23529
|
slug: ctx.slug,
|
|
23364
|
-
|
|
23530
|
+
kind: ctx.kind || "unknown",
|
|
23531
|
+
percent: ctx.bytesTotal > 0 ? Math.min(100, Math.round((ctx.bytesRecv / ctx.bytesTotal) * 100)) : 0,
|
|
23532
|
+
recvBytes: Math.max(0, Number(ctx.bytesRecv) || 0),
|
|
23533
|
+
totalBytes: Math.max(0, Number(ctx.bytesTotal) || 0),
|
|
23534
|
+
etaSec: Math.max(0, Number(ctx.etaSec) || 0),
|
|
23535
|
+
// 覆盖层上正在显示的那一行(面板若要自己画,直接用它,别各写一份格式化)。
|
|
23536
|
+
hint: weSwitchBufferHintText(ctx.bytesRecv, ctx.bytesTotal, ctx.etaSec),
|
|
23365
23537
|
reason: ctx.reason || "",
|
|
23366
23538
|
latencyMs: Number(ctx.latencyMs) || 0,
|
|
23367
23539
|
feedbackMs: Number(ctx.feedbackMs) || 0,
|
|
@@ -23380,7 +23552,12 @@ window.__ModuleLoader__.load({
|
|
|
23380
23552
|
ctx.slug = weSwitchSlugOf(url);
|
|
23381
23553
|
ctx.is1080p = o.is1080p === true || weSwitchIs1080p(url);
|
|
23382
23554
|
ctx.cacheFamilyHit = o.cacheFamilyHit === true;
|
|
23555
|
+
// 4.23.12:类型与站点原始地址(进度/切本地都要它)。
|
|
23556
|
+
if (o.kind) ctx.kind = String(o.kind);
|
|
23557
|
+
if (o.originUrl) ctx.originUrl = String(o.originUrl);
|
|
23383
23558
|
if (!ctx.settled) weSwitchSetState(ctx, "preloading");
|
|
23559
|
+
// 已经进入 buffering 的上下文,装上播放器后进度轮询要能跟上(幂等)。
|
|
23560
|
+
if (ctx.state === "buffering") weSwitchStartProgressPoll(ctx);
|
|
23384
23561
|
return ctx;
|
|
23385
23562
|
}
|
|
23386
23563
|
|
|
@@ -23392,13 +23569,26 @@ window.__ModuleLoader__.load({
|
|
|
23392
23569
|
weSwitchVisibleEl = el;
|
|
23393
23570
|
weSwitchStartJankProbe(el);
|
|
23394
23571
|
}
|
|
23572
|
+
// 首帧已上屏 ⇒ 缓冲提示立刻收掉(交叉淡入期间还挂着"缓冲中"是自相矛盾的观感)。
|
|
23573
|
+
weSwitchOverlayHide();
|
|
23395
23574
|
if (ctx && !ctx.settled) {
|
|
23575
|
+
weSwitchStopProgressPoll(ctx);
|
|
23396
23576
|
weSwitchSetState(ctx, "swapping");
|
|
23397
|
-
|
|
23398
|
-
|
|
23399
|
-
|
|
23577
|
+
// **结算点前移**(4.23.12 / P0):层一旦上屏(poster 或首帧已经挂进 DOM、交叉淡入开始),
|
|
23578
|
+
// 这一次切换对用户来说就"已经生效"了 —— 到此结算,**不等第一帧解码完成**。
|
|
23579
|
+
//
|
|
23580
|
+
// 旧行为:`o.fadeMs` 只在极少数调用点给过,`{}` 传来的那一次**根本不排结算**,于是
|
|
23581
|
+
// 状态机要一直等到 `weSwitchPlayed()` —— 而那条腿只在 `syncLayers` 里、`readyState >= 2`
|
|
23582
|
+
// 时才被调用,`syncLayers` 又要等一次 emit。实测结果就是 busy 窗口 = 首帧耗时 = 6–9 秒,
|
|
23583
|
+
// 这期间点别的(尤其"本机"里的场景壁纸)全部被连点防护吞掉 = 用户说的"点了没反应"。
|
|
23584
|
+
const at = ctx;
|
|
23585
|
+
const fade = Math.max(0, Number(o.fadeMs) || WE_SWITCH_FADE_MS);
|
|
23586
|
+
if (typeof setTimeout === "function") {
|
|
23587
|
+
setTimeout(() => { weSwitchFinish(at, true, ""); }, fade);
|
|
23400
23588
|
return ctx;
|
|
23401
23589
|
}
|
|
23590
|
+
weSwitchFinish(at, true, "");
|
|
23591
|
+
return ctx;
|
|
23402
23592
|
}
|
|
23403
23593
|
return ctx;
|
|
23404
23594
|
}
|
|
@@ -23434,15 +23624,21 @@ window.__ModuleLoader__.load({
|
|
|
23434
23624
|
if (ctx.pressTimer) { try { clearTimeout(ctx.pressTimer); } catch { /* ignore */ } }
|
|
23435
23625
|
if (ctx.bufferTimer) { try { clearTimeout(ctx.bufferTimer); } catch { /* ignore */ } }
|
|
23436
23626
|
if (ctx.hardTimer) { try { clearTimeout(ctx.hardTimer); } catch { /* ignore */ } }
|
|
23627
|
+
if (ctx.settleGuard) { try { clearTimeout(ctx.settleGuard); } catch { /* ignore */ } }
|
|
23437
23628
|
}
|
|
23438
23629
|
ctx.pressTimer = 0;
|
|
23439
23630
|
ctx.bufferTimer = 0;
|
|
23440
23631
|
ctx.hardTimer = 0;
|
|
23632
|
+
ctx.settleGuard = 0;
|
|
23633
|
+
weSwitchStopProgressPoll(ctx);
|
|
23634
|
+
// 结算即收提示:屏上要么已经有真画面,要么就有失败文案 —— 都不该再挂着"缓冲中"。
|
|
23635
|
+
weSwitchOverlayHide();
|
|
23441
23636
|
weSwitchSetState(ctx, ok ? "playing" : "failed");
|
|
23442
23637
|
// 卡片复位:pressed 摘掉、aria-busy 放下(需求 H14 / G4)。
|
|
23443
23638
|
weSwitchResetCard(ctx);
|
|
23444
23639
|
weSwitchRecord({
|
|
23445
23640
|
slug: ctx.slug,
|
|
23641
|
+
kind: ctx.kind || "unknown",
|
|
23446
23642
|
cacheFamilyHit: ctx.cacheFamilyHit === true,
|
|
23447
23643
|
is1080p: ctx.is1080p === true,
|
|
23448
23644
|
switchLatencyMs: Math.round(ctx.latencyMs),
|
|
@@ -23452,6 +23648,9 @@ window.__ModuleLoader__.load({
|
|
|
23452
23648
|
});
|
|
23453
23649
|
weSwitchListens(ctx);
|
|
23454
23650
|
if (weSwitchCtx === ctx) weSwitchCtx = null;
|
|
23651
|
+
// 视频档:首帧已上屏后开始等**本地副本**(下载完成就无缝切过去,需求 C)。
|
|
23652
|
+
// 非视频档没有 `<video>`,这条链自然不启动(`pendingEl` 为空)。
|
|
23653
|
+
if (ok === true && ctx.pendingEl) { try { weSwitchBeginLocalWatch(ctx); } catch { /* 增强 */ } }
|
|
23455
23654
|
// 连点防护:上一轮结束了,补切"最后点的那个"(需求 G7)。放在清空上下文之后 ——
|
|
23456
23655
|
// 补切会开启新的一轮,必须在 `weSwitchCtx` 已为空时进行,否则新上下文会被这里覆盖。
|
|
23457
23656
|
weSwitchFlushPick();
|
|
@@ -23575,6 +23774,170 @@ window.__ModuleLoader__.load({
|
|
|
23575
23774
|
return ctx;
|
|
23576
23775
|
}
|
|
23577
23776
|
|
|
23777
|
+
/**
|
|
23778
|
+
* 非视频类型(scene / web / image / 本机文件)的结算入口 —— **带类型**。
|
|
23779
|
+
*
|
|
23780
|
+
* 为什么要有它而不是直接 `weSwitchSettle()`:`kind` 是排障时区分"哪一类卡住"的唯一字段
|
|
23781
|
+
* (4.23.12 加进 `/folia/diag/switch-stats`)。调用点知道类型,状态机不该去猜。
|
|
23782
|
+
*/
|
|
23783
|
+
function weSwitchSettleAs(kind) {
|
|
23784
|
+
const ctx = weSwitchCtx;
|
|
23785
|
+
if (!ctx) return null;
|
|
23786
|
+
if (kind) ctx.kind = String(kind);
|
|
23787
|
+
return weSwitchSettle();
|
|
23788
|
+
}
|
|
23789
|
+
|
|
23790
|
+
// ── 后台下载完成 ⇒ 无缝切到本地文件(需求 C)────────────────────────────────
|
|
23791
|
+
/**
|
|
23792
|
+
* 「播放中」的这张壁纸,本地那一份什么时候到?到了就切过去。
|
|
23793
|
+
*
|
|
23794
|
+
* 为什么要切:代理流是**边下边播**,它的字节来自网络,用户拖动进度条、暂停再播都在打网络;
|
|
23795
|
+
* 而队列下完之后 `/folia/local-media/<job>` 是磁盘直出(实测 TTFB 1–5ms、45 MB 全量 97ms)。
|
|
23796
|
+
* 切过去之后,同一张再点一次就是 200ms 级(需求 D「之后重复点 = 直接用本地」)。
|
|
23797
|
+
*
|
|
23798
|
+
* 不变量:
|
|
23799
|
+
* · **不在在屏元素上换源**(那会清掉当前帧):本地这份装进**池里不在屏上的另一个元素**,
|
|
23800
|
+
* 首帧就绪后交叉淡入、旧元素淡出后才移除;
|
|
23801
|
+
* · 切换前把本地元素的 `currentTime` 对齐到在屏元素的当前时间,**差 > 0.3s 就放弃**
|
|
23802
|
+
* (宁可不切,也不要画面"跳一下");
|
|
23803
|
+
* · 整条链失败全部静默(代理流继续播,功能不受影响)。
|
|
23804
|
+
*/
|
|
23805
|
+
let weSwitchLocalWatch = null; // { timer, slug }
|
|
23806
|
+
|
|
23807
|
+
function weSwitchStopLocalWatch() {
|
|
23808
|
+
const w = weSwitchLocalWatch;
|
|
23809
|
+
weSwitchLocalWatch = null;
|
|
23810
|
+
if (w && w.timer && typeof clearInterval === "function") {
|
|
23811
|
+
try { clearInterval(w.timer); } catch { /* ignore */ }
|
|
23812
|
+
}
|
|
23813
|
+
}
|
|
23814
|
+
|
|
23815
|
+
/** 池里"不在屏上"的那个元素(切换/换源只许动它)。 */
|
|
23816
|
+
function weSwitchPoolPickHidden(except) {
|
|
23817
|
+
const pool = weSwitchPoolEnsure();
|
|
23818
|
+
for (const p of pool) {
|
|
23819
|
+
if (p && p.el && p.el !== except && p.el !== weSwitchVisibleEl) return p.el;
|
|
23820
|
+
}
|
|
23821
|
+
return null;
|
|
23822
|
+
}
|
|
23823
|
+
|
|
23824
|
+
/** 开始等这张壁纸的本地副本(幂等;没有 originUrl / 已命中族 就不必等)。 */
|
|
23825
|
+
function weSwitchBeginLocalWatch(ctx) {
|
|
23826
|
+
const o = ctx || {};
|
|
23827
|
+
if (!o.originUrl || o.cacheFamilyHit === true) return null;
|
|
23828
|
+
const slug = String(o.slug || "");
|
|
23829
|
+
if (weSwitchLocalWatch && weSwitchLocalWatch.slug === slug) return weSwitchLocalWatch;
|
|
23830
|
+
weSwitchStopLocalWatch();
|
|
23831
|
+
if (typeof setInterval !== "function" || typeof apiJson !== "function") return null;
|
|
23832
|
+
const rec = { timer: 0, slug };
|
|
23833
|
+
const tick = () => {
|
|
23834
|
+
if (weSwitchLocalWatch !== rec) return;
|
|
23835
|
+
Promise.resolve()
|
|
23836
|
+
.then(() => apiJson("/downloads/list"))
|
|
23837
|
+
.then((res) => {
|
|
23838
|
+
const doc = (res && res.data) || res || {};
|
|
23839
|
+
const jobs = Array.isArray(doc.jobs) ? doc.jobs : [];
|
|
23840
|
+
const job = jobs.find((j) => j && String(j.url || "") === String(o.originUrl)
|
|
23841
|
+
&& String(j.status || "") === "done");
|
|
23842
|
+
if (!job || !job.id) return;
|
|
23843
|
+
weSwitchStopLocalWatch();
|
|
23844
|
+
weSwitchHandoverLocal("/folia/local-media/" + encodeURIComponent(String(job.id)));
|
|
23845
|
+
})
|
|
23846
|
+
.catch(() => { /* 网络抖动:下一拍再试 */ });
|
|
23847
|
+
};
|
|
23848
|
+
rec.timer = setInterval(tick, 1000);
|
|
23849
|
+
try { if (rec.timer && typeof rec.timer.unref === "function") rec.timer.unref(); } catch { /* ignore */ }
|
|
23850
|
+
weSwitchLocalWatch = rec;
|
|
23851
|
+
return rec;
|
|
23852
|
+
}
|
|
23853
|
+
|
|
23854
|
+
/**
|
|
23855
|
+
* 把播放切到本地副本:预热(不在屏上的池元素)→ 对齐时间 → 交叉淡入。
|
|
23856
|
+
* 返回 true 表示**已开始**淡入(首帧在本元素上就位);false = 放弃(继续播代理流)。
|
|
23857
|
+
*/
|
|
23858
|
+
function weSwitchHandoverLocal(localUrl) {
|
|
23859
|
+
const from = weSwitchVisibleEl;
|
|
23860
|
+
const u = String(localUrl || "");
|
|
23861
|
+
if (!from || !u) return false;
|
|
23862
|
+
if (String(from.__weUrl || "") === u) return false; // 已经在本地那份上了
|
|
23863
|
+
const to = weSwitchPoolPickHidden(from);
|
|
23864
|
+
if (!to) return false;
|
|
23865
|
+
const fromTime = Number(from.currentTime) || 0;
|
|
23866
|
+
try {
|
|
23867
|
+
to.__weUrl = u;
|
|
23868
|
+
to.muted = true;
|
|
23869
|
+
to.src = u;
|
|
23870
|
+
if (typeof to.load === "function") to.load();
|
|
23871
|
+
} catch { return false; }
|
|
23872
|
+
// 先播起来(rVFC 只在出帧时回调),再把时间对齐 —— 顺序不能反:
|
|
23873
|
+
// 未起播时 `currentTime` 的赋值会被随后的资源选择覆盖掉。
|
|
23874
|
+
try {
|
|
23875
|
+
const p = to.play();
|
|
23876
|
+
if (p && typeof p.catch === "function") p.catch(() => { /* 静音元素被策略拒绝:等 loadeddata 再说 */ });
|
|
23877
|
+
} catch { /* ignore */ }
|
|
23878
|
+
try { if (fromTime > 0) to.currentTime = fromTime; } catch { /* ignore */ }
|
|
23879
|
+
|
|
23880
|
+
let done = false;
|
|
23881
|
+
const settleToLocal = () => {
|
|
23882
|
+
if (done) return;
|
|
23883
|
+
done = true;
|
|
23884
|
+
const nowTime = Number(from.currentTime) || 0;
|
|
23885
|
+
const toTime = Number(to.currentTime) || 0;
|
|
23886
|
+
// **对齐判据**:差 > 0.3s 就不切(画面跳一下比继续走网络更糟)。
|
|
23887
|
+
if (Math.abs(toTime - nowTime) > 0.3) return;
|
|
23888
|
+
try { if (Number(from.playbackRate) > 0) to.playbackRate = from.playbackRate; } catch { /* ignore */ }
|
|
23889
|
+
try { to.loop = from.loop === true; } catch { /* ignore */ }
|
|
23890
|
+
weSwitchCrossfadeTo(to, from);
|
|
23891
|
+
};
|
|
23892
|
+
if (typeof to.requestVideoFrameCallback === "function") {
|
|
23893
|
+
try { to.requestVideoFrameCallback(() => settleToLocal()); return true; } catch { /* 退化到事件 */ }
|
|
23894
|
+
}
|
|
23895
|
+
if (typeof to.addEventListener === "function") {
|
|
23896
|
+
const onData = () => settleToLocal();
|
|
23897
|
+
try { to.addEventListener("loadeddata", onData, { once: true }); } catch { /* ignore */ }
|
|
23898
|
+
try { to.addEventListener("canplay", onData, { once: true }); } catch { /* ignore */ }
|
|
23899
|
+
return true;
|
|
23900
|
+
}
|
|
23901
|
+
settleToLocal();
|
|
23902
|
+
return true;
|
|
23903
|
+
}
|
|
23904
|
+
|
|
23905
|
+
/**
|
|
23906
|
+
* 交叉淡入到 `to`,旧元素淡出**之后**才从 DOM 移除(任何时刻都有一帧在屏上 = 不黑屏)。
|
|
23907
|
+
* `to` 插在 `from` **之前**:`layer.querySelector("video")` 因此会返回新的那个。
|
|
23908
|
+
*/
|
|
23909
|
+
function weSwitchCrossfadeTo(to, from) {
|
|
23910
|
+
if (!to || !from) return false;
|
|
23911
|
+
const parent = from.parentElement;
|
|
23912
|
+
if (!parent) return false;
|
|
23913
|
+
try { parent.insertBefore(to, from); } catch { return false; }
|
|
23914
|
+
try {
|
|
23915
|
+
to.style.opacity = "0";
|
|
23916
|
+
to.style.transition = "opacity " + WE_SWITCH_FADE_MS + "ms linear";
|
|
23917
|
+
} catch { /* 精简 DOM:直接当已切换 */ }
|
|
23918
|
+
const reveal = () => {
|
|
23919
|
+
try { to.style.opacity = "1"; } catch { /* ignore */ }
|
|
23920
|
+
if (typeof setTimeout !== "function") { weSwitchFinishLocalSwap(to, from, parent); return; }
|
|
23921
|
+
setTimeout(() => { weSwitchFinishLocalSwap(to, from, parent); }, WE_SWITCH_FADE_MS);
|
|
23922
|
+
};
|
|
23923
|
+
// 强制一次样式读取,确保上面的 opacity:0 真的被应用(否则过渡不会发生)。
|
|
23924
|
+
try { void (parent && parent.offsetWidth); } catch { /* ignore */ }
|
|
23925
|
+
if (typeof setTimeout === "function") setTimeout(reveal, 0); else reveal();
|
|
23926
|
+
return true;
|
|
23927
|
+
}
|
|
23928
|
+
|
|
23929
|
+
/** 淡入结束:移除旧元素、把"在屏元素"换成新的。 */
|
|
23930
|
+
function weSwitchFinishLocalSwap(to, from, parent) {
|
|
23931
|
+
try { if (parent && from && from.parentElement === parent) parent.removeChild(from); } catch { /* ignore */ }
|
|
23932
|
+
try { if (from && typeof from.pause === "function") from.pause(); } catch { /* ignore */ }
|
|
23933
|
+
if (to) {
|
|
23934
|
+
weSwitchVisibleEl = to;
|
|
23935
|
+
weSwitchStartJankProbe(to);
|
|
23936
|
+
}
|
|
23937
|
+
// 切到本地 = 这一张从此"已缓存":复用缓存族标记,面板角标与度量同一口径。
|
|
23938
|
+
try { liveLog("local-handover", "已切到本地副本(后续切换走磁盘)"); } catch { /* ignore */ }
|
|
23939
|
+
}
|
|
23940
|
+
|
|
23578
23941
|
// ── 内联模块:src/media-prep.js —— 选中项落地:预准备(预挂载 + 探测 + 超时记账)→ buildMedia → applySelection(构建期注入;勿手改本段)──
|
|
23579
23942
|
/**
|
|
23580
23943
|
* media-prep.js — **选中的壁纸 → 屏幕上的媒体**:预准备(预挂载 + 探测 + 超时记账)、
|
|
@@ -24056,6 +24419,12 @@ window.__ModuleLoader__.load({
|
|
|
24056
24419
|
// themeFollowRelease)。两条清空路径(用户「清除」与皮肤让路)都汇到这里。
|
|
24057
24420
|
themeFollowRelease();
|
|
24058
24421
|
emit();
|
|
24422
|
+
// **终态保证**(4.23.12):清除壁纸也是一次"切换",不结算就会把状态机永远挂在
|
|
24423
|
+
// "切换中" ⇒ 之后点任何卡片都被连点防护吞掉。
|
|
24424
|
+
// ⚠️ 位置在 `emit()` **之后**:`test/verify-theme-follow.mjs` 用 `/themeFollowRelease\(\);\r?\n\s*emit\(\);/`
|
|
24425
|
+
// 钉着"放回主题 → 再重渲染"这条顺序(顺序反了主题会先画旧色再被覆盖)。结算与它无关,
|
|
24426
|
+
// 排后面既保住那条判据,也保住终态保证。
|
|
24427
|
+
try { if (typeof weSwitchSettleAs === "function") weSwitchSettleAs("cleared"); } catch { /* 度量是增强 */ }
|
|
24059
24428
|
return;
|
|
24060
24429
|
}
|
|
24061
24430
|
const w = selection.inventory.wallpapers.find((x) => x.id === selection.id);
|
|
@@ -24083,6 +24452,9 @@ window.__ModuleLoader__.load({
|
|
|
24083
24452
|
abortTranscodeUpgrade();
|
|
24084
24453
|
syncSceneAudio(selection);
|
|
24085
24454
|
syncRotationTimer();
|
|
24455
|
+
// **终态保证**(4.23.12):被过滤档排除 / 条目消失 —— 这也是一次"切换",同样必须结算。
|
|
24456
|
+
// 少这一行,"切换中"就永久为真(用户看到的是"点哪都没反应")。
|
|
24457
|
+
try { if (typeof weSwitchSettleAs === "function") weSwitchSettleAs(w ? "blocked" : "missing"); } catch { /* 度量是增强 */ }
|
|
24086
24458
|
emit();
|
|
24087
24459
|
return;
|
|
24088
24460
|
}
|
|
@@ -24171,12 +24543,18 @@ window.__ModuleLoader__.load({
|
|
|
24171
24543
|
refreshMediaInfo(true);
|
|
24172
24544
|
syncRotationTimer();
|
|
24173
24545
|
emit();
|
|
24174
|
-
// ── 切换手感的收口(4.23.9 P0
|
|
24546
|
+
// ── 切换手感的收口(4.23.9 P0 修复,4.23.12 补齐类型)──────────────────────
|
|
24175
24547
|
// **非视频**壁纸没有 `weSwitchPlayed()` 那条腿(它挂在 `<video>` 的首帧事件上),
|
|
24176
24548
|
// 不在这里结算的话,"有一个切换在飞"会一直为真 ⇒ 之后每一次点击都被连点防护
|
|
24177
24549
|
// 判成"正在切换,请稍候"而**什么都不做**(用户实测:点了完全没反应)。
|
|
24178
|
-
//
|
|
24179
|
-
|
|
24550
|
+
// 视频档由 `weSwitchPublish()`(层上屏)结算,并有 5s 硬上限兜底;这里补上**类型**,
|
|
24551
|
+
// 让 `/folia/diag/switch-stats` 能按 video/scene/web/image 分簇看出是哪一类卡住。
|
|
24552
|
+
try {
|
|
24553
|
+
if (selection.type !== "video") {
|
|
24554
|
+
if (typeof weSwitchSettleAs === "function") weSwitchSettleAs(selection.type || "image");
|
|
24555
|
+
else weSwitchSettle();
|
|
24556
|
+
}
|
|
24557
|
+
} catch { /* 度量是增强 */ }
|
|
24180
24558
|
// 主题随壁纸(无开关):本张壁纸的判决在**媒体层已经建好之后**才落一次(作者配色缺席时
|
|
24181
24559
|
// 它会自己去采样预览图:异步、带代次校验,取不到就保持当前主题不动)。
|
|
24182
24560
|
// 为什么必须落在 emit() 之后:写作入口在宿主侧是同步的一整轮 —— 重写全量别名令牌 +
|
package/lib/routes/diag.js
CHANGED
|
@@ -204,6 +204,10 @@ export function registerDiagRoutes(webServer, c) {
|
|
|
204
204
|
slug: String(b.slug || '').slice(0, 64),
|
|
205
205
|
cacheFamilyHit: b.cacheFamilyHit === true,
|
|
206
206
|
is1080p: b.is1080p === true,
|
|
207
|
+
// 4.23.12:**这次点的是哪一类**(video / scene / web / image)。
|
|
208
|
+
// 加它是因为"点了没反应"按类型分簇:某一类的结算腿断了,延迟会**恰好落在
|
|
209
|
+
// 15s 硬上限**上(reason:'timeout'),而光看延迟分不出是哪一类卡住。
|
|
210
|
+
kind: String(b.kind || '').slice(0, 16),
|
|
207
211
|
// 度量是遥测:非法/负数一律夹到 0,绝不因为一条坏数据让响应 500。
|
|
208
212
|
switchLatencyMs: Math.max(0, Math.round(Number(b.switchLatencyMs) || 0)),
|
|
209
213
|
clickFeedbackMs: Math.max(0, Math.round(Number(b.clickFeedbackMs) || 0)),
|
|
@@ -242,6 +246,29 @@ function summarizeSwitchStats(list) {
|
|
|
242
246
|
total: rows.length,
|
|
243
247
|
failed: rows.length - okRows.length,
|
|
244
248
|
familyHits: okRows.filter((r) => r.cacheFamilyHit === true).length,
|
|
249
|
+
// 4.23.12:被下一次点击**取代**的切换条数(last-click-wins 的收口)。
|
|
250
|
+
// 它是"用户等不及了"的直接证据:这些条目以前会表现为"点了没反应"(点击被吞)。
|
|
251
|
+
superseded: rows.filter((r) => String((r && r.reason) || '') === 'superseded').length,
|
|
252
|
+
busyTimeout: rows.filter((r) => String((r && r.reason) || '') === 'busy-timeout').length,
|
|
253
|
+
// 按类型分簇统计(4.23.12):一眼看出"哪一类在等 15s 上限"。
|
|
254
|
+
byKind: (() => {
|
|
255
|
+
/** @type {Record<string, {n:number, avg:number, max:number, failed:number}>} */
|
|
256
|
+
const out = {};
|
|
257
|
+
for (const r of rows) {
|
|
258
|
+
const k = String((r && r.kind) || '') || 'unknown';
|
|
259
|
+
if (!out[k]) out[k] = { n: 0, avg: 0, max: 0, failed: 0 };
|
|
260
|
+
const slot = out[k];
|
|
261
|
+
const ms = Math.max(0, Number(r && r.switchLatencyMs) || 0);
|
|
262
|
+
slot.n += 1;
|
|
263
|
+
slot.avg += ms;
|
|
264
|
+
slot.max = Math.max(slot.max, ms);
|
|
265
|
+
if (r && r.ok === false) slot.failed += 1;
|
|
266
|
+
}
|
|
267
|
+
for (const k of Object.keys(out)) {
|
|
268
|
+
out[k].avg = out[k].n ? Math.round(out[k].avg / out[k].n) : 0;
|
|
269
|
+
}
|
|
270
|
+
return out;
|
|
271
|
+
})(),
|
|
245
272
|
switchLatencyMs: agg('switchLatencyMs'),
|
|
246
273
|
clickFeedbackMs: agg('clickFeedbackMs'),
|
|
247
274
|
};
|
package/lib/routes/site-media.js
CHANGED
|
@@ -39,6 +39,8 @@ export const DEPS = ['disposers', 'base', 'serveFile', 'log'];
|
|
|
39
39
|
const DEFAULT_CAP_MB = 2048;
|
|
40
40
|
const FIRST_BYTE_TIMEOUT_MS = 8000;
|
|
41
41
|
const MAX_UPSTREAM_CONCURRENCY = 3;
|
|
42
|
+
/** 「下载中」渐进读的停滞上限(50ms 一拍 ⇒ ≈30 秒)。到点只收尾响应,不当错误。 */
|
|
43
|
+
const GROWING_STALL_LIMIT = 600;
|
|
42
44
|
const EXT_OK = new Set(['.mp4', '.webm', '.mov', '.m4v', '.jpg', '.jpeg', '.png', '.webp', '.avif', '.gif']);
|
|
43
45
|
|
|
44
46
|
/** 站点基址(与其它路由同一口径:`DSH_FOLIA_SITE=off` 关站,缺省官方站点)。 */
|
|
@@ -78,6 +80,20 @@ function keyFor(u) {
|
|
|
78
80
|
return h + (EXT_OK.has(ext) ? ext : '.bin');
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
/**
|
|
84
|
+
* 「下载中」文件按**媒体后缀**给 Content-Type。
|
|
85
|
+
*
|
|
86
|
+
* 为什么必须给对:4.23.11 及以前这里一律 `application/octet-stream`。`<video src>` 拿到
|
|
87
|
+
* octet-stream 时会走"未知类型"的解码路径 —— Chromium 需要**先攒够一大段字节**才敢起解码
|
|
88
|
+
*(它不能靠 Content-Length 判断进度,也不能对未知类型做渐进播放),于是首帧被推到
|
|
89
|
+
* "下完一大截"之后。这正是用户实测的 6–9 秒(见 docs/WALLPAPER-SWITCH-PERF.md §7)。
|
|
90
|
+
*/
|
|
91
|
+
const GROWING_MIME = {
|
|
92
|
+
'.mp4': 'video/mp4', '.m4v': 'video/mp4', '.webm': 'video/webm', '.mov': 'video/quicktime',
|
|
93
|
+
'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.webp': 'image/webp',
|
|
94
|
+
'.avif': 'image/avif', '.gif': 'image/gif',
|
|
95
|
+
};
|
|
96
|
+
|
|
81
97
|
/**
|
|
82
98
|
* 从"正在增长的文件"里按偏移读给客户端(**渐进读**)。
|
|
83
99
|
*
|
|
@@ -85,34 +101,113 @@ function keyFor(u) {
|
|
|
85
101
|
* 与其再开一条上游连接(会抢带宽、还会被播放器背压拖成 0.6 MB/s),
|
|
86
102
|
* 不如直接读这份正在长大的文件:队列按线路速度推进,读端有多少字节喂多少。
|
|
87
103
|
*
|
|
104
|
+
* ## 4.23.12:**必须让浏览器能"提前拿到 moov 就起播"**
|
|
105
|
+
*
|
|
106
|
+
* 旧响应是三处硬伤的组合(实测真机 6–9 秒首帧、甚至 15s 超时):
|
|
107
|
+
* · **没有 `content-length`**(chunked)⇒ 浏览器无法判断"文件多大 / 还差多少",
|
|
108
|
+
* 它的媒体缓冲启发式会**先攒一大段**才肯出帧;
|
|
109
|
+
* · **`accept-ranges: none`** ⇒ 不能 seek,也不能发 `Range: bytes=…` 去直接取 moov;
|
|
110
|
+
* · **`application/octet-stream`** ⇒ 未知类型,解码路径更保守。
|
|
111
|
+
*
|
|
112
|
+
* 现在的响应(总量来自队列 job 的 `total`,**已下的字节边下边喂**):
|
|
113
|
+
* · `content-length: <job.total>`、`accept-ranges: bytes`、正确的 `Content-Type`;
|
|
114
|
+
* · 支持 `Range`:区间**落在已写字节之内**就立刻发 `206 + Content-Range`;
|
|
115
|
+
* 落在已写字节之外(典型是 moov 在文件尾部)就**等文件长到那儿**再发(有停滞上限);
|
|
116
|
+
* · 仍是 `no-store`(内容在变,缓存半份没有意义),完成后的长缓存照旧由 `serveFile` 负责。
|
|
117
|
+
*
|
|
118
|
+
* 总量未知(队列还没拿到 `content-length`)时退回旧形态(chunked / `accept-ranges: none`)——
|
|
119
|
+
* 宁可慢,也不许给出一个错的总长(那会让浏览器把"短读"当成损坏)。
|
|
120
|
+
*
|
|
88
121
|
* 终止条件:读到 job 状态变成 done 且读到 total;或 job 失败/取消;或客户端断开。
|
|
89
122
|
* 任何异常都只是提前结束响应,不抛出(不能让一次播放把进程搞崩)。
|
|
90
|
-
*
|
|
91
|
-
* 缓存口径(4.23.5 明确):**下载中一律 `no-store`**(内容还在增长,缓存半份文件
|
|
92
|
-
* 没有意义)。完成后的长缓存由 `serveFile(..., { immutable: true })` 负责,并按
|
|
93
|
-
* 文件大小分档(见 lib/serve.js 的 `completedCacheControl`)。
|
|
94
123
|
* 导出供 `test/verify-site-media.mjs` 直接驱动这条分支(路由层没有可注入的队列替身)。
|
|
124
|
+
*
|
|
125
|
+
* @param {string} filePath `.part` 的绝对路径
|
|
126
|
+
* @param {any} job 队列 job(`refresh()` 返回最新快照;`total`/`status`/`dest`)
|
|
127
|
+
* @param {any} res 响应对象
|
|
128
|
+
* @param {boolean} headOnly HEAD:只发头
|
|
129
|
+
* @param {any} [req] 请求对象(读 `headers.range`;缺省 = 不支持 Range 的调用点)
|
|
95
130
|
*/
|
|
96
|
-
export async function serveGrowingFile(filePath, job, res, headOnly) {
|
|
131
|
+
export async function serveGrowingFile(filePath, job, res, headOnly, req) {
|
|
97
132
|
const openAt = () => { try { return openSync(filePath, 'r'); } catch { return -1; } };
|
|
133
|
+
const current = () => (typeof job.refresh === 'function' ? job.refresh() : job);
|
|
134
|
+
const sleep = (ms) => new Promise((r) => { setTimeout(r, ms); });
|
|
98
135
|
let fd = openAt();
|
|
99
|
-
let offset = 0;
|
|
100
136
|
let stalled = 0;
|
|
101
|
-
const
|
|
137
|
+
const total = (() => { const j = current(); return j ? Number(j.total) || 0 : 0; })();
|
|
138
|
+
// ⚠️ 扩展名要从**去掉 `.part` 之后**的名字取:`abc.mp4.part` 的 `extname` 是 `.part`,
|
|
139
|
+
// 照它查表会一路落到 `application/octet-stream`(正是 4.23.11 那个"未知类型、攒够才出帧"的坑)。
|
|
140
|
+
const extOf = extname(String(filePath).replace(/\.part$/i, '')).toLowerCase();
|
|
141
|
+
const ctype = GROWING_MIME[extOf] || 'application/octet-stream';
|
|
142
|
+
// 已写字节 = 现在能从盘上读到多少(后面边读边涨)。
|
|
143
|
+
const writtenNow = () => { try { return statSync(filePath).size; } catch { return 0; } };
|
|
144
|
+
|
|
145
|
+
// ── Range 解析(总量已知时才有意义:suffix 与 `*/total` 都要它)──────────────
|
|
146
|
+
const range = req && req.headers ? String(req.headers.range || '') : '';
|
|
147
|
+
let start = 0;
|
|
148
|
+
let end = total > 0 ? total - 1 : Infinity;
|
|
149
|
+
let partial = false;
|
|
150
|
+
if (range && total > 0) {
|
|
151
|
+
const m = /bytes=(\d*)-(\d*)/.exec(range);
|
|
152
|
+
if (!m || (!m[1] && !m[2])) {
|
|
153
|
+
try {
|
|
154
|
+
res.statusCode = 416;
|
|
155
|
+
res.setHeader('content-range', 'bytes */' + total);
|
|
156
|
+
res.setHeader('cache-control', 'no-store');
|
|
157
|
+
res.end();
|
|
158
|
+
} catch { /* ignore */ }
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (m[1] && m[2]) { start = parseInt(m[1], 10); end = Math.min(parseInt(m[2], 10), total - 1); }
|
|
162
|
+
else if (m[1]) { start = parseInt(m[1], 10); end = total - 1; }
|
|
163
|
+
else { start = Math.max(0, total - parseInt(m[2], 10)); end = total - 1; }
|
|
164
|
+
if (!(start <= end) || start >= total) {
|
|
165
|
+
try {
|
|
166
|
+
res.statusCode = 416;
|
|
167
|
+
res.setHeader('content-range', 'bytes */' + total);
|
|
168
|
+
res.setHeader('cache-control', 'no-store');
|
|
169
|
+
res.end();
|
|
170
|
+
} catch { /* ignore */ }
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
partial = true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let offset = start;
|
|
102
177
|
try {
|
|
103
|
-
res.statusCode = 200;
|
|
104
|
-
res.setHeader('content-type',
|
|
105
|
-
res.setHeader('accept-ranges', 'none');
|
|
178
|
+
res.statusCode = partial ? 206 : 200;
|
|
179
|
+
res.setHeader('content-type', ctype);
|
|
106
180
|
res.setHeader('cache-control', 'no-store');
|
|
107
|
-
res.setHeader('x-folia-source', '
|
|
181
|
+
res.setHeader('x-folia-source', 'growing');
|
|
182
|
+
if (total > 0) {
|
|
183
|
+
// 关键头:总量 + 可 Range(浏览器据此直接发 `Range: bytes=0-…` 取 moov)。
|
|
184
|
+
res.setHeader('accept-ranges', 'bytes');
|
|
185
|
+
res.setHeader('content-length', String(end - start + 1));
|
|
186
|
+
if (partial) res.setHeader('content-range', 'bytes ' + start + '-' + end + '/' + total);
|
|
187
|
+
} else {
|
|
188
|
+
// 总量未知:旧形态(宁可慢,也不给一个可能是错的总长)。
|
|
189
|
+
res.setHeader('accept-ranges', 'none');
|
|
190
|
+
}
|
|
108
191
|
if (headOnly) { res.end(); return true; }
|
|
192
|
+
// 请求的起点还在文件尾部之外(moov 在尾部 / 刚 seek 到后面)⇒ 等它长到那儿。
|
|
193
|
+
while (offset > writtenNow()) {
|
|
194
|
+
const cur = current();
|
|
195
|
+
if (cur && (cur.status === 'failed' || cur.status === 'cancelled')) { res.end(); return true; }
|
|
196
|
+
if (cur && cur.status === 'done') break; // done 之后 .part 已 rename,下面会重开 dest
|
|
197
|
+
stalled += 1;
|
|
198
|
+
if (stalled > GROWING_STALL_LIMIT) { res.end(); return true; }
|
|
199
|
+
await sleep(50);
|
|
200
|
+
}
|
|
201
|
+
stalled = 0;
|
|
109
202
|
const BUF = Buffer.alloc(256 * 1024);
|
|
110
203
|
for (;;) {
|
|
111
204
|
if (res.writableEnded) break;
|
|
205
|
+
if (offset > end) break; // 区间发完了
|
|
112
206
|
if (fd < 0) fd = openAt();
|
|
113
207
|
let n = 0;
|
|
114
208
|
if (fd >= 0) {
|
|
115
|
-
|
|
209
|
+
const want = Math.min(BUF.length, end - offset + 1);
|
|
210
|
+
try { n = readSync(fd, BUF, 0, want, offset); } catch { n = 0; }
|
|
116
211
|
}
|
|
117
212
|
if (n > 0) {
|
|
118
213
|
offset += n;
|
|
@@ -122,18 +217,18 @@ export async function serveGrowingFile(filePath, job, res, headOnly) {
|
|
|
122
217
|
continue;
|
|
123
218
|
}
|
|
124
219
|
// 暂时没有新字节:看队列是否还在推进
|
|
125
|
-
const cur =
|
|
220
|
+
const cur = current();
|
|
126
221
|
if (cur && cur.status === 'done') {
|
|
127
222
|
// 完成的瞬间 .part 会被 rename 掉;重新打开最终文件继续读完
|
|
128
223
|
try { if (fd >= 0) closeSync(fd); } catch { /* ignore */ }
|
|
129
224
|
fd = -1;
|
|
130
|
-
if (offset >= (cur.total || 0)) break;
|
|
225
|
+
if (offset > end || offset >= (Number(cur.total) || 0)) break;
|
|
131
226
|
filePath = cur.dest || filePath;
|
|
132
227
|
continue;
|
|
133
228
|
}
|
|
134
229
|
if (cur && (cur.status === 'failed' || cur.status === 'cancelled')) break;
|
|
135
230
|
stalled += 1;
|
|
136
|
-
if (stalled >
|
|
231
|
+
if (stalled > GROWING_STALL_LIMIT) break; // ≈ 30 秒没有新字节就收尾(不无限挂着)
|
|
137
232
|
await sleep(50);
|
|
138
233
|
}
|
|
139
234
|
res.end();
|
|
@@ -436,7 +531,9 @@ export function registerSiteMediaRoutes(webServer, c) {
|
|
|
436
531
|
if (existsSync(partPath)) {
|
|
437
532
|
// 复用即算命中:这条路没有走远端,统计里必须体现,否则会一直显示 hits=0 误导判断。
|
|
438
533
|
stats.hits += 1;
|
|
439
|
-
|
|
534
|
+
// 4.23.12:把 req 传下去 —— 这条分支要能回 `206 + content-range`(moov 前置的源
|
|
535
|
+
// 靠它只取前几百 KB 就能出首帧)。缺 req 的旧调用点仍走"无 Range"的 200 形态。
|
|
536
|
+
await serveGrowingFile(partPath, job, res, headOnly, req);
|
|
440
537
|
return;
|
|
441
538
|
}
|
|
442
539
|
}
|