/* 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)