updated the dungeon loading so it doesnt fail first try
This commit is contained in:
1 parent
e39aebf3a1
commit
7e60864e70
11 files changed
+383
-69
No files matched your search
+1
-6
@@ -29,12 +29,7 @@ function LoadingOverlay() {
|
||||
const encounter = useManastormStore((state) => state.currentEncounter);
|
||||
const dungeon = requireDungeonDefinition(activeDungeonId);
|
||||
const assetStatus = useGameStore((state) => state.assetStatus);
|
||||
const setAssetStatus = useGameStore((state) => state.setAssetStatus);
|
||||
const { active, progress, item, errors } = useProgress();
|
||||
|
||||
useEffect(() => {
|
||||
if (errors.length) setAssetStatus("error", `Could not load ${errors[0]}`);
|
||||
}, [errors, setAssetStatus]);
|
||||
const { active, progress, item } = useProgress();
|
||||
|
||||
if (assetStatus !== "checking" && assetStatus !== "loading") return null;
|
||||
const networkComplete = !active && progress >= 100;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { ErrorInfo } from "react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { restartActiveDungeonSession } from "../game/dungeonSession";
|
||||
import { SceneErrorBoundary } from "./SceneErrorBoundary";
|
||||
|
||||
vi.mock("../game/dungeonSession", () => ({
|
||||
restartActiveDungeonSession: vi.fn(() => true),
|
||||
}));
|
||||
|
||||
describe("scene error recovery", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("automatically rebuilds the active dungeon without reloading the page", () => {
|
||||
vi.useFakeTimers();
|
||||
vi.stubGlobal("window", {
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
location: { reload: vi.fn() },
|
||||
});
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
const boundary = new SceneErrorBoundary({ children: null });
|
||||
boundary.state = {
|
||||
error: new Error("transient GLB failure"),
|
||||
retryCount: 0,
|
||||
retryScheduled: false,
|
||||
};
|
||||
boundary.setState = vi.fn((patch) => {
|
||||
const next = typeof patch === "function"
|
||||
? patch(boundary.state, boundary.props)
|
||||
: patch;
|
||||
if (next) boundary.state = { ...boundary.state, ...next };
|
||||
}) as typeof boundary.setState;
|
||||
|
||||
boundary.componentDidCatch(
|
||||
boundary.state.error!,
|
||||
{ componentStack: "\n at DungeonEnvironment" } as ErrorInfo,
|
||||
);
|
||||
expect(boundary.state.retryScheduled).toBe(true);
|
||||
|
||||
vi.advanceTimersByTime(500);
|
||||
|
||||
expect(restartActiveDungeonSession).toHaveBeenCalledOnce();
|
||||
expect(window.location.reload).not.toHaveBeenCalled();
|
||||
expect(boundary.state).toMatchObject({
|
||||
error: null,
|
||||
retryCount: 1,
|
||||
retryScheduled: false,
|
||||
});
|
||||
boundary.componentWillUnmount();
|
||||
});
|
||||
});
|
||||
@@ -1,19 +1,69 @@
|
||||
import { Component, type ErrorInfo, type ReactNode } from "react";
|
||||
import { restartActiveDungeonSession } from "../game/dungeonSession";
|
||||
import { useGameStore } from "../game/store";
|
||||
|
||||
interface Props { children: ReactNode }
|
||||
interface State { error: Error | null }
|
||||
interface State {
|
||||
error: Error | null;
|
||||
retryCount: number;
|
||||
retryScheduled: boolean;
|
||||
}
|
||||
|
||||
const MAX_AUTOMATIC_RETRIES = 1;
|
||||
const AUTOMATIC_RETRY_DELAY_MS = 500;
|
||||
const RETRY_BUDGET_RESET_MS = 60_000;
|
||||
|
||||
export class SceneErrorBoundary extends Component<Props, State> {
|
||||
state: State = { error: null };
|
||||
state: State = { error: null, retryCount: 0, retryScheduled: false };
|
||||
private retryTimer: number | null = null;
|
||||
private retryBudgetTimer: number | null = null;
|
||||
|
||||
static getDerivedStateFromError(error: Error): State {
|
||||
static getDerivedStateFromError(error: Error): Partial<State> {
|
||||
return { error };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, info: ErrorInfo) {
|
||||
console.error("Healer Man scene failed", error, info.componentStack);
|
||||
useGameStore.getState().setAssetStatus("error", error.message);
|
||||
if (this.retryBudgetTimer !== null) {
|
||||
window.clearTimeout(this.retryBudgetTimer);
|
||||
this.retryBudgetTimer = null;
|
||||
}
|
||||
if (this.state.retryCount < MAX_AUTOMATIC_RETRIES) this.scheduleRetry();
|
||||
}
|
||||
|
||||
componentWillUnmount(): void {
|
||||
if (this.retryTimer !== null) window.clearTimeout(this.retryTimer);
|
||||
if (this.retryBudgetTimer !== null) window.clearTimeout(this.retryBudgetTimer);
|
||||
}
|
||||
|
||||
private scheduleRetry(): void {
|
||||
if (this.retryTimer !== null) return;
|
||||
this.setState({ retryScheduled: true });
|
||||
this.retryTimer = window.setTimeout(() => {
|
||||
this.retryTimer = null;
|
||||
this.retryScene();
|
||||
}, AUTOMATIC_RETRY_DELAY_MS);
|
||||
}
|
||||
|
||||
private retryScene = (): void => {
|
||||
const retryCount = this.state.retryCount + 1;
|
||||
try {
|
||||
if (!restartActiveDungeonSession()) {
|
||||
this.setState({ retryScheduled: false });
|
||||
return;
|
||||
}
|
||||
this.setState({ error: null, retryCount, retryScheduled: false });
|
||||
this.retryBudgetTimer = window.setTimeout(() => {
|
||||
this.retryBudgetTimer = null;
|
||||
if (!this.state.error) this.setState({ retryCount: 0 });
|
||||
}, RETRY_BUDGET_RESET_MS);
|
||||
} catch (error) {
|
||||
const nextError = error instanceof Error ? error : new Error("The dungeon retry failed.");
|
||||
console.error("Healer Man scene retry failed", nextError);
|
||||
useGameStore.getState().setAssetStatus("error", nextError.message);
|
||||
this.setState({ error: nextError, retryCount, retryScheduled: false });
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -21,9 +71,17 @@ export class SceneErrorBoundary extends Component<Props, State> {
|
||||
return (
|
||||
<div className="scene-error" role="alert">
|
||||
<p className="eyebrow">Dungeon load interrupted</p>
|
||||
<h1>The cavern could not be assembled.</h1>
|
||||
<p>{this.state.error.message}</p>
|
||||
<button type="button" className="button button--primary" onClick={() => window.location.reload()}>Retry loading</button>
|
||||
<h1>{this.state.retryScheduled ? "Recovering the expedition..." : "The cavern could not be assembled."}</h1>
|
||||
<p>
|
||||
{this.state.retryScheduled
|
||||
? "Clearing the failed asset cache and rebuilding the dungeon in smaller batches."
|
||||
: this.state.error.message}
|
||||
</p>
|
||||
{!this.state.retryScheduled && (
|
||||
<button type="button" className="button button--primary" onClick={this.retryScene}>
|
||||
Retry dungeon
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user