|
| 1 | +import type { Interactor, RunnerContext } from './interactor-types.ts'; |
| 2 | +import type { RuntimeOperationFact } from './platform-runtime.ts'; |
| 3 | +import type { SnapshotRuntimeExecution } from './snapshot-runtime.ts'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Neutral intent for one Action Button press: no arguments, because `XCUIDevice.press(.action)` |
| 7 | + * takes none. Only runner metadata travels, so this is `home`'s shape — the two differ in what |
| 8 | + * they do to the device, not in what they carry. |
| 9 | + */ |
| 10 | +export type ActionButtonInput = Readonly<{ |
| 11 | + options?: Readonly<{ appBundleId?: string }>; |
| 12 | + /** Same runner metadata a capture needs; reuses that type rather than restating it. */ |
| 13 | + execution?: SnapshotRuntimeExecution; |
| 14 | +}>; |
| 15 | + |
| 16 | +/** The press returns nothing; a result type here would be a surface the command never has. */ |
| 17 | +export type ActionButtonRuntimeOperations = Readonly<{ |
| 18 | + actionButton(input: ActionButtonInput): Promise<void>; |
| 19 | +}>; |
| 20 | + |
| 21 | +export type ActionButtonRuntimeOperationFacts = Readonly<{ |
| 22 | + actionButton: RuntimeOperationFact; |
| 23 | +}>; |
| 24 | + |
| 25 | +export function actionButtonRuntimeOperationFacts( |
| 26 | + input: Readonly<{ actionButton: RuntimeOperationFact }>, |
| 27 | +): ActionButtonRuntimeOperationFacts { |
| 28 | + return Object.freeze({ actionButton: input.actionButton }); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Captures one selected owner's interactor authority for the lifetime of a request binding. |
| 33 | + * |
| 34 | + * The member is required rather than optional even though only Apple can press the button, which is |
| 35 | + * how `tvRemote` already handles a button only some owners have. An optional member would turn a |
| 36 | + * fact that advertises the press without an interactor that performs it into a successful-looking |
| 37 | + * no-op, and the response would report a press that never happened. Owners without the hardware |
| 38 | + * declare the refusal on the interactor, and the fact is what keeps that throw off every supported |
| 39 | + * path. |
| 40 | + */ |
| 41 | +export function bindActionButton( |
| 42 | + signal: AbortSignal, |
| 43 | + resolveInteractor: (runner: RunnerContext) => Promise<Interactor>, |
| 44 | +): ActionButtonRuntimeOperations { |
| 45 | + return Object.freeze({ |
| 46 | + actionButton: async (input: ActionButtonInput) => { |
| 47 | + signal.throwIfAborted(); |
| 48 | + const interactor = await resolveInteractor({ |
| 49 | + ...input.execution, |
| 50 | + appBundleId: input.options?.appBundleId, |
| 51 | + signal, |
| 52 | + }); |
| 53 | + await interactor.actionButton(); |
| 54 | + }, |
| 55 | + }); |
| 56 | +} |
0 commit comments