perf(canvas): 画布渲染 - 降低大画布节点开销
This commit is contained in:
1 parent
d89b55d182
commit
c7cb6e14a0
17 files changed
+734
-352
No files matched your search
@@ -15,8 +15,30 @@ export function sameCanvasContent(left: CanvasProject | undefined, right: Canvas
|
||||
return [...new Set([...Object.keys(a), ...Object.keys(b)])].every((key) => a[key] === b[key] || canonicalize(a[key]) === canonicalize(b[key]));
|
||||
}
|
||||
|
||||
export async function canvasContentHash(project: CanvasProject) {
|
||||
const serialized = canonicalize(canvasContentSnapshot(project));
|
||||
type ContentSnapshot = ReturnType<typeof canvasContentSnapshot>;
|
||||
const contentHashes = new WeakMap<CanvasProject["nodes"], { content: ContentSnapshot; hash: Promise<string> }>();
|
||||
|
||||
// Store updates are immutable. Reuse a hash across viewport/revision-only copies;
|
||||
// any content field/reference change invalidates it. Weak keys bound its lifetime.
|
||||
export function canvasContentHash(project: CanvasProject): Promise<string> {
|
||||
const content = canvasContentSnapshot(project);
|
||||
const cached = contentHashes.get(project.nodes);
|
||||
if (cached && sameContentReferences(cached.content, content)) return cached.hash;
|
||||
const hash = hashContent(content);
|
||||
contentHashes.set(project.nodes, { content, hash });
|
||||
void hash.catch(() => {
|
||||
if (contentHashes.get(project.nodes)?.hash === hash) contentHashes.delete(project.nodes);
|
||||
});
|
||||
return hash;
|
||||
}
|
||||
|
||||
function sameContentReferences(a: ContentSnapshot, b: ContentSnapshot) {
|
||||
const keys = Object.keys(a) as (keyof ContentSnapshot)[];
|
||||
return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
|
||||
}
|
||||
|
||||
async function hashContent(content: ContentSnapshot) {
|
||||
const serialized = canonicalize(content);
|
||||
// LAN HTTP deployments may lack Web Crypto. Keep an exact baseline there;
|
||||
// a lossy checksum could incorrectly discard an unsaved draft during login.
|
||||
if (!globalThis.crypto?.subtle) return `json:${serialized}`;
|
||||
|
||||
Reference in new issue
Block a user