/** * The registration slots: every port a sibling or the embedder installs. One * owner: the slots live here; areas read them at call time (never captured), * and every disposer is identity-safe (it clears a slot only while the * registrant is still current). Executors are last-wins. No capability read * exposes a slot, so installing one wakes no reader. */ import type { Unsubscribe } from '@embedpdf/core'; import type { PdfActionType } from '@embedpdf/engine-core/runtime'; import type { ActionExecutor, ActionSubmitHandler, ActionUiAdapter } from '../contract'; import type { ActionsHostCapability, AnnotationCommitSink, FormCommitSink, FormFocus, FormReader, SubmitResolver, } from '../host-contract'; import type { ActionsEvents } from './events'; export interface ActionPorts { readonly executors: Map; annotCommitSink: AnnotationCommitSink | null; formCommitSink: FormCommitSink | null; /** The form plugin's form, as the user sees it. */ formReader: FormReader | null; /** The form plugin's cursor, for `field.setFocus()`. */ formFocus: FormFocus | null; uiAdapter: ActionUiAdapter | null; /** Sink 1 of the submit chain (consent = installation). */ submitHandler: ActionSubmitHandler | null; /** The form plugin's dataset resolver: the one door every submit resolves through. */ submitResolver: SubmitResolver | null; } export function createPorts({ reportDiagnostic }: Pick) { const ports: ActionPorts = { executors: new Map(), annotCommitSink: null, formCommitSink: null, formReader: null, formFocus: null, uiAdapter: null, submitHandler: null, submitResolver: null, }; const api = { registerExecutor: (type, executor): Unsubscribe => { if (ports.executors.has(type)) { reportDiagnostic( { code: 'duplicate-executor', message: `executor for '${type}' replaced (last-wins)` }, { action: type }, ); } ports.executors.set(type, executor); return () => { if (ports.executors.get(type) === executor) ports.executors.delete(type); }; }, registerAnnotCommitSink: (sink): Unsubscribe => { ports.annotCommitSink = sink; return () => { if (ports.annotCommitSink === sink) ports.annotCommitSink = null; }; }, registerFormCommitSink: (sink): Unsubscribe => { ports.formCommitSink = sink; return () => { if (ports.formCommitSink === sink) ports.formCommitSink = null; }; }, registerFormReader: (reader): Unsubscribe => { ports.formReader = reader; return () => { if (ports.formReader === reader) ports.formReader = null; }; }, registerFormFocus: (focus): Unsubscribe => { ports.formFocus = focus; return () => { if (ports.formFocus === focus) ports.formFocus = null; }; }, setSubmitHandler: (handler): Unsubscribe => { ports.submitHandler = handler; // Not an open-sequence release: that stays the UI adapter's role. return () => { if (ports.submitHandler === handler) ports.submitHandler = null; }; }, registerSubmitResolver: (resolver): Unsubscribe => { ports.submitResolver = resolver; return () => { if (ports.submitResolver === resolver) ports.submitResolver = null; }; }, } satisfies Partial; return { slots: ports, api }; }