Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@
"types": "./src/ios-target-activation.ts",
"default": "./src/ios-target-activation.ts"
},
"./interactor-operation-binding": {
"types": "./src/interactor-operation-binding.ts",
"default": "./src/interactor-operation-binding.ts"
},
"./interactor-operation-catalog": {
"types": "./src/interactor-operation-catalog.ts",
"default": "./src/interactor-operation-catalog.ts"
Expand Down
27 changes: 26 additions & 1 deletion packages/contracts/src/alert-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InteractorOperationDefinition } from './interactor-operation-binding.ts';
import type { AlertInteractorOptions, Interactor, RunnerContext } from './interactor-types.ts';
import type { RuntimeOperationFact } from './platform-runtime.ts';
import type { SessionSurface } from './session-surface.ts';
Expand Down Expand Up @@ -109,7 +110,7 @@ function alertInteractorOptions(
type AlertLeg = 'readAlert' | 'awaitAlert' | 'acceptAlert' | 'dismissAlert';

/** How a fail-closed refusal names each leg to the caller, matching the command that asked for it. */
export const ALERT_LEG_LABELS = {
const ALERT_LEG_LABELS = {
readAlert: 'alert get',
awaitAlert: 'alert wait',
acceptAlert: 'alert accept',
Expand All @@ -132,3 +133,27 @@ export function bindAlertLeg<Leg extends AlertLeg>(
},
}) as Readonly<Record<Leg, (input: AlertRuntimeInput) => Promise<Record<string, unknown>>>>;
}

/** The family's rows for the interactor-operation adapters: one fact, one bind call. */
export const ALERT_INTERACTOR_OPERATIONS = [
{
operation: 'readAlert',
label: ALERT_LEG_LABELS.readAlert,
bind: (signal, resolve) => bindAlertLeg('readAlert', signal, resolve),
},
{
operation: 'awaitAlert',
label: ALERT_LEG_LABELS.awaitAlert,
bind: (signal, resolve) => bindAlertLeg('awaitAlert', signal, resolve),
},
{
operation: 'acceptAlert',
label: ALERT_LEG_LABELS.acceptAlert,
bind: (signal, resolve) => bindAlertLeg('acceptAlert', signal, resolve),
},
{
operation: 'dismissAlert',
label: ALERT_LEG_LABELS.dismissAlert,
bind: (signal, resolve) => bindAlertLeg('dismissAlert', signal, resolve),
},
] as const satisfies readonly InteractorOperationDefinition<AlertRuntimeOperations>[];
6 changes: 6 additions & 0 deletions packages/contracts/src/app-event-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InteractorOperationDefinition } from './interactor-operation-binding.ts';
import type { Interactor, RunnerContext } from './interactor-types.ts';
import type { RuntimeOperationFact } from './platform-runtime.ts';
import type { SnapshotRuntimeExecution } from './snapshot-runtime.ts';
Expand Down Expand Up @@ -55,3 +56,8 @@ export function bindAppEvent(
},
});
}

/** The family's rows for the interactor-operation adapters: one fact, one bind call. */
export const APP_EVENT_INTERACTOR_OPERATIONS = [
{ operation: 'triggerAppEvent', label: 'trigger-app-event', bind: bindAppEvent },
] as const satisfies readonly InteractorOperationDefinition<AppEventRuntimeOperations>[];
6 changes: 6 additions & 0 deletions packages/contracts/src/back-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InteractorOperationDefinition } from './interactor-operation-binding.ts';
import type { BackMode, Interactor, RunnerContext } from './interactor-types.ts';
import type { RuntimeOperationFact } from './platform-runtime.ts';
import type { SnapshotRuntimeExecution } from './snapshot-runtime.ts';
Expand Down Expand Up @@ -52,3 +53,8 @@ export function bindBack(
},
});
}

/** The family's rows for the interactor-operation adapters: one fact, one bind call. */
export const BACK_INTERACTOR_OPERATIONS = [
{ operation: 'back', label: 'back', bind: bindBack },
] as const satisfies readonly InteractorOperationDefinition<BackRuntimeOperations>[];
17 changes: 16 additions & 1 deletion packages/contracts/src/clipboard-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InteractorOperationDefinition } from './interactor-operation-binding.ts';
import type { Interactor, RunnerContext } from './interactor-types.ts';
import type { RuntimeOperationFact, RuntimeOperationUnavailability } from './platform-runtime.ts';
import type { SnapshotRuntimeExecution } from './snapshot-runtime.ts';
Expand Down Expand Up @@ -70,7 +71,7 @@ export function clipboardRuntimeOperationFacts(
}

/** How a fail-closed refusal names each clipboard half to the caller. */
export const CLIPBOARD_LEG_LABELS = {
const CLIPBOARD_LEG_LABELS = {
readClipboard: 'clipboard read',
writeClipboard: 'clipboard write',
} as const;
Expand Down Expand Up @@ -131,3 +132,17 @@ export function bindClipboardWrite(
},
});
}

/** The family's rows for the interactor-operation adapters: one fact, one bind call. */
export const CLIPBOARD_INTERACTOR_OPERATIONS = [
{
operation: 'readClipboard',
label: CLIPBOARD_LEG_LABELS.readClipboard,
bind: bindClipboardRead,
},
{
operation: 'writeClipboard',
label: CLIPBOARD_LEG_LABELS.writeClipboard,
bind: bindClipboardWrite,
},
] as const satisfies readonly InteractorOperationDefinition<ClipboardRuntimeOperations>[];
74 changes: 74 additions & 0 deletions packages/contracts/src/interactor-operation-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,77 @@ export function providerInteractorSource(
);
};
}

