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:
@@ -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],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user