Skip to content

Commit 633b60a

Browse files
committed
Fix wrong dependencies when both Android and iOS clients connected.

Also support more platform and more bundle entries.
1 parent 7c38339 commit 633b60a

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

local-cli/server/util/attachHMRServer.js

+46-29
Original file line numberDiff line numberDiff line change
@@ -106,44 +106,63 @@ function attachHMRServer({httpServer, path, packagerServer}) {
106106
path: path,
107107
});
108108

109-
const wsList = {
110-
clients: [],
111-
fileChangeListener: (type, filename) => {}
112-
};
113-
114-
const removeClient = function (wsClient) {
115-
wsList.clients.splice(wsList.clients.indexOf(wsClient), 1);
116-
if (wsList.clients.length === 0) {
117-
packagerServer.setHMRFileChangeListener(null);
109+
// Support multi hot reloading listener
110+
const watchClients: {
111+
[bundleEntry: string]: {
112+
clients: Object[],
113+
fileChangeListener: (type: string, filename: string) => string
118114
}
119-
};
120-
121-
const sendAllClients = function (data) {
122-
const removed = [];
123-
wsList.clients.forEach(wsClient => {
124-
try {
125-
wsClient.send(data);
126-
} catch (e) {
127-
wsClient.close();
128-
removed.push(wsClient);
129-
}
130-
});
131-
132-
removed.forEach(wsClient => {
133-
removeClient(wsClient);
134-
});
135-
};
115+
} = {};
136116

137117
const listener = (type, filename) => {
138-
wsList.fileChangeListener(type, filename);
118+
for (const bundleEntry in watchClients) {
119+
watchClients[bundleEntry].fileChangeListener(type, filename);
120+
}
139121
};
140122

141123
wss.on('connection', ws => {
142124
const params = querystring.parse(url.parse(ws.upgradeReq.url).query);
125+
126+
if (!params.bundleEntry) {
127+
return;
128+
}
129+
130+
packagerServer.setHMRFileChangeListener(listener);
131+
132+
// Group clients by bundleEntry (and platform, included in bundleEntry)
133+
const wsList = watchClients[params.bundleEntry] || {clients: []};
134+
watchClients[params.bundleEntry] = wsList;
135+
143136
const wasEmpty = wsList.clients.length === 0;
144137

145138
wsList.clients.push(ws);
146139

140+
const removeClient = function (wsClient) {
141+
wsList.clients.splice(wsList.clients.indexOf(wsClient), 1);
142+
if (wsList.clients.length === 0) {
143+
delete watchClients[params.bundleEntry];
144+
}
145+
if (Object.keys(watchClients).length === 0) {
146+
packagerServer.setHMRFileChangeListener(null);
147+
}
148+
};
149+
150+
const sendAllClients = function (data) {
151+
const removed = [];
152+
wsList.clients.forEach(wsClient => {
153+
try {
154+
wsClient.send(data);
155+
} catch (e) {
156+
wsClient.close();
157+
removed.push(wsClient);
158+
}
159+
});
160+
161+
removed.forEach(wsClient => {
162+
removeClient(wsClient);
163+
});
164+
};
165+
147166
ws.on('error', e => {
148167
removeClient(ws);
149168
console.error('[Hot Module Replacement] Unexpected error', e);
@@ -174,8 +193,6 @@ function attachHMRServer({httpServer, path, packagerServer}) {
174193
inverseDependenciesCache,
175194
};
176195

177-
packagerServer.setHMRFileChangeListener(listener);
178-
179196
wsList.fileChangeListener = (type, filename) => {
180197
if (!client) {
181198
return;

0 commit comments

Comments
 (0)