feat(boot): 修启动白屏——静态启动屏 + 并行建目录 + 一次性维护挪到首屏后
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m24s

用户把资料目录放局域网:点开后先有 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:同步新的阶段边界
This commit is contained in:
qiqi committed 2026-11-27 21:38:05 +08:00
1 parent e3c7a3ff9b
commit 81a30f261a
9 files changed
+141 -38

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@
- 图片**新写入主通道 = IndexedDB Blob `{__blob}` / key `imgblob_<hash>_<len>`** (A) `app.js:2143`;`{__cache}`(Cache Storage)为兜底;\`ref_\`(`app.js` 内定义的 `IDB_REF_PREFIX = 'ref_'`)为 legacy 形态 (A)。
- 大图判定:`isBigDataUrl` = `data:` 且 `length > 3000` (A) `app.js:2018`;**画布保存保留阈值** `< 16384` 才原样保留 (A) `app.js:2182`。**两处阈值并存,含义不同**。
- 单测是**源码契约测试**:`tests/` 目录共 **493** 处源码文本钉点(`source.includes` / `source.indexOf` / `/re/.test(source)`),分布在 **20** 个文件(其中 `iwb-v32-web-test.js` 一个就占 **407**)→ **结构性重构会先打红测试**。**该数字已由 `tests/source-text-ratchet-test.js` 的「只减不增」棘轮守门**(2026-10-20 起守卫范围从 1 个文件扩到全目录:新增任何一处源码文本钉点都会打红)。**2026-11-12 复算**(`npm run audit:pins`,v3.24.76):二分 STRUCT **417** / NAME **76**;形态 **POS 403 / NEG 87 / COUNT 3**;难度 **易 62 / 中 316 / 难 115**。钉点减法第一批(v3.24.72)把 **537 → 505**:8 条探针钉点改为「门禁内 spec 是否真的读该计数器」、5 条改为运行时断言(生产态 `window` 上无探针)、10 条改为 DOM 断言(质量档位禁用 / 传输页无三模式按钮 / 模型项 title)、4 条改为运行时对象断言(健康检查两域、策略对象冻结)、其余 9 条有同文件 DOM 等价断言或仅剩 NEG。第二批(v3.24.76)把 **505 → 493**:94-5~7 三条「多标签释放语义」(10 处正则)换成 `tests/iwb-tabs-release.spec.js` 的 3 条真 e2e,剩 1 条「消费方契约」(断言该 spec 在门禁内且 spec 源码真的读两个探针);另 2 条 `wPurgeIntent.add` 钉点改为读 `src/workspace-purge.js`。**这一批一换就查出 3 个真缺陷**(objectURL 从未释放 / 彻底删除留孤儿标签 · session / 刷新后误回收打开标签的图)—— 见 CHANGELOG v3.24.76。
- `app.js` = 单 IIFE **8,323 行**(**双层预算**:绝对上限 8,329 / 占总源码 ≤ 40%,现 **26.2%**,2026-11-12 起;此前为单一上限 8,509、零余量);`Workbench()` = **2,174 行**(上限 **2,179**,余量 5;2026-11-16 把生成取消族 `cancelRunsFor`/`cancelRun`/`cancelTaskJobs` 与工作区列表写回族 `persistWs`/`duplicateWs` 搬进 `src/runtime-workbench-logic.js`(`createCancelActions` / `createWorkspaceListActions`),并删掉全仓零调用的 `runAll`,3090 → 2,998);大量逻辑已抽到 `src/*.js`(现 **117** 个模块 / 23,462 行,`sourceModules` 117 项;v3.24.68 新增 `clipboard-graph.js`,v3.24.74 新增 `image-admit.js`,v3.24.75 新增 `image-dedupe.js`,v3.24.76 新增 `workspace-purge.js`,v3.24.79 新增 `generation-provider-ping.js`,v3.24.80 删除无调用的 `createWBFlushViewport` 17 行)。
- `app.js` = 单 IIFE **8,323 行**(**双层预算**:绝对上限 8,329 / 占总源码 ≤ 40%,现 **26.2%**,2026-11-12 起;此前为单一上限 8,509、零余量);`Workbench()` = **2,174 行**(上限 **2,179**,余量 5;2026-11-16 把生成取消族 `cancelRunsFor`/`cancelRun`/`cancelTaskJobs` 与工作区列表写回族 `persistWs`/`duplicateWs` 搬进 `src/runtime-workbench-logic.js`(`createCancelActions` / `createWorkspaceListActions`),并删掉全仓零调用的 `runAll`,3090 → 2,998);大量逻辑已抽到 `src/*.js`(现 **117** 个模块 / 23,482 行,`sourceModules` 117 项;v3.24.68 新增 `clipboard-graph.js`,v3.24.74 新增 `image-admit.js`,v3.24.75 新增 `image-dedupe.js`,v3.24.76 新增 `workspace-purge.js`,v3.24.79 新增 `generation-provider-ping.js`,v3.24.80 删除无调用的 `createWBFlushViewport` 17 行)。
- 门禁:`npm run verify` = `verify:ci`(`build` → `node --check app.js` → `node --check dist/app.js` → `check-dist.js` → `lint`(ESLint 正确性规则,`src/` + `app.js`,P4-16a)→ `test:unit` → `test` → `test:facts`)+ `test:e2e` (A) `package.json`。
## 2. 门禁与环境现状
+73
View File
@@ -0,0 +1,73 @@
'use strict'
// 静态启动屏(v3.26.109):内联进 index.html,不依赖任何脚本就能出画面。
// 为什么:资料目录放在局域网时,app.js 要等「存储装载」完成才渲染 React,
// 那段时间原本是纯白板(实测 15s)。
const css = `<style>
#iwb-boot-static{position:fixed;inset:0;z-index:2147483000;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:16px;background:#f6f6f7;color:#1c1d1f;font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Microsoft YaHei",sans-serif;transition:opacity .45s ease}
#iwb-boot-static.iwb-boot-out{opacity:0;pointer-events:none}
#iwb-boot-static .bs-logo{width:54px;height:54px;color:#575d69;animation:bs-bob 1.2s ease-in-out infinite}
#iwb-boot-static .bs-title{font-size:18px;font-weight:700;letter-spacing:1px}
#iwb-boot-static .bs-bar{width:300px;height:6px;border-radius:999px;background:#e8e9ec;border:1px solid #d8dade;overflow:hidden;position:relative}
#iwb-boot-static .bs-fill{position:absolute;left:0;top:0;bottom:0;width:8%;border-radius:999px;background:linear-gradient(90deg,#6b7280,#575d69);transition:width .32s cubic-bezier(.2,.7,.3,1)}
#iwb-boot-static .bs-fill::after{content:"";position:absolute;inset:0;transform:translateX(-100%);background:linear-gradient(90deg,transparent,#fff,transparent);animation:bs-sheen 1.5s ease-in-out infinite}
#iwb-boot-static .bs-meta{width:300px;display:flex;align-items:baseline;justify-content:space-between;gap:12px;font-size:12px;color:#5c6068}
#iwb-boot-static .bs-pct{font-variant-numeric:tabular-nums;font-weight:600;color:#1c1d1f}
@keyframes bs-bob{0%,100%{transform:translateY(0)}50%{transform:translateY(-9px)}}
@keyframes bs-sheen{0%{transform:translateX(-100%)}100%{transform:translateX(100%)}}
@media (prefers-color-scheme:dark){
#iwb-boot-static{background:#161a19;color:#e9eaea}
#iwb-boot-static .bs-logo{color:#c9cbcd}
#iwb-boot-static .bs-bar{background:#23282a;border-color:#2f3638}
#iwb-boot-static .bs-fill{background:linear-gradient(90deg,#e8e8e8,#c9cbcd)}
#iwb-boot-static .bs-meta{color:#a8adb0}
#iwb-boot-static .bs-pct{color:#e9eaea}
}
@media (prefers-reduced-motion:reduce){#iwb-boot-static .bs-logo,#iwb-boot-static .bs-fill::after{animation:none}}
</style>`
const markup = `<div id="iwb-boot-static" role="status" aria-live="polite">
<svg class="bs-logo" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><circle cx="17.5" cy="17.5" r="3.5"/></svg>
<div class="bs-title">生图工作台</div>
<div class="bs-bar"><div class="bs-fill" id="bs-fill"></div></div>
<div class="bs-meta"><span id="bs-sub">正在启动…</span><span class="bs-pct" id="bs-pct"></span></div>
</div>`
// 轮询进度总线:真实百分比(IWBBootProgress 就绪后),之前显示已等待秒数
const script = `<script>
(function () {
var t0 = Date.now()
var box = document.getElementById('iwb-boot-static')
var fill = document.getElementById('bs-fill')
var sub = document.getElementById('bs-sub')
var pct = document.getElementById('bs-pct')
var lastPct = -1
function hide () {
if (!box) return
box.className = 'iwb-boot-out'
setTimeout(function () { if (box && box.parentNode) box.parentNode.removeChild(box) }, 500)
}
window.__iwbHideBootStatic = hide
function apply () {
if (!box) return
var bp = window.IWBBootProgress
var s = bp && bp.snapshot ? bp.snapshot() : null
if (s) {
if (!s.indeterminate && s.pct !== lastPct) {
lastPct = s.pct
fill.style.width = Math.max(8, s.pct) + '%'
pct.textContent = s.pct + '%'
}
sub.textContent = s.detail ? (s.label + ' · ' + s.detail) : s.label
if (s.done) { hide(); return }
} else {
var sec = Math.floor((Date.now() - t0) / 1000)
sub.textContent = '正在启动…' + (sec >= 2 ? ' ' + sec + 's' : '')
}
requestAnimationFrame(apply)
}
requestAnimationFrame(apply)
})()
</script>`
module.exports = { css, markup, script }
+25 -17
View File
@@ -230,23 +230,31 @@ const swWithVersion = swSource.replace(/__IWB_VERSION__/, version)
if (swWithVersion === swSource) throw new Error('__IWB_VERSION__ marker not found in sw.js')
fs.writeFileSync(path.join(dist, 'sw.js'), swWithVersion)
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>无限画布</title>
<meta name="theme-color" content="#2563eb">
<link rel="stylesheet" href="style.css?v=${version}">
</head>
<body>
<div id="root"></div>
<script src="vendor/react.production.min.js"></script>
<script src="vendor/react-dom.production.min.js"></script>
<script src="app.js?v=${version}"></script>
</body>
</html>
`
const bootSplash = require('./boot-splash')
const html = [
'<!DOCTYPE html>',
'<html lang="zh-CN">',
'<head>',
'<meta charset="utf-8">',
'<meta name="viewport" content="width=device-width, initial-scale=1">',
'<title>无限画布</title>',
'<meta name="theme-color" content="#2563eb">',
'<link rel="stylesheet" href="style.css?v=' + version + '">',
// v3.26.109:静态加载屏内联在 index.html 里。资料目录在局域网时,app.js 要等存储装载完
// 才渲染 React,那段时间原本是纯白板(实测 15s);这段 HTML/CSS 不依赖任何脚本就能出画面。
bootSplash.css,
'</head>',
'<body>',
'<div id="root"></div>',
bootSplash.markup,
bootSplash.script,
'<script src="vendor/react.production.min.js"></script>',
'<script src="vendor/react-dom.production.min.js"></script>',
'<script src="app.js?v=' + version + '"></script>',
'</body>',
'</html>',
''
].join('\n')
fs.writeFileSync(path.join(dist, 'index.html'), html)
fs.writeFileSync(path.join(dist, 'version.json'), JSON.stringify({ version, packageVersion: pkg.version, commit: gitSha }, null, 2) + '\n')
// 导航 logo 用真实应用图标(v3.25.61;缺失不阻塞构建)
+3 -2
View File
@@ -41,9 +41,10 @@ if (!fs.existsSync(htmlPath)) fail('dist/index.html 不存在')
const html = fs.readFileSync(htmlPath, 'utf8')
if (!html.includes('app.js?v=')) fail('dist/index.html 缺少 app.js?v=')
const scriptCount = (html.match(/<script/g) || []).length
if (scriptCount !== 3) fail('dist/index.html <script 数量应为 3,实际 ' + scriptCount)
if (scriptCount !== 4) fail('dist/index.html <script 数量应为 4(React/ReactDOM/app.js + 内联启动屏),实际 ' + scriptCount)
if (!html.includes('iwb-boot-static')) fail('dist/index.html 缺少静态启动屏(局域网资料目录启动时的白屏兜底)')
if (html.includes('src/')) fail('dist/index.html 仍含 src/ 引用')
pass('dist/index.html 含 app.js?v=,<script 数量为 3,无 src/ 引用')
pass('dist/index.html 含 app.js?v=、静态启动屏,<script 数量为 4,无 src/ 引用')
// 3. dist/app.js 含无引号标记子串,证明内联压缩 worker 字面量在 minify 后仍存活
if (!app.includes('self.onmessage=async function(ev)')) {
+3 -3
View File
@@ -10,9 +10,9 @@
;(function (global) {
'use strict'
const PHASES = {
prepare: { from: 0, to: 6, label: '准备资料目录' },
data: { from: 6, to: 10, label: '读取 data.json' },
canvases: { from: 10, to: 70, label: '读取画布' },
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: '恢复会话' },
}
+31 -13
View File
@@ -754,6 +754,15 @@
return fromDataJson ? 'loaded' : 'migrated'
}
// 首屏之后再做的一次性维护:延迟到当前渲染帧之后,别和启动抢 I/O
const deferAfterPaint = (fn) => {
const run = () => { try { Promise.resolve(fn()).catch(() => {}) } catch (e) { /* ignore */ } }
try {
if (typeof setTimeout === 'function') setTimeout(run, 1200)
else run()
} catch (e) { run() }
}
// 一次性迁移:把老版本存在 IndexedDB 里的图片字节搬到 images/ 下(跑过一次就写标记文件)
// 启动日志:超过 256KB 只保留最后 64KB(否则长年累月会一直长)
const blog = async (m) => {
@@ -886,9 +895,9 @@
/* 建立工作必需目录:canvases/ library/ plugins/ trash/ 与 images/{canvas,gallery,thumbs,legacy} */
async function ensureDirs() {
const subs = [CANVAS_DIR, 'library', PLUG_DIR, 'trash', IMG_DIR, IMG_DIR + '/canvas', IMG_DIR + '/gallery', IMG_DIR + '/thumbs', IMG_DIR + '/legacy']
for (const sub of subs) {
try { await fs().mkdir(await join(dir, sub), { recursive: true }) } catch (e) { /* 已存在或不可写:继续下一个 */ }
}
// v3.26.109:串行建 9 个目录 = 9 次网络往返(局域网资料目录上就是十几秒白屏的主因之一),
// 改成并发:一次往返的量级。mkdir recursive 本来幂等,并发安全。
await Promise.all(subs.map((sub) => join(dir, sub).then((abs) => fs().mkdir(abs, { recursive: true })).catch(() => {})))
return true
}
@@ -913,30 +922,39 @@
// 授权(fs scope 是运行时授权,重启后由这里补一次)
try { await core().invoke('grant_dir', { dir: want }) } catch (e) { console.error('[iwb][file-store] 授权失败', e); return false }
dir = want
bpReport('prepare', 1, 4, '资料目录已授权')
try { global.localStorage.setItem(PTR_KEY, dir) } catch (e) { /* ignore */ }
try { await fs().mkdir(await join(dir, IMG_DIR), { recursive: true }) } catch (e) { /* 已存在 */ }
// 兜底:把工作必需的子目录一次性补齐(幂等)。
// 为什么需要:Windows 全新安装时 images/ 下只有 thumbs/,缺了 canvas/ 会导致
// 图片写入失败(历史 bug:写进不持久的 Cache → 重启丢图);目录被手工删掉时也靠这里自愈,
// 否则启动阶段读目录抛错会直接影响首屏渲染。
bpReport('prepare', 2, 4, '补齐子目录')
try { await ensureDirs() } catch (e) { /* 建目录失败不阻塞启动 */ }
bpReport('prepare', 3, 4, '整理画布文件名')
try { await repairCanvasFileNames() } catch (e) { /* 修复失败按原样装载 */ }
bpReport('prepare', 4, 4)
await loadInto()
// 从 WebView 的 localStorage 捞回被截断/覆盖的画布(一次性)
try { await recoverFromWebStorage() } catch (e) { /* 忽略 */ }
loaded = true
bpReport('finalize', 1, 1)
// 首次迁移要立刻落盘,别等 debounce
await flush()
// 老版本图片在 IndexedDB 里 ⇒ 搬到 images/(带标记,只跑一次)
try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ }
// 图库清单补齐缩略图(一次性)
try { await backfillGalleryThumbs() } catch (e) { /* 忽略 */ }
// 图库清单去 base64(一次性):缩略图已在 images/thumbs/,清单只留 thumbHash
try { await stripInlineGalleryThumbs() } catch (e) { /* 忽略 */ }
// trash/ 轮转:gcImages 只是移进来,超过 30 天在这里真正删除
try { await rotateTrash(30) } catch (e) { /* 忽略 */ }
// 旧的 768px 显示缩略图一次性清理(缓存可再生)
try { const dropped = await dropLegacyDisplayThumbs(); if (dropped) console.info('[iwb][file-store] 清理旧缩略图', dropped, '个') } catch (e) { /* 忽略 */ }
// v3.26.109:以下都是一次性维护(各自带标记文件,正常情况下只是 check 一下),
// 但每次都要若干次网络往返。放到首屏渲染之后跑,局域网资料目录的启动时间能少好几秒。
deferAfterPaint(async () => {
// 老版本图片在 IndexedDB 里 ⇒ 搬到 images/(带标记,只跑一次)
try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ }
// 图库清单补齐缩略图(一次性)
try { await backfillGalleryThumbs() } catch (e) { /* 忽略 */ }
// 图库清单去 base64(一次性):缩略图已在 images/thumbs/,清单只留 thumbHash
try { await stripInlineGalleryThumbs() } catch (e) { /* 忽略 */ }
// trash/ 轮转:gcImages 只是移进来,超过 30 天在这里真正删除
try { await rotateTrash(30) } catch (e) { /* 忽略 */ }
// 旧的 768px 显示缩略图一次性清理(缓存可再生)
try { const dropped = await dropLegacyDisplayThumbs(); if (dropped) console.info('[iwb][file-store] 清理旧缩略图', dropped, '个') } catch (e) { /* 忽略 */ }
})
// 自愈:图库清单丢了但 assets 还有 out_ 键 ⇒ 按键重建(名称/时间/idb 可推,label/远程 url 无法恢复)
try {
if (mem['iwb_web_outputs'] === undefined) {
+2
View File
@@ -14,6 +14,8 @@
const bp = global.IWBBootProgress
const [s, setS] = React.useState(() => (bp ? bp.snapshot() : { pct: 0, label: '正在启动…', detail: '', indeterminate: true }))
React.useEffect(() => {
// React 启动屏一挂载就顶掉 index.html 里的静态启动屏(同款外观,切换无感)
try { if (typeof window.__iwbHideBootStatic === 'function') window.__iwbHideBootStatic() } catch (e) { /* ignore */ }
const un = bp ? bp.subscribe(setS) : NOOP
return function () { try { un() } catch (e) { /* ignore */ } }
}, [])
+2 -2
View File
@@ -27,12 +27,12 @@ ok('初始为不定长(还没拿到文件数)', s0.indeterminate === true &&
BP.report('canvases', 0, 10)
const s1 = BP.snapshot()
ok('知道总量后切成确定进度', s1.indeterminate === false)
ok('canvases 起点 = 10%', s1.pct === 10, 'pct=' + s1.pct)
ok('canvases 起点 = 16%', s1.pct === 16, 'pct=' + s1.pct)
ok('阶段文字是「读取画布」', s1.label === '读取画布', s1.label)
ok('明细显示已读/总数', s1.detail === '0 / 10', s1.detail)
BP.report('canvases', 5, 10)
ok('读到一半 = 10 + 60*0.5 = 40%', BP.snapshot().pct === 40, 'pct=' + BP.snapshot().pct)
ok('读到一半 = 16 + 54*0.5 = 43%', BP.snapshot().pct === 43, 'pct=' + BP.snapshot().pct)
BP.report('canvases', 10, 10)
ok('读完 canvases 到 70%', BP.snapshot().pct === 70, 'pct=' + BP.snapshot().pct)
+1
View File
@@ -55,6 +55,7 @@ interface Window {
IWBBootLoad?: any
IWBBootProgress?: any // 启动加载进度总线(v3.26.109)
IWBUISplash?: any // 启动加载屏(v3.26.109)
__iwbHideBootStatic?: any // index.html 静态启动屏的隐藏钩子(v3.26.109)
IWBCanvasNodes?: any
IWBUIWorkspaceTabs?: any
IWBUICanvasTopBar?: any