From f231396260480713f89d49105b659829aa9ec0a7 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Thu, 15 Mar 2018 15:28:00 -0700 Subject: [PATCH] Fix HMR connection issues when the client gets reconnects Reviewed By: BYK Differential Revision: D7288613 fbshipit-source-id: ac8b5800af0bfaf1c2a25108b564092e3a47273c --- packages/metro/src/DeltaBundler/index.js | 10 ++++++++++ packages/metro/src/HmrServer/index.js | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/metro/src/DeltaBundler/index.js b/packages/metro/src/DeltaBundler/index.js index 7bebc383..5e8f31e9 100644 --- a/packages/metro/src/DeltaBundler/index.js +++ b/packages/metro/src/DeltaBundler/index.js @@ -49,6 +49,16 @@ class DeltaBundler { this._deltaTransformers = new Map(); } + endTransformer(clientId: string) { + const deltaTransformer = this._deltaTransformers.get(clientId); + + if (deltaTransformer) { + deltaTransformer.end(); + + this._deltaTransformers.delete(clientId); + } + } + async getDeltaTransformer( clientId: string, options: BundleOptions, diff --git a/packages/metro/src/HmrServer/index.js b/packages/metro/src/HmrServer/index.js index 34a9c382..4a42724c 100644 --- a/packages/metro/src/HmrServer/index.js +++ b/packages/metro/src/HmrServer/index.js @@ -26,6 +26,7 @@ import type PackagerServer from '../Server'; import type {Reporter} from '../lib/reporting'; type Client = {| + clientId: string, deltaTransformer: DeltaTransformer, sendFn: (data: string) => mixed, |}; @@ -74,7 +75,7 @@ class HmrServer { this._lastSequenceId = id; // Listen to file changes. - const client = {sendFn, deltaTransformer}; + const client = {clientId: clientUrl, deltaTransformer, sendFn}; deltaTransformer.on('change', this._handleFileChange.bind(this, client)); return client; @@ -91,7 +92,7 @@ class HmrServer { onClientDisconnect(client: TClient) { // We can safely stop the delta transformer since the // transformer is not shared between clients. - client.deltaTransformer.end(); + this._packagerServer.getDeltaBundler().endTransformer(client.clientId); } async _handleFileChange(client: Client) {