Complete prepared presentation build and runtime integration
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
import type { WorkspaceRevision, ValueType } from "../capability-model/types.js";
|
||||
import { moduleContract } from "../capability-model/modules.js";
|
||||
import { canonicalJson } from "../capability-model/evolution.js";
|
||||
import { queryBindingType } from "../query/composition.js";
|
||||
import { moduleTypeContext } from "../execution-world/module-contracts.js";
|
||||
import { preparationArtifact, type ReactPreparationArtifact } from "./declarations.js";
|
||||
import { expandPresentationQueries } from "./expansion.js";
|
||||
|
||||
/** Explicit frontend stage. Resolve finite provider branches after workspace
|
||||
* bindings exist; the generic workspace compiler does not invoke this itself. */
|
||||
export async function completeWorkspace(workspace: WorkspaceRevision): Promise<void> {
|
||||
const modules = workspace.packageImports.flatMap((pkg) => {
|
||||
const artifact = pkg.buildArtifacts?.[preparationArtifact] as ReactPreparationArtifact | undefined;
|
||||
return (artifact?.modules ?? []).map((preparation) => {
|
||||
const module = pkg.modules?.find((entry) => entry.id === preparation.module);
|
||||
if (!module) throw Error(`React preparation has no module ${preparation.module}`);
|
||||
const contract = moduleContract(workspace.interfaceImports, module)!;
|
||||
return { pkg, module, contract, preparation };
|
||||
});
|
||||
});
|
||||
const graph = new Map<string, string[]>();
|
||||
const key = (pkg: string, id: string) => JSON.stringify([pkg, id]);
|
||||
for (const parent of modules) {
|
||||
const composition = parent.pkg.queryCompositions?.find((entry) => entry.id === parent.preparation.composition);
|
||||
if (!composition || composition.output.kind !== "record")
|
||||
throw Error(`Missing checked inputs for ${parent.module.displayName}`);
|
||||
const outputs = new Map(Object.entries(composition.output.fields));
|
||||
const dependencies: string[] = [];
|
||||
const children = parent.preparation.children.map((child) => {
|
||||
const at = `${parent.module.displayName}.${child.name}`;
|
||||
let item: ValueType | undefined;
|
||||
if (child.with) {
|
||||
const type = queryBindingType(child.with, composition.input, outputs);
|
||||
item = type.kind === "optional" ? type.value : type;
|
||||
}
|
||||
if (child.each) {
|
||||
const type = queryBindingType(child.each.source, composition.input, outputs);
|
||||
if (type.kind !== "list") throw Error(`${at}: each requires a list`);
|
||||
item = type.value;
|
||||
}
|
||||
const root = queryBindingType(child.root, composition.input, outputs, item);
|
||||
if (
|
||||
root.kind !== "object-ref" ||
|
||||
root.expectation.kind !== "interface" ||
|
||||
root.expectation.interfaceRevisionId !== child.subject
|
||||
)
|
||||
throw Error(`${at}: child root must be a ${child.subject} reference; unwrap nulls and lists explicitly`);
|
||||
if (
|
||||
child.when &&
|
||||
canonicalJson(queryBindingType(child.when, composition.input, outputs, item)) !==
|
||||
canonicalJson({ kind: "scalar", name: "bool" })
|
||||
)
|
||||
throw Error(`${at}: when requires a boolean`);
|
||||
const atoms = workspace.atoms.filter((atom) =>
|
||||
workspace.conformances.some((entry) => entry.atomId === atom.id && entry.interfaceRevisionId === child.subject),
|
||||
);
|
||||
if (!atoms.length) throw Error(`${at}: no installed subject atoms`);
|
||||
const branches = atoms.map((atom) => {
|
||||
let candidates = modules.filter(
|
||||
(entry) =>
|
||||
entry.contract.subject?.kind === "interface" &&
|
||||
entry.contract.subject.interfaceRevisionId === child.subject,
|
||||
);
|
||||
if (child.role) {
|
||||
const iface = workspace.interfaceImports.find((entry) => entry.revisionId === child.role!.interface);
|
||||
const member = iface?.members.find(
|
||||
(entry) => entry.displayName === child.role!.member || entry.id === child.role!.member,
|
||||
);
|
||||
if (member?.kind !== "module") throw Error(`${at}: unknown rendering role`);
|
||||
if (
|
||||
!workspace.conformances.some(
|
||||
(entry) => entry.atomId === atom.id && entry.interfaceRevisionId === iface!.revisionId,
|
||||
)
|
||||
)
|
||||
throw Error(`${at}: ${atom.id} does not implement the requested role`);
|
||||
candidates = candidates.filter(
|
||||
(entry) => entry.module.interfaceRevisionId === iface!.revisionId && entry.module.memberId === member.id,
|
||||
);
|
||||
} else {
|
||||
candidates = candidates.filter((entry) =>
|
||||
["org.quixos.react.subject-only/1", "org.quixos.react.component/2"].includes(entry.contract.contract),
|
||||
);
|
||||
}
|
||||
if (child.provider) {
|
||||
const pkg = parent.pkg.clientPackages?.[child.provider.package];
|
||||
if (!pkg) throw Error(`${at}: provider package is not imported`);
|
||||
candidates = candidates.filter(
|
||||
(entry) => entry.pkg.revisionId === pkg && entry.module.displayName === child.provider!.module,
|
||||
);
|
||||
} else {
|
||||
const bound = candidates.filter((entry) =>
|
||||
workspace.conformances.some(
|
||||
(conformance) =>
|
||||
conformance.atomId === atom.id &&
|
||||
conformance.interfaceRevisionId === entry.module.interfaceRevisionId &&
|
||||
conformance.moduleBindings?.some(
|
||||
(binding) =>
|
||||
binding.memberId === entry.module.memberId &&
|
||||
binding.packageRevisionId === entry.pkg.revisionId &&
|
||||
binding.moduleId === entry.module.id,
|
||||
),
|
||||
),
|
||||
);
|
||||
candidates = bound.length
|
||||
? bound
|
||||
: child.role
|
||||
? []
|
||||
: candidates.filter((entry) =>
|
||||
workspace.presentations?.some(
|
||||
(presentation) =>
|
||||
presentation.subject === child.subject &&
|
||||
presentation.packageRevisionId === entry.pkg.revisionId &&
|
||||
presentation.componentId === entry.module.id,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (candidates.length !== 1)
|
||||
throw Error(`${at}: ${atom.id} has ${candidates.length} compatible providers; select one explicitly`);
|
||||
const selected = candidates[0]!;
|
||||
dependencies.push(key(selected.pkg.revisionId, selected.module.id));
|
||||
return {
|
||||
atom: atom.id,
|
||||
packageRevision: selected.pkg.revisionId,
|
||||
id: selected.module.id,
|
||||
role: {
|
||||
interface: selected.module.interfaceRevisionId,
|
||||
member: selected.module.memberId,
|
||||
subject: child.subject,
|
||||
contract: selected.contract.contract,
|
||||
...moduleTypeContext(
|
||||
workspace.interfaceImports.find((entry) => entry.revisionId === selected.module.interfaceRevisionId)!,
|
||||
),
|
||||
},
|
||||
};
|
||||
});
|
||||
return { ...child, branches };
|
||||
});
|
||||
graph.set(key(parent.pkg.revisionId, parent.module.id), dependencies);
|
||||
Object.assign(parent.preparation, { children });
|
||||
}
|
||||
const visited = new Set<string>();
|
||||
const visit = (node: string, ancestors: string[]) => {
|
||||
if (ancestors.includes(node)) throw Error(`Eager React preparation cycle: ${[...ancestors, node].join(" -> ")}`);
|
||||
if (visited.has(node)) return;
|
||||
for (const child of graph.get(node) ?? []) visit(child, [...ancestors, node]);
|
||||
visited.add(node);
|
||||
};
|
||||
for (const node of graph.keys()) visit(node, []);
|
||||
await expandPresentationQueries(workspace);
|
||||
}
|
||||
Reference in New Issue
Block a user