import type { AxialCoord } from "./hex.ts"; import type { TopologySignal } from "./SignalEngine.ts"; export type NavigationSignalPhase = | "idle" | "origin" | "path" | "receiving" | "broadcast" | "complete" | "cancelled"; export interface NavigationSignalTransaction { id: string; sourceId: string | null; destinationId: string; phase: NavigationSignalPhase; startedAt: number; } export interface NavigationPhasePayload { signals: TopologySignal[]; holdMs?: number; } export interface NavigationPhaseContext { transaction: NavigationSignalTransaction; phase: Exclude; source?: AxialCoord | null; destination: AxialCoord; route: AxialCoord[]; transitionSpeed: number; reducedMotion: boolean; } export interface SignalCoordinatorOptions { transitionSpeed?: number; reducedMotion?: boolean; onPhase?: (detail: NavigationPhaseContext & NavigationPhasePayload) => void; onComplete?: (transaction: NavigationSignalTransaction) => void; onCancel?: (transaction: NavigationSignalTransaction) => void; } export type BuildNavigationPhasePayload = (context: NavigationPhaseContext) => NavigationPhasePayload; function now() { return performance.now(); } function createTransactionId(sourceId: string | null, destinationId: string) { return "nav:" + (sourceId ?? "none") + "->" + destinationId + ":" + Math.round(now()); } function sleep(ms: number) { if (ms <= 0) { return Promise.resolve(); } return new Promise((resolve) => { globalThis.setTimeout(resolve, ms); }); } export class SignalCoordinator { current?: NavigationSignalTransaction; private generation = 0; private readonly options: SignalCoordinatorOptions; constructor(options: SignalCoordinatorOptions = {}) { this.options = options; } isForegroundActive() { return Boolean(this.current && this.current.phase !== "complete" && this.current.phase !== "cancelled"); } cancelCurrent() { if (!this.current) return; const transaction = this.current; this.generation += 1; transaction.phase = "cancelled"; this.current = undefined; this.options.onCancel?.(transaction); } private async waitForTurn(ms: number, token: number) { await sleep(ms); return this.generation === token; } async navigate( sourceId: string | null, destinationId: string, source: AxialCoord | null, destination: AxialCoord, buildPhasePayload: BuildNavigationPhasePayload, route: AxialCoord[] = [], ) { this.cancelCurrent(); const transaction: NavigationSignalTransaction = { id: createTransactionId(sourceId, destinationId), sourceId, destinationId, phase: "idle", startedAt: now(), }; this.current = transaction; this.generation += 1; const token = this.generation; const transitionSpeed = Math.max(0.25, this.options.transitionSpeed ?? 1.35); const reducedMotion = Boolean(this.options.reducedMotion); const emitPhase = async (phase: NavigationPhaseContext["phase"]) => { if (this.generation !== token) return false; transaction.phase = phase; const payload = buildPhasePayload({ transaction, phase, source, destination, route, transitionSpeed, reducedMotion, }); this.options.onPhase?.({ transaction, phase, source, destination, route, transitionSpeed, reducedMotion, ...payload, }); const holdMs = reducedMotion ? 0 : Math.max(0, payload.holdMs ?? Math.max(...payload.signals.map((signal) => signal.duration), 0)); if (!(await this.waitForTurn(holdMs, token))) { return false; } return this.generation === token; }; const hasSource = Boolean(sourceId && source && sourceId !== destinationId); const sameSource = Boolean(sourceId && source && sourceId === destinationId); const hasRoute = route.length > 1 && !sameSource; if (hasSource) { if (hasRoute) { if (!(await emitPhase("path"))) return; } if (!(await emitPhase("receiving"))) return; if (!(await emitPhase("broadcast"))) return; } else if (sameSource) { if (!(await emitPhase("broadcast"))) return; } else { if (!(await emitPhase("broadcast"))) return; } if (this.generation !== token) return; transaction.phase = "complete"; this.current = undefined; this.options.onComplete?.(transaction); } }