From f2ca178b18ba9a168a43668635607d31a9c15cde Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Sat, 27 May 2023 21:54:39 +0200 Subject: [PATCH] Allow device identifiers when registering inspectable device --- .../src/InspectorProxy.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/metro-inspector-proxy/src/InspectorProxy.js b/packages/metro-inspector-proxy/src/InspectorProxy.js index 7373d17fbf..dc59a192be 100644 --- a/packages/metro-inspector-proxy/src/InspectorProxy.js +++ b/packages/metro-inspector-proxy/src/InspectorProxy.js @@ -162,16 +162,28 @@ class InspectorProxy { // $FlowFixMe[value-as-type] wss.on('connection', async (socket: WS, req) => { try { + const fallbackDeviceId = String(this._deviceCounter++); + const query = url.parse(req.url || '', true).query || {}; + const deviceId = query.device || fallbackDeviceId; const deviceName = query.name || 'Unknown'; const appName = query.app || 'Unknown'; - const deviceId = String(this._deviceCounter++); + + const oldDevice = this._devices.get(deviceId); + if (oldDevice) { + // Keep the debugger connection alive when disconnecting the device, if possible + if (oldDevice._name === deviceName && oldDevice._app === appName) { + oldDevice._debuggerConnection = null; + } + oldDevice._deviceSocket.close(); + } + this._devices.set( - SdeviceId, + deviceId, new Device(deviceId, deviceName, appName, socket, this._projectRoot), ); - debug(`Got new connection: device=${deviceName}, app=${appName}`); + debug(`Got new connection: device=${deviceName}, app=${appName}, id=${deviceId}`); socket.on('close', () => { this._devices.delete(deviceId);