# core/loaders/walk.loader.ts

> 130 lines of code and 23 definitions.

Tree: Site tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/tree#file-core-loaders-walk-loader-ts
Source text: https://banes-lab.com/assets/sources/source.81e50ea15033433ccd29319e773cc289f283b5b13a9ed42a3fecd7ebab5784a3.generated.txt

## Definitions

- `textOf` (lexical_declaration, line 21)
- `lineOf` (lexical_declaration, line 26)
- `loadSource` (lexical_declaration, line 130, exported)
- `cellsOf` (lexical_declaration, line 31)
- `isPayload` (lexical_declaration, line 17)
- `isRecord` (lexical_declaration, line 13)
- `loadWalk` (lexical_declaration, line 77, exported)
- `collectSources` (lexical_declaration, line 108)
- `fetchWalk` (lexical_declaration, line 58)
- `fetchSource` (lexical_declaration, line 92)
- `sourceNameOf` (lexical_declaration, line 119, exported)
- `walks` (lexical_declaration, line 10)
- `sources` (lexical_declaration, line 11)
- `value` (lexical_declaration, line 27)
- `column` (lexical_declaration, line 32)
- `[state, depth, label, file, line, name, text, severity]` (lexical_declaration, line 35)
- `[vector, data]` (lexical_declaration, line 60)
- `parsed` (lexical_declaration, line 65)
- `response` (lexical_declaration, line 94)
- `sourceNames` (lexical_declaration, line 106)
- `names` (lexical_declaration, line 121, exported)
- `pending` (lexical_declaration, line 131, exported)
- `loading` (lexical_declaration, line 135, exported)

## Uses

- [core/converters/folder.converter.ts](https://banes-lab.com/source/tree/core/converters/folder.converter.ts.md)
- [core/predicates/resource.predicate.ts](https://banes-lab.com/source/tree/core/predicates/resource.predicate.ts.md)
- [core/registries/anatomy.registry.ts](https://banes-lab.com/source/tree/core/registries/anatomy.registry.ts.md)
- [core/reporters/failure.reporter.ts](https://banes-lab.com/source/tree/core/reporters/failure.reporter.ts.md)

## Used by

- [presentation/renderers/markdown.renderer.ts](https://banes-lab.com/source/tree/presentation/renderers/markdown.renderer.ts.md)
- [presentation/renderers/source.renderer.ts](https://banes-lab.com/source/tree/presentation/renderers/source.renderer.ts.md)
- [presentation/renderers/walk.renderer.ts](https://banes-lab.com/source/tree/presentation/renderers/walk.renderer.ts.md)

## Source

```typescript
import { ANATOMY_TREES, walkTreeOf } from "#core/registries/anatomy.registry";
import type { AnatomyFolder, LoadedWalk, WalkCellView, WalkCellsPayload, WalkRef } from "#types/anatomy.types";
import { CELL_COLUMNS, REF_RADIX } from "#configuration/constants/anatomy.constants";
import { SOURCE_UNAVAILABLE, WALK_UNAVAILABLE } from "#configuration/strings/report.strings";
import { sourceLocation, walkLocation } from "#assets/walk.assets";
import { isVector } from "#core/predicates/resource.predicate";
import { qualifiedPath } from "#core/converters/folder.converter";
import { reportFailure } from "#core/reporters/failure.reporter";

const walks = new Map<string, Promise<LoadedWalk | null>>();
const sources = new Map<string, Promise<string | null>>();

const isRecord = function isRecord(value: unknown): value is Record<string, unknown> {
    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, tree: string): 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) === "" ? "" : qualifiedPath(tree, 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<LoadedWalk | null> {
    try {
        const [vector, data] = await Promise.all([fetch(walkLocation(ref.vector)), fetch(walkLocation(ref.cells))]);
        if (!isVector(vector) || !data.ok) {
            reportFailure(WALK_UNAVAILABLE, ref);
            return null;
        }
        const parsed: unknown = await data.json();
        if (!isPayload(parsed)) {
            reportFailure(WALK_UNAVAILABLE, parsed);
            return null;
        }
        return { cells: cellsOf(parsed, walkTreeOf(ref.vector)), markup: await vector.text() };
    } catch (error) {
        reportFailure(WALK_UNAVAILABLE, error);
        return null;
    }
};

export const loadWalk = async function loadWalk(ref: WalkRef): Promise<LoadedWalk | null> {
    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<string | null> {
    try {
        const response = await fetch(sourceLocation(name));
        if (!response.ok) {
            reportFailure(SOURCE_UNAVAILABLE, name);
            return null;
        }
        return await response.text();
    } catch (error) {
        reportFailure(SOURCE_UNAVAILABLE, error);
        return null;
    }
};

let sourceNames: ReadonlyMap<string, string> | null = null;

const collectSources = function collectSources(folder: AnatomyFolder, into: Map<string, string>): 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<string, string>();
        for (const tree of ANATOMY_TREES) {
            collectSources(tree.snapshot.tree, names);
        }
        sourceNames = names;
    }
    return sourceNames.get(path) ?? null;
};

export const loadSource = async function loadSource(name: string): Promise<string | null> {
    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;
};
```
