@@ -106,44 +106,63 @@ function attachHMRServer({httpServer, path, packagerServer}) {
106
106
path : path ,
107
107
} ) ;
108
108
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
118
114
}
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
+ } = { } ;
136
116
137
117
const listener = ( type , filename ) => {
138
- wsList . fileChangeListener ( type , filename ) ;
118
+ for ( const bundleEntry in watchClients ) {
119
+ watchClients [ bundleEntry ] . fileChangeListener ( type , filename ) ;
120
+ }
139
121
} ;
140
122
141
123
wss . on ( 'connection' , ws => {
142
124
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
+
143
136
const wasEmpty = wsList . clients . length === 0 ;
144
137
145
138
wsList . clients . push ( ws ) ;
146
139
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
+
147
166
ws . on ( 'error' , e => {
148
167
removeClient ( ws ) ;
149
168
console . error ( '[Hot Module Replacement] Unexpected error' , e ) ;
@@ -174,8 +193,6 @@ function attachHMRServer({httpServer, path, packagerServer}) {
174
193
inverseDependenciesCache,
175
194
} ;
176
195
177
- packagerServer . setHMRFileChangeListener ( listener ) ;
178
-
179
196
wsList . fileChangeListener = ( type , filename ) => {
180
197
if ( ! client ) {
181
198
return ;
0 commit comments