Files
image-workbench-desktop/src/runtime-boot-progress.js
T
qiqi 81a30f261a
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m24s
feat(boot): 修启动白屏——静态启动屏 + 并行建目录 + 一次性维护挪到首屏后
用户把资料目录放局域网:点开后先有 15s 纯白板,然后进度条 1s 闪过。
原因有两层:
1) app.js 顶部 `await IWBStorageFile.ready()` 在 React 首次渲染之前,存储装载多久白板就多久;
   而 index.html 里没有任何静态画面,WebView 只能显示空白。
2) 那段装载里有一串串行网络往返:串行建 9 个子目录、读 data.json、逐个读画布、
   再加 5 个一次性维护(migrateBlobs/backfillGalleryThumbs/stripInlineGalleryThumbs/
   rotateTrash/dropLegacyDisplayThumbs),局域网下每次往返几百毫秒就是十几秒。

- scripts/boot-splash.js(新增)+ scripts/build.js:index.html 内联静态启动屏
  (logo/标题/进度条/阶段文字/已等待秒数),不依赖脚本即可出画面;React 启动屏挂载时顶掉它
- src/storage-file.js:ensureDirs 改并发(9 次往返 → 1 次);一次性维护延后到首屏后 1.2s;
  prepare 阶段按 4 步上报(授权 / 建目录 / 整理文件名 / 完成)
- src/runtime-boot-progress.js:阶段权重改为 prepare 0-10 / data 10-16 / canvases 16-70 /
  library 70-80 / finalize 80-96,进度条从第一秒起覆盖整段装载
- scripts/check-dist.js:index.html script 数 3→4(含内联启动屏),并断言启动屏存在
- tests/boot-progress-test.js:同步新的阶段边界
2026-11-27 21:38:05 +08:00

70 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* boot-progress.js —— 启动加载进度总线(v3.26.109)
*
* 为什么要它:资料目录放在局域网/NAS 上时,读取是"每个文件一次网络往返",
* canvases/ 里有几十个文件就要等几十次。原来的启动屏只有一条不定长动画,
* 用户不知道在读什么、还要多久。
*
* 这里把"读到第几个文件"换算成真实百分比(按阶段分配权重),启动屏只负责渲染。
* 百分比单调不减:并发分支回来的慢结果不会让进度条倒退。
*/
;(function (global) {
'use strict'
const PHASES = {
prepare: { from: 0, to: 10, label: '准备资料目录' },
data: { from: 10, to: 16, label: '读取 data.json' },
canvases: { from: 16, to: 70, label: '读取画布' },
library: { from: 70, to: 80, label: '读取资源库' },
finalize: { from: 80, to: 96, label: '恢复会话' },
}
const state = { pct: 0, label: '正在启动…', detail: '', indeterminate: true, done: false }
const subs = new Set()
const snapshot = () => ({ pct: state.pct, label: state.label, detail: state.detail, indeterminate: state.indeterminate, done: state.done })
function emit () {
const s = snapshot()
subs.forEach((fn) => { try { fn(s) } catch (e) { /* 单个订阅者出错不影响其它 */ } })
}
// report(阶段, 已完成, 总数):total 缺省/0 时该阶段仍算"不定长"
function report (phase, done, total, detail) {
const p = PHASES[phase]
if (p) {
const d = Number(done) || 0
const t = Number(total) || 0
const k = t > 0 ? Math.max(0, Math.min(1, d / t)) : 0
const pct = Math.round(p.from + (p.to - p.from) * k)
if (pct > state.pct) state.pct = pct
state.indeterminate = !(t > 0)
state.label = p.label
state.detail = detail || (t > 0 ? d + ' / ' + t : '')
}
if (detail) state.detail = String(detail)
emit()
}
function note (text) {
if (text) state.detail = String(text)
emit()
}
function finish () {
if (state.done) return
state.pct = 100
state.indeterminate = false
state.done = true
state.label = '加载完成'
state.detail = ''
emit()
}
function subscribe (fn) {
if (typeof fn !== 'function') return function () {}
subs.add(fn)
try { fn(snapshot()) } catch (e) { /* ignore */ }
return function () { subs.delete(fn) }
}
global.IWBBootProgress = { report, note, finish, subscribe, snapshot, phases: PHASES }
})(typeof window !== 'undefined' ? window : globalThis)