fix(skins): refresh plugin assets when switching skins

This commit is contained in:
illusia committed 2026-10-10 15:53:51 +08:00
1 parent 0c4cc344ef
commit 16f28ca348
5 files changed
+99 -8

No files matched your search

@@ -0,0 +1,60 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createSkinPluginAssetUrlCache } from "./skin-plugin-asset-urls.js";
test("plugin asset URLs are reused only for the same Blob and released on replacement or skin change", () => {
const originalCreate = URL.createObjectURL;
const originalRevoke = URL.revokeObjectURL;
const created: string[] = [];
const revoked: string[] = [];
URL.createObjectURL = () => {
const url = `blob:skin-test-${created.length + 1}`;
created.push(url);
return url;
};
URL.revokeObjectURL = (url) => { revoked.push(url); };
try {
const cache = createSkinPluginAssetUrlCache();
const path = "assets/icon.png";
const firstAsset = new Blob(["first skin"]);
const secondAsset = new Blob(["second skin"]);
const firstUrl = cache.resolve(path, { [path]: firstAsset });
assert.equal(firstUrl, "blob:skin-test-1");
assert.equal(cache.resolve(path, { [path]: firstAsset }), firstUrl);
assert.equal(cache.size, 1);
const secondUrl = cache.resolve(path, { [path]: secondAsset });
assert.equal(secondUrl, "blob:skin-test-2");
assert.deepEqual(revoked, [firstUrl]);
assert.equal(cache.size, 1);
cache.clear();
assert.deepEqual(revoked, [firstUrl, secondUrl]);
assert.equal(cache.size, 0);
} finally {
URL.createObjectURL = originalCreate;
URL.revokeObjectURL = originalRevoke;
}
});
test("a missing plugin asset releases a cached URL instead of returning stale artwork", () => {
const originalCreate = URL.createObjectURL;
const originalRevoke = URL.revokeObjectURL;
const revoked: string[] = [];
URL.createObjectURL = () => "blob:skin-test";
URL.revokeObjectURL = (url) => { revoked.push(url); };
try {
const cache = createSkinPluginAssetUrlCache();
const path = "assets/icon.png";
cache.resolve(path, { [path]: new Blob(["old"]) });
assert.equal(cache.resolve(path, {}), undefined);
assert.deepEqual(revoked, ["blob:skin-test"]);
assert.equal(cache.size, 0);
} finally {
URL.createObjectURL = originalCreate;
URL.revokeObjectURL = originalRevoke;
}
});
@@ -0,0 +1,31 @@
/** Owns object URLs for one rendered skin-plugin outlet. */
export function createSkinPluginAssetUrlCache() {
const urls = new Map<string, { asset: Blob; url: string }>();
function release(path: string): void {
const entry = urls.get(path);
if (!entry) return;
URL.revokeObjectURL(entry.url);
urls.delete(path);
}
return {
resolve(path: string, assets: Readonly<Record<string, Blob>>): string | undefined {
const asset = assets[path];
const existing = urls.get(path);
if (!(asset instanceof Blob)) {
release(path);
return undefined;
}
if (existing?.asset === asset) return existing.url;
release(path);
const url = URL.createObjectURL(asset);
urls.set(path, { asset, url });
return url;
},
clear(): void {
for (const path of urls.keys()) release(path);
},
get size(): number { return urls.size; },
};
}
@@ -8,6 +8,7 @@ import {
revokeSkinPluginApprovals,
} from "./skin-plugin-approval.js";
import { createSkinPluginQuickServerProjection } from "./skin-plugin-context.js";
import "./skin-plugin-asset-urls.test.js";
function surfaceDocument(): SkinPluginDocument {
return {