/**
* How a family turns one resolved interactor into one of its own typed operations. Every row
* shares this shape, which is what lets one adapter drive all of them: the family owns what the
* operation *does* and declares its rows beside its binders, and the adapters below own only which
* interactor a row reaches and how a refusal reads.
*/
export type InteractorOperationDefinition<Operations extends object> = Readonly<{
/** The facts key that admits it, and the operations key it binds. */
operation: keyof Operations;
/** How a provider fail-closed refusal names this operation to the caller. */
label: string;
bind: (
signal: AbortSignal,
resolveInteractor: (runner: RunnerContext) => Promise<Interactor>,
) => Partial<Operations>;
}>;

/**
* `operations` is the rows of every family the owner implements, so an owner evaluates only those
* families. `facts[operation]` alone still decides whether a row binds: `facts` is an owner's full
* `RuntimeFacts.operations` (extra keys are ignored) or a narrower standalone map, and a key it
* never defines is never bindable.
*/
type AdmittedInteractorOperationParams<Operations extends object, Resolver> = Readonly<{
device: DeviceInfo;
signal: AbortSignal;
resolveInteractor: Resolver;
operations: readonly InteractorOperationDefinition<Operations>[];
facts: Readonly<Partial<Record<keyof Operations, Readonly<{ available: boolean }>>>>;
}>;

/**
* Walks the rows once, binding each operation the facts admitted through whichever interactor
* source the caller's entry point supplies. The source is the only thing that differs between a
* local owner and a provider.
*/
function bindAdmittedInteractorOperations<Operations extends object, Resolver>(
params: AdmittedInteractorOperationParams<Operations, Resolver>,
source: (
definition: InteractorOperationDefinition<Operations>,
) => (runner: RunnerContext) => Promise<Interactor>,
): Partial<Operations> {
const bound: Partial<Operations> = {};
for (const definition of params.operations) {
if (!params.facts[definition.operation]?.available) continue;
Object.assign(bound, definition.bind(params.signal, source(definition)));
}
return bound;
}

/**
* Binds whichever local operations the owner's own facts admitted. The owner keeps full
* authority — its facts alone decide what binds — this only removes the once-per-operation
* ternary that read them.
*/
export function bindAdmittedLocalInteractorOperations<Operations extends object>(
params: AdmittedInteractorOperationParams<Operations, LocalInteractorOperationResolver>,
): Partial<Operations> {
return bindAdmittedInteractorOperations(params, () => localInteractorSource(params));
}

/**
* Binds whichever provider operations the owner's own facts admitted, failing closed when the
* exact owner no longer exposes its interactor. Each refusal names the operation the caller asked
* for, which is what the per-operation `label` carries.
*/
export function bindAdmittedProviderInteractorOperations<Operations extends object>(
params: AdmittedInteractorOperationParams<Operations, ProviderInteractorOperationResolver>,
): Partial<Operations> {
return bindAdmittedInteractorOperations(params, (definition) =>
providerInteractorSource({ ...params, operation: definition.label }),
);
}
13 changes: 10 additions & 3 deletions packages/contracts/src/interactor-operation-catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { expect, test, vi } from 'vitest';
import {
bindAdmittedLocalInteractorOperations,
bindAdmittedProviderInteractorOperations,
} from './interactor-operation-catalog.ts';
} from './interactor-operation-binding.ts';
import { INTERACTOR_OPERATIONS } from './interactor-operation-catalog.ts';
import type { Interactor } from './interactor-types.ts';

const device = {
Expand All @@ -18,6 +19,7 @@ const unavailable = { available: false, reason: 'unsupported-device-kind' } as c

test('skips an operation the facts refused', () => {
const operations = bindAdmittedLocalInteractorOperations({
operations: INTERACTOR_OPERATIONS,
device,
signal: new AbortController().signal,
resolveInteractor: vi.fn(),
Expand All @@ -28,8 +30,9 @@ test('skips an operation the facts refused', () => {
expect(operations.home).toBeUndefined();
});

// The facts a caller passes are the ONLY source of truth for what binds — there is no separate,
// caller-maintained operation list that could drift from them.
// Among the rows a caller supplies, its facts are the ONLY source of truth for what binds. An owner
// that supplies fewer families than the catalog pins, in its own runtime test, that its facts
// admit no operation outside them.
test('binds every one of the seven navigation operations when every fact admits it', async () => {
const back = vi.fn(async () => undefined);
const home = vi.fn(async () => undefined);
Expand All @@ -52,6 +55,7 @@ test('binds every one of the seven navigation operations when every fact admits
);

const operations = bindAdmittedLocalInteractorOperations({
operations: INTERACTOR_OPERATIONS,
device,
signal: new AbortController().signal,
resolveInteractor,
Expand Down Expand Up @@ -93,6 +97,7 @@ test('binds every one of the seven navigation operations when every fact admits

test('treats an operation missing from a partial facts map as unavailable', () => {
const operations = bindAdmittedLocalInteractorOperations({
operations: INTERACTOR_OPERATIONS,
device,
signal: new AbortController().signal,
resolveInteractor: vi.fn(),
Expand All @@ -108,6 +113,7 @@ test('a provider binding drives its own resolved interactor for every admitted o
const resolveInteractor = vi.fn(() => ({ tvRemote }) as unknown as Interactor);

const operations = bindAdmittedProviderInteractorOperations({
operations: INTERACTOR_OPERATIONS,
device,
signal: new AbortController().signal,
resolveInteractor,
Expand All @@ -123,6 +129,7 @@ test('a provider binding drives its own resolved interactor for every admitted o

test('a provider binding fails closed when its exact owner exposes no interactor', async () => {
const operations = bindAdmittedProviderInteractorOperations({
operations: INTERACTOR_OPERATIONS,
device,
signal: new AbortController().signal,
resolveInteractor: () => undefined,
Expand Down
Loading
Loading