Files
image-workbench-desktop/src/image-display-thumb.js
T
qiqi 0baeda2abd
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m7s
perf(canvas): 缩略图生成限流 + 空闲启动 + 大图 paint 隔离(v3.25.67)
2026-11-20 22:40:40 +08:00

110 lines
4.3 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.
/* image-display-thumb.js —— 画布内大图的显示缩略图生成队列(v3.25.67)
*
* 为什么单独成模块:
* 1) 规则要求 app.js 只做装配(行数棘轮),生成逻辑必须落到 src/<domain>-*.js;
* 2) 这段逻辑与 React 无关,纯 DOM/canvas,便于单测。
*
* 背景(实测数据):某画布 27 张参考图是 2160×2880 的远程 PNG(单张约 2MB,解码后约 25MB 位图)。
* 旧实现每张图在渲染时立刻 new Image() 解码整图 —— 27 张并发 ≈ 670MB 位图,
* 主线程被解码 + canvas 缩放占满,表现就是「打开这个画布很卡」。
*
* 本模块做三件事:
* ① 并发上限(默认 2):排队生成,不再一次性解码几十张;
* ② 空闲启动:requestIdleCallback(有则用),首帧渲染优先;
* ③ 结果写回宿主的缓存 Map,并落盘(宿主注入 save),下次打开直接读盘。
*/
;(function (global) {
'use strict'
function create (opts) {
const o = opts || {}
const max = Math.max(64, Number(o.max) || 768)
const cacheMax = Math.max(16, Number(o.cacheMax) || 300)
const concurrency = Math.max(1, Number(o.concurrency) || 2)
const cache = o.cache
const pending = o.pending
if (!cache || !pending) throw new Error('cache/pending required')
const queue = []
let running = 0
let scheduled = false
const remember = (key, val) => {
if (cache.size >= cacheMax) {
const first = cache.keys().next().value
if (first !== undefined) cache.delete(first)
}
cache.set(key, val)
}
const run = (key, v) => new Promise((resolve) => {
if (typeof document === 'undefined' || typeof Image !== 'function') { resolve(false); return }
const img = new Image()
try { img.decoding = 'async' } catch (e) { /* ignore */ }
img.onload = () => {
try {
const iw = img.naturalWidth || 0
const ih = img.naturalHeight || 0
if (iw <= max && ih <= max) { remember(key, v); resolve(true); return }
const scale = Math.min(max / Math.max(1, iw), max / Math.max(1, ih))
const cw = Math.max(1, Math.round(iw * scale))
const ch = Math.max(1, Math.round(ih * scale))
const cv = document.createElement('canvas')
cv.width = cw
cv.height = ch
const ctx = cv.getContext('2d')
if (!ctx) { resolve(false); return }
ctx.drawImage(img, 0, 0, cw, ch)
const small = cv.toDataURL('image/jpeg', 0.78)
remember(key, small)
try { if (typeof o.save === 'function') o.save(key, small) } catch (e) { /* ignore */ }
resolve(true)
} catch (e) { remember(key, v); resolve(false) }
try { if (typeof o.bump === 'function') o.bump() } catch (e) { /* ignore */ }
}
img.onerror = () => {
// Cache 虚拟路径 404 属于 SW 未接管,不可负缓存(否则恢复后仍空白)
try { if (!(typeof o.isTransient === 'function' && o.isTransient(v))) remember(key, v) } catch (e) { /* ignore */ }
resolve(false)
}
img.src = v
})
const pump = () => {
while (running < concurrency && queue.length) {
const job = queue.shift()
running++
run(job.key, job.v).catch(() => { /* ignore */ }).then(() => {
running--
try { pending.delete(job.key) } catch (e) { /* ignore */ }
pump()
})
}
}
const schedule = () => {
if (scheduled) return
scheduled = true
const kick = () => { scheduled = false; pump() }
try {
if (typeof requestIdleCallback === 'function') requestIdleCallback(kick, { timeout: 1200 })
else setTimeout(kick, 60)
} catch (e) { setTimeout(kick, 60) }
}
return {
/** 入队生成;已有缓存/已在队列则忽略 */
queue: function (key, v) {
if (!key || cache.has(key) || pending.has(key)) return false
pending.add(key)
queue.push({ key: key, v: v })
schedule()
return true
},
/** 队列/运行中任务数(诊断用) */
stats: function () { return { queued: queue.length, running: running } },
}
}
global.IWBImageDisplayThumb = { create: create }
})(typeof window !== 'undefined' ? window : globalThis)