import type { InstalledContractContribution } from "../contracts/external-contract-runtime.ts"; import { composeFeatureAdapterInputs, type ContributionsCapabilityContext, type InstalledFeatureInputs, } from "./feature-adapter-contribution.ts"; import { loadFeatureRuntime, type FeatureModuleRuntime, } from "./feature-module.ts"; import { INSTALLED_PRODUCT_FEATURE_MODULES, type ProductFeatureModule, } from "./installed-product-manifest.ts"; type InstalledFeatureRuntime = FeatureModuleRuntime; // 09-01. Indexed access distributes over the installed-module union, so this is // every installed contribution. `flatMap` alone infers its element type from // the first union member and rejected the second installed feature. type InstalledFeatureAdapterContribution = InstalledFeatureRuntime["adapters"][number]; type InstalledFeatureCapabilityContext = ContributionsCapabilityContext< readonly InstalledFeatureAdapterContribution[] >; /** One feature's input, created when its runtime half has loaded. */ export type InstalledFeatureLoader = () => Promise; /** * 15-01. Adapter installation is a projection of feature-owned descriptors, as * before, but each feature's descriptors now arrive with its runtime half. The * composition root hands these loaders to the application; the router calls * `application.features.load` before it renders the feature's routes. Adding a * feature still changes only its descriptor and the generated index * (tests/unit/two-feature-install-typecheck.test.ts installs a second one). * * Q-6.9-5. Only `activeFeatureIds` (installed minus `FEATURE_OVERRIDES`) get a * loader. A feature switched off there has no input, so * `application.features.get()` cannot hand out its API. The default (every * installed feature) is for callers that have no runtime configuration. * * `installContracts` receives the runtime's contract contributions before its * input is created, so the HTTP executor can resolve the feature's operations * by the time the feature can call them. */ export function createInstalledFeatureLoaders( context: InstalledFeatureCapabilityContext, activeFeatureIds: readonly string[] = INSTALLED_PRODUCT_FEATURE_MODULES.map( (module) => module.id, ), installContracts: ( featureId: string, contributions: readonly InstalledContractContribution[], ) => void = () => undefined, ): Readonly> { const active = new Set(activeFeatureIds); return Object.freeze( Object.fromEntries( INSTALLED_PRODUCT_FEATURE_MODULES.filter((module) => active.has(module.id), ).map((module) => [ module.id, async (): Promise => { const runtime: InstalledFeatureRuntime = await loadFeatureRuntime(module); const adapters: readonly InstalledFeatureAdapterContribution[] = runtime.adapters; // With no installed feature the contribution type is `never`, which // has no `featureId`; the widened view still type-checks then. const owners: readonly Readonly<{ featureId: string }>[] = adapters; for (const adapter of owners) { if (adapter.featureId !== module.id) { throw new TypeError( `feature ${module.id} loaded an adapter contribution of ${adapter.featureId}`, ); } } installContracts( module.id, runtime.contractContributions as readonly InstalledContractContribution[], ); return composeFeatureAdapterInputs(adapters, [module.id], context); }, ]), ), ); }