Implementation:AIScientists Dev WorldSeed replay store
| Knowledge Sources | |
|---|---|
| Domains | Frontend, State_Management |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete Zustand store for independent per-tick snapshot replay playback with rAF-driven animation loop, provided by the WorldSeed frontend.
Description
The useReplayStore manages replay state as a layer on top of normal operation — it never touches `worldStore`, so live polling/SSE continues unaffected. `start(runId)` fetches the snapshot tick list, loads tick 0, builds an O(1) `tickIndexMap`, enqueues subtitle cues, and starts the rAF loop. The loop advances ticks when the subtitle system is not busy and no fetch is in progress; empty ticks use a short minimum delay while content ticks are held by `subtitleBusy`. `stepForward`/`stepBack` pause and fetch specific snapshots. `seekTo` finds the nearest available tick and clears subtitles. `stop` tears down the loop and clears state. A `fetching` flag prevents overlapping async fetches.
Usage
Import `useReplayStore` from the replay controls component and the map view (which reads replay entities when active). Check `active` to switch between live and replay rendering.
Code Reference
Source Location
- Repository: WorldSeed
- File: frontend/src/stores/replay.ts
Signature
export const useReplayStore = create<ReplayState & ReplayActions>()((set, get) => ({
active: boolean;
paused: boolean;
tick: number;
entities: Entity[];
availableTicks: number[];
speed: number;
start: (runId: string) => Promise<void>;
stop: () => void;
pause: () => void;
resume: () => void;
stepForward: () => void;
stepBack: () => void;
setSpeed: (speed: number) => void;
seekTo: (targetTick: number) => void;
}));
Import
import { useReplayStore } from '@/stores/replay';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| runId | string | Yes (start) │ Run ID to replay | |
| speed | number | No │ Ticks per second multiplier | |
| targetTick | number | Yes (seekTo) │ Tick to seek to |
Outputs
| Name | Type | Description |
|---|---|---|
| active | boolean │ Whether replay mode is engaged | |
| entities | Entity[] │ Snapshot entities for current tick | |
| tick | number │ Current replay tick |
Usage Examples
import { useReplayStore } from '@/stores/replay';
// Start replay
await useReplayStore.getState().start('run_2026_04_26');
// Check if replay is active
const isActive = useReplayStore(s => s.active);
const entities = useReplayStore(s => s.entities);
// Step forward
useReplayStore.getState().stepForward();
// Stop replay
useReplayStore.getState().stop();