import { PLATFORM_ROUTE_RUNTIME_CONTRACT } from "../contracts/route-runtime-contract.ts"; import { PLATFORM_ROUTE_REGISTRY } from "../contracts/routes.ts"; import { composeUniqueRecord } from "../platform/cardinality/compose-unique-record.ts"; import { INSTALLED_PRODUCT_FEATURES } from "./installed-product-manifest.ts"; /** * 15-01. Boot-time registries composed from the features' declarations: the * route table, route runtime contracts, cache invalidation and navigation. * The V2 API registries need the executable half of every feature and live in * `loaded-feature-runtimes.ts`, which the app boot never imports. */ export const INSTALLED_FEATURE_CONTRACTS = INSTALLED_PRODUCT_FEATURES; // 09-01. The registries keep composeUniqueRecord's own type, the intersection // of every record. The former cast to `PLATFORM & InstalledFeatureContract[...]` // was an intersection with a union once two features were installed, which // kept only the route keys common to both. export const ROUTE_REGISTRY = composeUniqueRecord( [ PLATFORM_ROUTE_REGISTRY, ...INSTALLED_FEATURE_CONTRACTS.map((contract) => contract.routes), ], () => "route registry", ); export const ROUTE_RUNTIME_CONTRACT = composeUniqueRecord( [ PLATFORM_ROUTE_RUNTIME_CONTRACT, ...INSTALLED_FEATURE_CONTRACTS.map( (contract) => contract.routeRuntimeContracts, ), ], () => "route runtime contract", ); // 09-01. Element types are spelled out for the same reason as in // installed-feature-adapters.ts: flatMap over the installed-contract union // otherwise returns the member arrays unflattened. type InstalledInvalidation = (typeof INSTALLED_FEATURE_CONTRACTS)[number]["invalidation"]; type InstalledTopicVersion = (typeof INSTALLED_FEATURE_CONTRACTS)[number]["topicVersions"][number]; export const INVALIDATION_REGISTRY = Object.freeze({ topics: Object.freeze( INSTALLED_FEATURE_CONTRACTS.flatMap( (contract): readonly InstalledInvalidation["topics"][number][] => contract.invalidation.topics, ), ), namespaces: Object.freeze( INSTALLED_FEATURE_CONTRACTS.flatMap( (contract): readonly InstalledInvalidation["namespaces"][number][] => contract.invalidation.namespaces, ), ), edges: Object.freeze( INSTALLED_FEATURE_CONTRACTS.flatMap( (contract): readonly InstalledInvalidation["edges"][number][] => contract.invalidation.edges, ), ), }); export const INVALIDATION_TOPIC_VERSIONS = Object.freeze( INSTALLED_FEATURE_CONTRACTS.flatMap( (contract): readonly InstalledTopicVersion[] => contract.topicVersions, ), ); export const ROUTE_FEATURE_OWNER: Readonly> = Object.freeze( Object.fromEntries( INSTALLED_FEATURE_CONTRACTS.flatMap((contract) => Object.keys(contract.routes).map( (routeId) => [routeId, contract.featureId] as const, ), ), ), ); export const NAVIGATION_ROUTES = Object.freeze( Object.values(ROUTE_REGISTRY) .filter(isNavigableRoute) .sort((left, right) => left.navigationOrder - right.navigationOrder), ); type InstalledRouteDefinition = (typeof ROUTE_REGISTRY)[keyof typeof ROUTE_REGISTRY]; type NavigableRoute = InstalledRouteDefinition & Readonly<{ navigationOrder: number }>; function isNavigableRoute( definition: InstalledRouteDefinition, ): definition is NavigableRoute { return definition.navigationOrder !== null; } export function getRoute(routeId: string): InstalledRouteDefinition { const registry: Readonly> = ROUTE_REGISTRY; const selected = registry[routeId]; if (!selected) throw new Error(`Unregistered route: ${routeId}`); return selected; } export function routePath(routeId: string): string { return getRoute(routeId).path; }