迁移游戏延迟初始化脚本

This commit is contained in:
2026-04-25 14:03:15 +08:00
parent a766f2a9c5
commit 43d613bfb1
3 changed files with 41 additions and 20 deletions
+34
View File
@@ -0,0 +1,34 @@
// 聊天室游戏延迟初始化工具,避免非关键游戏逻辑抢占首屏渲染资源。
/**
* 延迟执行非关键游戏初始化。
*
* @param {Function} callback
* @param {number} [timeout]
* @returns {void}
*/
export function deferChatGameBootstrap(callback, timeout = 2500) {
if (typeof callback !== "function") {
return;
}
if ("requestIdleCallback" in window) {
window.requestIdleCallback(callback, { timeout });
return;
}
window.setTimeout(callback, Math.min(timeout, 1200));
}
/**
* 暴露游戏延迟初始化入口给存量游戏面板脚本。
*
* @returns {void}
*/
export function bindGameBootstrapControls() {
if (typeof window === "undefined") {
return;
}
window.deferChatGameBootstrap = deferChatGameBootstrap;
}