perf: preload cảnh kề lúc trình duyệt rảnh

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
tuyenio
2026-06-24 16:12:13 +07:00
parent cf360694d6
commit 5cc3c7b636
+20
View File
@@ -294,6 +294,23 @@ function directionFromYawPitch(yaw: number, pitch: number) {
);
}
type IdleWindow = typeof window & {
requestIdleCallback?: (cb: () => void, opts?: { timeout: number }) => number;
};
// Lên lịch chạy lúc trình duyệt rảnh (fallback setTimeout) — dùng cho preload nền.
function scheduleIdle(cb: () => void) {
if (typeof window === "undefined") {
return;
}
const w = window as IdleWindow;
if (w.requestIdleCallback) {
w.requestIdleCallback(cb, { timeout: 2000 });
} else {
window.setTimeout(cb, 400);
}
}
export default function VirtualTour({
tourId = "chua-thai-lac",
}: {
@@ -1282,7 +1299,9 @@ function TourExperience({
.catch(() => undefined);
};
// Cảnh hiện tại: tải ngay. Cảnh kề: lùi xuống lúc rảnh để không nghẽn khi vừa vào.
preloadImage(scene.image);
scheduleIdle(() => {
scene.hotspots.forEach((hotspot) => {
const targetScene = tourConfig.sceneById.get(hotspot.targetId);
@@ -1290,6 +1309,7 @@ function TourExperience({
preloadImage(targetScene.image);
}
});
});
},
[tourConfig, warmPanoramaTexture],
);