@@ -12,12 +12,6 @@ import { isApplePlatform, normalizePlatformSelector } from '../utils/device.ts';
1212
1313type LockPlatform = NonNullable < DaemonRequest [ 'meta' ] > [ 'lockPlatform' ] ;
1414
15- type LockPolicyContext = {
16- allowsSelectorOverride : boolean ;
17- conflicts : SessionSelectorConflict [ ] ;
18- lockPlatform : LockPlatform ;
19- } ;
20-
2115const LOCKABLE_SELECTOR_KEYS : Array < keyof CommandFlags > = [
2216 'target' ,
2317 'device' ,
@@ -42,11 +36,25 @@ export function applyRequestLockPolicy(
4236 }
4337
4438 const nextFlags : CommandFlags = { ...( req . flags ?? { } ) } ;
45- const context = resolveLockPolicyContext ( req , existingSession , nextFlags ) ;
46-
47- if ( context . conflicts . length === 0 ) {
48- if ( shouldApplyLockPlatformDefault ( context , existingSession , nextFlags ) ) {
49- nextFlags . platform = context . lockPlatform ;
39+ const allowsSelectorOverride = SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS . has ( req . command ) ;
40+ const conflicts = listLockPolicyConflicts (
41+ req ,
42+ existingSession ,
43+ nextFlags ,
44+ allowsSelectorOverride ,
45+ ) ;
46+ const lockPlatform = req . meta ?. lockPlatform ;
47+
48+ if ( conflicts . length === 0 ) {
49+ if (
50+ shouldApplyLockPlatformDefault (
51+ allowsSelectorOverride ,
52+ existingSession ,
53+ nextFlags ,
54+ lockPlatform ,
55+ )
56+ ) {
57+ nextFlags . platform = lockPlatform ;
5058 }
5159 return {
5260 ...req ,
@@ -55,7 +63,7 @@ export function applyRequestLockPolicy(
5563 }
5664
5765 if ( lockPolicy === 'strip' ) {
58- applyStripLockPolicy ( nextFlags , context , existingSession ) ;
66+ applyStripLockPolicy ( nextFlags , conflicts , lockPlatform , existingSession ) ;
5967 return {
6068 ...req ,
6169 flags : nextFlags ,
@@ -64,24 +72,11 @@ export function applyRequestLockPolicy(
6472
6573 throw new AppError (
6674 'INVALID_ARGS' ,
67- `${ req . command } cannot override session lock policy with ${ context . conflicts . map ( formatSessionSelectorConflict ) . join ( ', ' ) } . ` +
75+ `${ req . command } cannot override session lock policy with ${ conflicts . map ( formatSessionSelectorConflict ) . join ( ', ' ) } . ` +
6876 'Unset those selectors or remove the request lock policy.' ,
6977 ) ;
7078}
7179
72- function resolveLockPolicyContext (
73- req : DaemonRequest ,
74- existingSession : SessionState | undefined ,
75- flags : CommandFlags ,
76- ) : LockPolicyContext {
77- const allowsSelectorOverride = SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS . has ( req . command ) ;
78- return {
79- allowsSelectorOverride,
80- conflicts : listLockPolicyConflicts ( req , existingSession , flags , allowsSelectorOverride ) ,
81- lockPlatform : req . meta ?. lockPlatform ,
82- } ;
83- }
84-
8580function listLockPolicyConflicts (
8681 req : DaemonRequest ,
8782 existingSession : SessionState | undefined ,
@@ -95,30 +90,32 @@ function listLockPolicyConflicts(
9590}
9691
9792function shouldApplyLockPlatformDefault (
98- context : LockPolicyContext ,
93+ allowsSelectorOverride : boolean ,
9994 existingSession : SessionState | undefined ,
10095 flags : CommandFlags ,
96+ lockPlatform : LockPlatform ,
10197) : boolean {
102- if ( ! context . lockPlatform || existingSession || flags . platform !== undefined ) {
98+ if ( ! lockPlatform || existingSession || flags . platform !== undefined ) {
10399 return false ;
104100 }
105- if ( ! context . allowsSelectorOverride ) {
101+ if ( ! allowsSelectorOverride ) {
106102 return true ;
107103 }
108104 return flags . serial === undefined && flags . androidDeviceAllowlist === undefined ;
109105}
110106
111107function applyStripLockPolicy (
112108 flags : CommandFlags ,
113- context : LockPolicyContext ,
109+ conflicts : SessionSelectorConflict [ ] ,
110+ lockPlatform : LockPlatform ,
114111 existingSession : SessionState | undefined ,
115112) : void {
116113 if ( existingSession ) {
117- stripSessionConflicts ( flags , context . conflicts ) ;
114+ stripSessionConflicts ( flags , conflicts ) ;
118115 flags . platform = existingSession . device . platform ;
119116 return ;
120117 }
121- stripFreshSessionConflicts ( flags , context . lockPlatform ) ;
118+ stripFreshSessionConflicts ( flags , lockPlatform ) ;
122119}
123120
124121function listFreshSessionConflicts (
0 commit comments