Skip to content

Commit d1113cd

Browse files
vzaidmanfacebook-github-bot
authored andcommitted
allow exposing the list of connected devices, and subscribe to that list being changed. (#54447)
Summary: Changelog: [Internal] Differential Revision: D86319965
1 parent ec7f729 commit d1113cd

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/dev-middleware/src/createDevMiddleware.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import type {CreateCustomMessageHandlerFn} from './inspector-proxy/CustomMessageHandler';
12+
import type Device from './inspector-proxy/Device';
1213
import type {BrowserLauncher} from './types/BrowserLauncher';
1314
import type {EventReporter, ReportableEvent} from './types/EventReporter';
1415
import type {Experiments, ExperimentsConfig} from './types/Experiments';
@@ -73,6 +74,10 @@ type Options = $ReadOnly<{
7374
type DevMiddlewareAPI = $ReadOnly<{
7475
middleware: NextHandleFunction,
7576
websocketEndpoints: {[path: string]: ws$WebSocketServer},
77+
getConnectedDevices: () => $ReadOnlyArray<Device>,
78+
onDevicesListChanged: (
79+
callback: (devices: Map<string, Device>) => void,
80+
) => () => void,
7681
}>;
7782

7883
export default function createDevMiddleware({
@@ -131,6 +136,8 @@ export default function createDevMiddleware({
131136
return {
132137
middleware,
133138
websocketEndpoints: inspectorProxy.createWebSocketListeners(),
139+
getConnectedDevices: () => inspectorProxy.getConnectedDevices(),
140+
onDevicesListChanged: cb => inspectorProxy.onDevicesListChanged(cb),
134141
};
135142
}
136143

packages/dev-middleware/src/inspector-proxy/InspectorProxy.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export default class InspectorProxy implements InspectorProxyQueries {
9797

9898
#eventLoopPerfTracker: EventLoopPerfTracker;
9999

100+
#onDevicesListChangedCallbacks: Set<(devices: Map<string, Device>) => void>;
101+
100102
constructor(
101103
serverBaseUrl: string,
102104
eventReporter: ?EventReporter,
@@ -111,6 +113,7 @@ export default class InspectorProxy implements InspectorProxyQueries {
111113
this.#experiments = experiments;
112114
this.#logger = logger;
113115
this.#customMessageHandler = customMessageHandler;
116+
this.#onDevicesListChangedCallbacks = new Set();
114117
if (trackEventLoopPerf) {
115118
this.#eventLoopPerfTracker = new EventLoopPerfTracker({
116119
perfMeasurementDuration: EVENT_LOOP_PERF_MEASUREMENT_MS,
@@ -142,6 +145,19 @@ export default class InspectorProxy implements InspectorProxyQueries {
142145
}
143146
}
144147

148+
getConnectedDevices(): Array<Device> {
149+
return Array.from(this.#devices.values());
150+
}
151+
152+
onDevicesListChanged(
153+
callback: (devices: Map<string, Device>) => void,
154+
): () => void {
155+
this.#onDevicesListChangedCallbacks.add(callback);
156+
return () => {
157+
this.#onDevicesListChangedCallbacks.delete(callback);
158+
};
159+
}
160+
145161
getPageDescriptions({
146162
requestorRelativeBaseUrl,
147163
logNoPagesForConnectedDevice = false,
@@ -365,6 +381,7 @@ export default class InspectorProxy implements InspectorProxyQueries {
365381
}
366382

367383
this.#devices.set(deviceId, newDevice);
384+
this.#onDevicesListChangedCallbacks.forEach(cb => cb(this.#devices));
368385

369386
debug(
370387
"Got new device connection: name='%s', app=%s, device=%s, via=%s",
@@ -457,6 +474,9 @@ export default class InspectorProxy implements InspectorProxyQueries {
457474

458475
if (this.#devices.get(deviceId)?.dangerouslyGetSocket() === socket) {
459476
this.#devices.delete(deviceId);
477+
this.#onDevicesListChangedCallbacks.forEach(cb =>
478+
cb(this.#devices),
479+
);
460480
}
461481
});
462482
} catch (error) {

0 commit comments

Comments
 (0)