select method
Future<PatchbayInvocation>
select({
- required PatchbayInspectSelectRequestWire request,
- required String requestId,
Implementation
Future<PatchbayInvocation> select({
required PatchbayInspectSelectRequestWire request,
required String requestId,
}) async {
if (_unavailableNow case final PatchbayInspectUnavailableWire unavailable) {
return _unavailable(requestId, unavailable);
}
final int? ttlMs = request.ttlMs;
if (!request.enabled && ttlMs != null) {
// A lease is the thing that expires *while the mode is on*; asking for
// one while turning the mode off states two intents at once, so it is
// refused rather than silently dropped.
return _reject(
requestId,
'invalidInspectArguments',
details: <String, Object?>{
'invalid': const <String>['ttlMs'],
'reason': 'ttlMs applies only when enabled is true',
},
);
}
if (ttlMs != null &&
(ttlMs <= 0 || ttlMs > _policy.maxLease.inMilliseconds)) {
return _reject(
requestId,
'invalidInspectArguments',
details: <String, Object?>{
'invalid': const <String>['ttlMs'],
'maxTtlMs': _policy.maxLease.inMilliseconds,
},
);
}
final PatchbayGateRejection? gate = await _gates.evaluate(_policy.gates);
if (gate != null) return _gateRejected(requestId, gate);
// A gate may await, and two things can change underneath it: a consumer
// that injects its own inspector can make the overlay unavailable, and the
// host can be torn down entirely. Resuming into a disposed bridge would
// turn the flag on with nobody left holding a lease to put it back —
// the device would keep swallowing taps. So the whole availability
// question is asked again on this side of the wait rather than assumed.
if (_unavailableNow case final PatchbayInspectUnavailableWire after) {
return _unavailable(requestId, after);
}
final bool previous = _surface.selectMode;
if (request.enabled) {
// Baseline is captured once per managed span: renewing a lease must not
// rewrite "what it was before Patchbay touched it" with the value
// Patchbay itself installed. A new span also drops the previous span's
// release reason, which would otherwise read as an explanation of a
// switch that is currently on.
if (_restoresTo == null) {
_restoresTo = previous;
_lastRelease = null;
}
_surface.selectMode = true;
_arm(
ttlMs == null ? _policy.defaultLease : Duration(milliseconds: ttlMs),
);
} else {
// Explicit off means off, even when the baseline was on: the operator
// asked for this one, unlike the lease, which only undoes what Patchbay
// did.
_release(PatchbayInspectReleaseWire.explicitOff, restore: false);
_surface.selectMode = false;
}
return PatchbayInvocation.accepted(
requestId: requestId,
payload: _state(
outcome: 'applied',
previousSelectMode: previous,
).toJson(),
);
}