import type { DomainBinding, KernelClient } from '@astrale-os/shell' import { schema } from '@astrale-os/sdk/schema' import { useSessionKernel } from '../auth/session-kernel.js' type Resource = | { readonly status: 'pending'; readonly promise: Promise } | { readonly status: 'ready'; readonly value: T } | { readonly status: 'failed'; readonly error: unknown } const typed = new WeakMap>>() const dynamic = new WeakMap>() const aggregates = new WeakMap< KernelClient, Map>>> >() const bindingByValue = new WeakMap() /** * Resolve an exact verified DomainBinding. The first render suspends while Session snapshot and schema * revision checks complete; later renders return the retained binding synchronously. */ export function useDomain(schema: S): DomainBinding { const kernel = useSessionKernel() let bySchema = typed.get(kernel) if (bySchema === undefined) { bySchema = new WeakMap() typed.set(kernel, bySchema) } let resource = bySchema.get(schema) if (resource === undefined) { resource = createResource(kernel.domain(schema)) as Resource bySchema.set(schema, resource) } const binding = read(resource) as unknown as DomainBinding registerBinding(binding as unknown as DomainBinding) return binding } /** Dynamic verified root binding used by tools that intentionally do not carry a schema witness. */ export function useDynamicDomain(): DomainBinding { const kernel = useSessionKernel() let resource = dynamic.get(kernel) if (resource === undefined) { resource = createResource(kernel.domain()) dynamic.set(kernel, resource) } const binding = read(resource) registerBinding(binding) return binding } /** Bind several exact installed schemas behind one Suspense boundary and stable named result. */ export function useDomains>>( schemas: Schemas, ): { readonly [Name in keyof Schemas]: DomainBinding } { const kernel = useSessionKernel() const names = Object.keys(schemas).sort() if (names.length === 0) return Object.freeze({}) as never const key = names .map((name) => `${name}\u0000${schemas[name]!.origin}\u0000${schema.revision(schemas[name]!)}`) .join('\u0001') let bySet = aggregates.get(kernel) if (bySet === undefined) { bySet = new Map() aggregates.set(kernel, bySet) } let resource = bySet.get(key) if (resource === undefined) { resource = createResource( Promise.all( names.map(async (name) => [name, await kernel.domain(schemas[name]!)] as const), ).then((entries) => Object.freeze(Object.fromEntries(entries))), ) bySet.set(key, resource) } const values = read(resource) Object.values(values).forEach(registerBinding) return values as { readonly [Name in keyof Schemas]: DomainBinding } } export function bindingFor(value: object): DomainBinding { const binding = bindingByValue.get(value) if (binding === undefined) { throw new Error('The definition is not owned by a Domain bound in this React runtime.') } return binding } function registerBinding(binding: DomainBinding): void { bindingByValue.set(binding, binding) bindingByValue.set(binding.domain, binding) for (const definition of Object.values(binding.domain.classes)) { bindingByValue.set(definition, binding) for (const method of definition.methods) bindingByValue.set(method, binding) for (const method of definition.static.methods) bindingByValue.set(method, binding) } for (const callable of Object.values(binding.domain.functions)) { bindingByValue.set(callable, binding) } } function createResource(promise: Promise): Resource { const cell: { status: 'pending' | 'ready' | 'failed' promise: Promise value?: T error?: unknown } = { status: 'pending', promise: Promise.resolve(), } cell.promise = promise.then( (value) => { cell.status = 'ready' cell.value = value }, (error: unknown) => { cell.status = 'failed' cell.error = error }, ) return cell as Resource } function read(resource: Resource): T { if (resource.status === 'pending') throw resource.promise if (resource.status === 'failed') throw resource.error return resource.value }