Files
image-workbench-desktop/src/canvas-history.js
T

39 lines
1.8 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.
/* 撤销历史快照的纯判定(自 app.js Workbench() 收编,P2-9)
*
* - historySnapHasBigImage(snap):快照是否真的含大图 base64(`data:` 且 >3000 字符)。
* - historySnapshotBytes(snap):快照里节点 data 的字符串总长度。
* 只读入参,无外部依赖;宿主只经 window.IWBCanvasHistory 使用。
*
* (承自 app.js 的关键修复)判据必须是「真的含大图 base64(data: 且 >3000)」:
* 若用「字符串总长度 > 3000」当代理,含大量小字符串(结果 URL、长提示词)的快照会永远命中,
* 清扫器每轮重复处理 → 实测 ~100~125 次/秒的无效引用化,垃圾以每秒几百 MB 产生、堆冲到 GB 级。
*/
;(function (global) {
'use strict'
const historySnapHasBigImage = (snap) => {
let found = false
const scan = (v) => {
if (found) return
if (typeof v === 'string') { if (v.length > 3000 && v.indexOf('data:') === 0) found = true; return }
if (Array.isArray(v)) { for (const x of v) { if (found) return; scan(x) } return }
if (v && typeof v === 'object') { for (const k in v) { if (found) return; scan(v[k]) } }
}
for (const nd of ((snap && snap.nodes) || [])) { if (found) break; scan((nd && nd.data) || {}) }
return found
}
const historySnapshotBytes = (snap) => {
let n = 0
const scan = (v) => {
if (typeof v === 'string') { n += v.length; return }
if (Array.isArray(v)) { for (const x of v) scan(x); return }
if (v && typeof v === 'object') { for (const k in v) scan(v[k]) }
}
for (const nd of ((snap && snap.nodes) || [])) scan((nd && nd.data) || {})
return n
}
global.IWBCanvasHistory = Object.freeze({ historySnapHasBigImage, historySnapshotBytes })
})(typeof window !== 'undefined' ? window : globalThis)