36 lines
1.6 KiB
JavaScript
36 lines
1.6 KiB
JavaScript
export function isWalkingMemoryError(message) {
|
|
return /out of memory|memory allocation|could not allocate memory|cannot enlarge memory/i.test(message);
|
|
}
|
|
|
|
export function walkingErrorMessage(message) {
|
|
if (isWalkingMemoryError(message))
|
|
return '浏览器暂时无法为仿真分配内存,当前仿真已停止。请稍后重试;若仍失败,请关闭其他模型页面后再试。';
|
|
return message;
|
|
}
|
|
|
|
// Give the child time to drain inference and release native objects. Detach on
|
|
// timeout too: a failed WASM bootstrap may never install its normal disposer.
|
|
export function disposeWalkingFrame(frame, channel, { eventTarget = window, origin = location.origin, timeoutMs = 1500 } = {}) {
|
|
if (!frame) return Promise.resolve();
|
|
return new Promise((resolve) => {
|
|
let finished = false;
|
|
const child = frame.contentWindow;
|
|
const finish = () => {
|
|
if (finished) return;
|
|
finished = true;
|
|
clearTimeout(timer);
|
|
eventTarget.removeEventListener('message', onMessage);
|
|
frame.remove(); // Destroys the iframe realm and its dedicated workers.
|
|
resolve();
|
|
};
|
|
const onMessage = (event) => {
|
|
if (event.origin === origin && event.source === child && event.data?.channel === channel && event.data?.type === 'vibemake:walk-disposed') finish();
|
|
};
|
|
const timer = setTimeout(finish, timeoutMs);
|
|
if (!child || !frame.getAttribute('src') || frame.getAttribute('src') === 'about:blank') { finish(); return; }
|
|
eventTarget.addEventListener('message', onMessage);
|
|
try { child.postMessage({ type: 'vibemake:walk-dispose', channel }, origin); }
|
|
catch { finish(); }
|
|
});
|
|
}
|