Skip to content

Commit 8a6d6c1

Browse files
authored
refactor: split request handler chain (#514)
* refactor: split request handler chain * fix: mark request handler chain import as type-only
1 parent bc7cf47 commit 8a6d6c1

2 files changed

Lines changed: 90 additions & 90 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { CommandFlags } from '../core/dispatch.ts';
2+
import type { AndroidAdbExecutor } from '../platforms/android/adb-executor.ts';
3+
import type { DaemonCommandContext } from './context.ts';
4+
import { handleFindCommands } from './handlers/find.ts';
5+
import { handleInteractionCommands } from './handlers/interaction.ts';
6+
import { handleLeaseCommands } from './handlers/lease.ts';
7+
import { handleRecordTraceCommands } from './handlers/record-trace.ts';
8+
import { handleSessionCommands } from './handlers/session.ts';
9+
import { handleSnapshotCommands } from './handlers/snapshot.ts';
10+
import type { LeaseRegistry } from './lease-registry.ts';
11+
import type { SessionStore } from './session-store.ts';
12+
import type { DaemonRequest, DaemonResponse } from './types.ts';
13+
14+
export async function runRequestHandlerChain(params: {
15+
req: DaemonRequest;
16+
sessionName: string;
17+
logPath: string;
18+
sessionStore: SessionStore;
19+
leaseRegistry: LeaseRegistry;
20+
invoke: (req: DaemonRequest) => Promise<DaemonResponse>;
21+
androidAdbExecutor?: AndroidAdbExecutor;
22+
contextFromFlags: (
23+
flags: CommandFlags | undefined,
24+
appBundleId?: string,
25+
traceLogPath?: string,
26+
) => DaemonCommandContext;
27+
}): Promise<DaemonResponse | null> {
28+
const {
29+
req,
30+
sessionName,
31+
logPath,
32+
sessionStore,
33+
leaseRegistry,
34+
invoke,
35+
androidAdbExecutor,
36+
contextFromFlags,
37+
} = params;
38+
39+
const leaseResponse = await handleLeaseCommands({ req, leaseRegistry });
40+
if (leaseResponse) return leaseResponse;
41+
42+
const sessionResponse = await handleSessionCommands({
43+
req,
44+
sessionName,
45+
logPath,
46+
sessionStore,
47+
invoke,
48+
androidAdbExecutor,
49+
});
50+
if (sessionResponse) return sessionResponse;
51+
52+
const snapshotResponse = await handleSnapshotCommands({
53+
req,
54+
sessionName,
55+
logPath,
56+
sessionStore,
57+
});
58+
if (snapshotResponse) return snapshotResponse;
59+
60+
const recordTraceResponse = await handleRecordTraceCommands({
61+
req,
62+
sessionName,
63+
sessionStore,
64+
logPath,
65+
});
66+
if (recordTraceResponse) return recordTraceResponse;
67+
68+
const findResponse = await handleFindCommands({
69+
req,
70+
sessionName,
71+
logPath,
72+
sessionStore,
73+
invoke,
74+
});
75+
if (findResponse) return findResponse;
76+
77+
const interactionResponse = await handleInteractionCommands({
78+
req,
79+
sessionName,
80+
logPath,
81+
sessionStore,
82+
contextFromFlags,
83+
});
84+
if (interactionResponse) return interactionResponse;
85+
86+
return null;
87+
}

src/daemon/request-router.ts

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
withAndroidAdbProvider,
3-
type AndroidAdbExecutor,
4-
} from '../platforms/android/adb-executor.ts';
1+
import { withAndroidAdbProvider } from '../platforms/android/adb-executor.ts';
52
import type { CommandFlags } from '../core/dispatch.ts';
63
import {
74
type DeviceInventoryProvider,
@@ -14,12 +11,6 @@ import {
1411
contextFromFlags as contextFromFlagsWithLog,
1512
type DaemonCommandContext,
1613
} from './context.ts';
17-
import { handleSessionCommands } from './handlers/session.ts';
18-
import { handleSnapshotCommands } from './handlers/snapshot.ts';
19-
import { handleFindCommands } from './handlers/find.ts';
20-
import { handleRecordTraceCommands } from './handlers/record-trace.ts';
21-
import { handleInteractionCommands } from './handlers/interaction.ts';
22-
import { handleLeaseCommands } from './handlers/lease.ts';
2314
import { assertSessionSelectorMatches } from './session-selector.ts';
2415
import { applyRequestLockPolicy } from './request-lock-policy.ts';
2516
import { resolveEffectiveSessionName } from './session-routing.ts';
@@ -49,6 +40,7 @@ import {
4940
refreshRecordingHealth,
5041
shouldBlockForInvalidRecording,
5142
} from './request-recording-health.ts';
43+
import { runRequestHandlerChain } from './request-handler-chain.ts';
5244

5345
const sessionExecutionLocks = new Map<string, Promise<unknown>>();
5446

@@ -75,85 +67,6 @@ function contextFromFlags(
7567
};
7668
}
7769

