import type { AnatomyFolder, LoadedWalk, WalkCellView, WalkCellsPayload, WalkRef } from "#types/anatomy.types"; import { CELL_COLUMNS, REF_RADIX } from "#configuration/constants/anatomy.constants"; import { sourceLocation, walkLocation } from "#assets/walk.assets"; import { ANATOMY } from "#assets/anatomy.generated"; import { isVector } from "#core/predicates/resource.predicate"; const walks = new Map>(); const sources = new Map>(); const isRecord = function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null; }; const isPayload = function isPayload(value: unknown): value is WalkCellsPayload { return isRecord(value) && Array.isArray(value["rows"]) && Array.isArray(value["columns"]); }; const textOf = function textOf(cell: readonly (number | string | null)[], at: number): string { const value = cell[at]; return typeof value === "string" ? value : ""; }; const lineOf = function lineOf(cell: readonly (number | string | null)[], at: number): number | null { const value = cell[at]; return typeof value === "number" ? value : null; }; const cellsOf = function cellsOf(payload: WalkCellsPayload): WalkCellView[] { const column = function column(name: string): number { return payload.columns.indexOf(name); }; const [state, depth, label, file, line, name, text, severity] = [ column(CELL_COLUMNS.state), column(CELL_COLUMNS.depth), column(CELL_COLUMNS.label), column(CELL_COLUMNS.file), column(CELL_COLUMNS.line), column(CELL_COLUMNS.name), column(CELL_COLUMNS.text), column(CELL_COLUMNS.severity), ]; return payload.rows.map((cell, index) => ({ depth: lineOf(cell, depth) ?? 0, file: textOf(cell, file), label: textOf(cell, label), line: lineOf(cell, line), name: textOf(cell, name), ref: index.toString(REF_RADIX), severity: cell[severity] === null ? null : textOf(cell, severity), state: textOf(cell, state), text: textOf(cell, text), })); }; const fetchWalk = async function fetchWalk(ref: WalkRef): Promise { try { const [vector, data] = await Promise.all([fetch(walkLocation(ref.vector)), fetch(walkLocation(ref.cells))]); if (!isVector(vector) || !data.ok) { return null; } const parsed: unknown = await data.json(); return { cells: isPayload(parsed) ? cellsOf(parsed) : [], markup: await vector.text() }; } catch { return null; } }; export const loadWalk = async function loadWalk(ref: WalkRef): Promise { const pending = walks.get(ref.vector); if (pending !== undefined) { return pending; } const loading = fetchWalk(ref).then((walk) => { if (walk === null) { walks.delete(ref.vector); } return walk; }); walks.set(ref.vector, loading); return loading; }; const fetchSource = async function fetchSource(name: string): Promise { try { const response = await fetch(sourceLocation(name)); return response.ok ? await response.text() : null; } catch { return null; } }; let sourceNames: ReadonlyMap | null = null; const collectSources = function collectSources(folder: AnatomyFolder, into: Map): void { for (const file of folder.files) { if (file.source !== null) { into.set(file.path, file.source); } } for (const child of folder.folders) { collectSources(child, into); } }; export const sourceNameOf = function sourceNameOf(path: string): string | null { if (sourceNames === null) { const names = new Map(); collectSources(ANATOMY.tree, names); sourceNames = names; } return sourceNames.get(path) ?? null; }; export const loadSource = async function loadSource(name: string): Promise { const pending = sources.get(name); if (pending !== undefined) { return pending; } const loading = fetchSource(name).then((text) => { if (text === null) { sources.delete(name); } return text; }); sources.set(name, loading); return loading; };