Repository navigation
Expand file tree
/
Copy pathsettings-runtime.ts
More file actions
166 lines (154 loc) · 6.6 KB
/
Copy pathsettings-runtime.ts
File metadata and controls
166 lines (154 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type { Interactor, RunnerContext } from './interactor-types.ts';
import type { RuntimeOperationFact, RuntimeOperationUnavailability } from './platform-runtime.ts';
import type { ReadSettingResult, SettingOptions } from './settings.ts';
import type { ReadableSetting, SettingName } from './settings-catalog.ts';
import type { SnapshotRuntimeExecution } from './snapshot-runtime.ts';
/**
* Neutral intent for one settings mutation. `setting` and `state` are the device-settings
* vocabulary the owner's own API is keyed by (`interactor.setSetting`), not a command payload:
* the daemon has already parsed the CLI form, validated it, resolved the target app, and typed
* the coordinates in `options`, so nothing command-shaped or argv-shaped travels here.
*/
export type SetSettingInput = Readonly<{
setting: string;
state: string;
/** The app the setting targets, already resolved from the request or the session. */
appBundleId?: string;
options?: SettingOptions;
/** Same runner metadata a capture needs; reuses that type rather than restating it. */
execution?: SnapshotRuntimeExecution;
}>;
/**
* Neutral intent for one settings read. It carries no `state` because a read asks for the value the
* device holds rather than asserting one, and its `setting` is narrowed to the readable list: the leg
* resolver only ever names a setting some owner answers, so an owner's read switch is exhaustive over
* it rather than a default case that refuses.
*/
export type ReadSettingInput = Readonly<{
setting: ReadableSetting;
/** Same runner metadata a capture needs; reuses that type rather than restating it. */
execution?: SnapshotRuntimeExecution;
}>;
/**
* Owners answer a write with their own settings payload or nothing at all, and a read with the
* payload of the setting it serves; the daemon composes the response text around whichever it gets.
*/
export type SettingsRuntimeOperations = Readonly<{
setSetting(input: SetSettingInput): Promise<Record<string, unknown> | void>;
readSetting(input: ReadSettingInput): Promise<ReadSettingResult>;
}>;
export type SettingsRuntimeOperationFacts = Readonly<{
setSetting: RuntimeOperationFact;
readSetting: RuntimeOperationFact;
}>;
/**
* Builds an owner's settings facts from the settings it serves. `target` gates every setting on the
* device in hand; each listed setting may narrow it further, and a setting the owner does not list
* answers with `unsupported`. An operation is available while any of its settings is; otherwise it
* answers with its first listed setting's refusal, or `unsupported` when it lists none.
*/
export function settingsRuntimeOperationFacts(
input: Readonly<{
target: RuntimeOperationFact;
unsupported: RuntimeOperationUnavailability;
write: Readonly<Partial<Record<SettingName, RuntimeOperationFact>>>;
read?: Readonly<Partial<Record<ReadableSetting, RuntimeOperationFact>>>;
}>,
): SettingsRuntimeOperationFacts {
return Object.freeze({
setSetting: settingSubjectsFact(input, input.write),
readSetting: settingSubjectsFact(input, input.read ?? {}),
});
}
function settingSubjectsFact(
owner: Readonly<{ target: RuntimeOperationFact; unsupported: RuntimeOperationUnavailability }>,
served: Readonly<Partial<Record<SettingName, RuntimeOperationFact>>>,
): RuntimeOperationFact {
if (!owner.target.available) return owner.target;
const listed = Object.values(served).filter((fact) => fact !== undefined);
if (!listed.some((fact) => fact.available)) return listed[0] ?? owner.unsupported;
const subjects = Object.fromEntries(
Object.entries(served).map(([name, fact]) => [name, fact.available ? AVAILABLE : fact]),
);
return Object.freeze({
available: true,
subjects: Object.freeze(subjects),
unlisted: owner.unsupported,
});
}
const AVAILABLE = Object.freeze({ available: true } as const);
/**
* Captures one selected owner's interactor authority for the lifetime of a request binding. The
* owner is already chosen by the time a binder is called, so each entry point supplies its own
* resolution and this holds only what both share: the runner context and the setting named.
*/
async function resolveSettingsInteractor(
signal: AbortSignal,
resolveInteractor: (runner: RunnerContext) => Promise<Interactor>,
input: { execution?: SnapshotRuntimeExecution; appBundleId?: string },
): Promise<Interactor> {
signal.throwIfAborted();
return await resolveInteractor({
...input.execution,
appBundleId: input.appBundleId,
signal,
});
}
export function bindSetSetting(
signal: AbortSignal,
resolveInteractor: (runner: RunnerContext) => Promise<Interactor>,
): Pick<SettingsRuntimeOperations, 'setSetting'> {
return Object.freeze({
setSetting: async (input: SetSettingInput) => {
const interactor = await resolveSettingsInteractor(signal, resolveInteractor, input);
return await interactor.setSetting(
input.setting,
input.state,
input.appBundleId,
input.options,
);
},
});
}
/**
* The read leg binds through the optional `readSetting` member, so an owner that never declares the
* fact is refused at admission rather than resolving an absent method into an empty answer — the
* same guard every optional-member binder takes.
*/
export function bindReadSetting(
signal: AbortSignal,
resolveInteractor: (runner: RunnerContext) => Promise<Interactor>,
): Pick<SettingsRuntimeOperations, 'readSetting'> {
return Object.freeze({
readSetting: async (input: ReadSettingInput) => {
const interactor = await resolveSettingsInteractor(signal, resolveInteractor, input);
// Loaded on the call rather than at module evaluation because this facade's eager closure is
// held at its merge-base size (`eager-closure-budgets`); a static edge would grow it.
const { requireInteractorMethod } = await import('./interactor-operation-binding.ts');
const method = requireInteractorMethod(interactor.readSetting, 'settings read');
return await method.call(interactor, input.setting);
},
});
}
/** The settings the shared Android adb interactor serves, wherever an owner runs it. */
export const ANDROID_ADB_SETTINGS = [
'wifi',
'airplane',
'location',
'animations',
'appearance',
'text-size',
'fingerprint',
'clear-app-state',
'permission',
] as const satisfies readonly SettingName[];
/** The same fact for each named setting. */
export function servedSettings<const Name extends SettingName>(
names: readonly Name[],
fact: RuntimeOperationFact,
): Readonly<Record<Name, RuntimeOperationFact>> {
return Object.fromEntries(names.map((name) => [name, fact])) as Record<
Name,
RuntimeOperationFact
>;
}