Skip to content

Commit 87ad239

Browse files
committed
refactor(live2d-companion): simplify widget loader and fix wrong warn message
Two cleanups in the pack frontend script: 1. fetchConfigRecordId() catch block had a copy-paste warn message ('failed to parse SSR config') that had nothing to do with the function's actual job (fetching the live2d_config record id). Now reports 'live2d_config record not found'. 2. loadWidgetScript() had three overlapping timers (10s outer reject, 200ms poll, 8s onload-internal resolve) racing over a shared 'resolved' flag. Rewrote with a single 'settled' flag plus done() and fail(msg) helpers: hardTimeout covers script-load failure, onerror is its own path, onload keeps one poll that resolves after 8s of no #waifu (script loaded but widget slow — fallback would hide a late real widget).
1 parent 2055f3d commit 87ad239

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

packs/live2d-companion/frontend/live2d-companion.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function loadSsrConfig() {
3636
try {
3737
const data = JSON.parse(node.textContent || "{}");
3838
return data.serverConfig || null;
39-
} catch { console.warn("[live2d-companion] failed to parse SSR config");
39+
} catch {
40+
console.warn("[live2d-companion] failed to parse SSR config");
4041
return null;
4142
}
4243
}
@@ -112,37 +113,45 @@ function loadWidgetScript() {
112113
const script = document.createElement("script");
113114
script.src = CONFIG.widgetPath + "autoload.js";
114115
script.async = true;
115-
let resolved = false;
116-
const timer = setTimeout(() => {
117-
if (!resolved) reject(new Error("CDN timeout"));
118-
}, 10000);
116+
117+
let settled = false;
118+
const fail = (msg) => {
119+
if (settled) return;
120+
settled = true;
121+
reject(new Error(msg));
122+
};
123+
const done = () => {
124+
if (settled) return;
125+
settled = true;
126+
resolve();
127+
};
128+
129+
// Hard timeout: reject if the script itself never loads.
130+
const hardTimeout = setTimeout(() => fail("CDN timeout"), 10000);
131+
132+
script.onerror = () => {
133+
clearTimeout(hardTimeout);
134+
fail("CDN script unavailable");
135+
};
119136

120137
script.onload = () => {
121-
const checkInterval = setInterval(() => {
122-
const waifu = document.getElementById("waifu");
123-
if (waifu) {
124-
clearInterval(checkInterval);
125-
clearTimeout(timer);
126-
resolved = true;
127-
resolve();
138+
clearTimeout(hardTimeout);
139+
// autoload.js injects #waifu asynchronously; poll briefly for it so
140+
// moveWidgetIntoNamespace() can relocate it into our namespaced root.
141+
const started = Date.now();
142+
const poll = setInterval(() => {
143+
if (document.getElementById("waifu")) {
144+
clearInterval(poll);
145+
done();
146+
} else if (Date.now() - started > 8000) {
147+
clearInterval(poll);
148+
// Script loaded but widget never mounted. Resolve anyway — the
149+
// widget may appear later; fallback UI would hide a late arrival.
150+
done();
128151
}
129152
}, 200);
130-
setTimeout(() => {
131-
if (!resolved) {
132-
clearInterval(checkInterval);
133-
clearTimeout(timer);
134-
resolved = true;
135-
resolve();
136-
}
137-
}, 8000);
138-
};
139-
script.onerror = () => {
140-
clearTimeout(timer);
141-
if (!resolved) {
142-
resolved = true;
143-
reject(new Error("CDN script unavailable"));
144-
}
145153
};
154+
146155
document.head.append(script);
147156
});
148157
}
@@ -193,7 +202,8 @@ async function fetchConfigRecordId() {
193202
try {
194203
const record = await pb.collection(COLLECTION).getFirstListItem("1=1");
195204
return record.id;
196-
} catch { console.warn("[live2d-companion] failed to parse SSR config");
205+
} catch {
206+
console.warn("[live2d-companion] live2d_config record not found");
197207
return null;
198208
}
199209
}

0 commit comments

Comments
 (0)