mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
fix(skins): refresh plugin assets when switching skins
This commit is contained in:
1 parent
0c4cc344ef
commit
16f28ca348
5 files changed
+99
-8
No files matched your search
@@ -16,6 +16,7 @@ import {
|
||||
import { parseSkinExtensionUiOutput } from "../../../src/shared/skin-extension-ui.js";
|
||||
import { parseSkinExtensionUiInput } from "../../../src/shared/skin-extension-ui-input.js";
|
||||
import { approveSkinPluginComponents, getMissingSkinPluginApprovals, isSkinPluginComponentApproved, revokeSkinPluginApprovals } from "../services/skin-plugin-approval.js";
|
||||
import { createSkinPluginAssetUrlCache } from "../services/skin-plugin-asset-urls.js";
|
||||
|
||||
type Scalar = string | number | boolean;
|
||||
type SafeContext = Record<string, unknown>;
|
||||
@@ -66,7 +67,7 @@ export default defineComponent({
|
||||
const manageOpen = ref(false);
|
||||
const refresh = ref(0);
|
||||
const stateByComponent = reactive<Record<string, Record<string, Scalar>>>({});
|
||||
const objectUrls = new Map<string, string>();
|
||||
const assetUrls = createSkinPluginAssetUrlCache();
|
||||
const validatedDocument = computed(() => {
|
||||
let document: SkinPluginDocument;
|
||||
const emptyRuntimeBase = Boolean(props.extensionOutput)
|
||||
@@ -118,6 +119,7 @@ export default defineComponent({
|
||||
surfaceSuppressed.value = false;
|
||||
refresh.value += 1;
|
||||
}, { immediate: true });
|
||||
watch(() => [props.skinId, props.skinVersion, props.assets] as const, () => assetUrls.clear());
|
||||
watch(activeSurface, (surface) => emit("surface-change", Boolean(surface)), { immediate: true, flush: "sync" });
|
||||
|
||||
function localState(component: SkinPluginComponent): Record<string, Scalar> {
|
||||
@@ -202,11 +204,7 @@ export default defineComponent({
|
||||
}
|
||||
|
||||
function assetUrl(path: string): string | undefined {
|
||||
const current = props.assets[path];
|
||||
if (!(current instanceof Blob)) return undefined;
|
||||
let url = objectUrls.get(path);
|
||||
if (!url) { url = URL.createObjectURL(current); objectUrls.set(path, url); }
|
||||
return url;
|
||||
return assetUrls.resolve(path, props.assets);
|
||||
}
|
||||
|
||||
function layoutComponentPath(component: SkinPluginComponent): string {
|
||||
@@ -345,7 +343,7 @@ export default defineComponent({
|
||||
refresh.value += 1;
|
||||
}
|
||||
|
||||
onUnmounted(() => { objectUrls.forEach((url) => URL.revokeObjectURL(url)); objectUrls.clear(); });
|
||||
onUnmounted(() => assetUrls.clear());
|
||||
|
||||
return () => {
|
||||
const nodes: VNodeChild[] = renderedComponents.value.map((component) => h("div", {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user