78-
// ---------------------------------------------------------------------------
79-
// Specialized handler chain
80-
// ---------------------------------------------------------------------------
81-
82-
async function runHandlerChain(params: {
83-
req: DaemonRequest;
84-
sessionName: string;
85-
logPath: string;
86-
sessionStore: SessionStore;
87-
leaseRegistry: LeaseRegistry;
88-
invoke: (req: DaemonRequest) => Promise<DaemonResponse>;
89-
androidAdbExecutor?: AndroidAdbExecutor;
90-
contextFromFlags: (
91-
flags: CommandFlags | undefined,
92-
appBundleId?: string,
93-
traceLogPath?: string,
94-
) => DaemonCommandContext;
95-
}): Promise<DaemonResponse | null> {
96-
const {
97-
req,
98-
sessionName,
99-
logPath,
100-
sessionStore,
101-
leaseRegistry,
102-
invoke,
103-
androidAdbExecutor,
104-
contextFromFlags,
105-
} = params;
106-
107-
const leaseResponse = await handleLeaseCommands({ req, leaseRegistry });
108-
if (leaseResponse) return leaseResponse;
109-
110-
const sessionResponse = await handleSessionCommands({
111-
req,
112-
sessionName,
113-
logPath,
114-
sessionStore,
115-
invoke,
116-
androidAdbExecutor,
117-
});
118-
if (sessionResponse) return sessionResponse;
119-
120-
const snapshotResponse = await handleSnapshotCommands({
121-
req,
122-
sessionName,
123-
logPath,
124-
sessionStore,
125-
});
126-
if (snapshotResponse) return snapshotResponse;
127-
128-
const recordTraceResponse = await handleRecordTraceCommands({
129-
req,
130-
sessionName,
131-
sessionStore,
132-
logPath,
133-
});
134-
if (recordTraceResponse) return recordTraceResponse;
135-
136-
const findResponse = await handleFindCommands({
137-
req,
138-
sessionName,
139-
logPath,
140-
sessionStore,
141-
invoke,
142-
});
143-
if (findResponse) return findResponse;
144-
145-
const interactionResponse = await handleInteractionCommands({
146-
req,
147-
sessionName,
148-
logPath,
149-
sessionStore,
150-
contextFromFlags,
151-
});
152-
if (interactionResponse) return interactionResponse;
153-
154-
return null;
155-
}
156-
15770
// ---------------------------------------------------------------------------
15871
// Request handler API
15972
// ---------------------------------------------------------------------------
@@ -260,7 +173,7 @@ export function createRequestHandler(
260173
// The ADB provider is scoped to this single locked request; handlers may re-read
261174
// the session state, but all device-scoped adb calls in this request share it.
262175
// Phase 1: Try specialized handler chain
263-
const handlerResponse = await runHandlerChain({
176+
const handlerResponse = await runRequestHandlerChain({
264177
req: lockedReq,
265178
sessionName,
266179
logPath,

0 commit comments

Comments
 (0)