Skip to content

Commit 9a19867

Browse files
rafecafacebook-github-bot
authored andcommitted
Make the React Native HMR client extend from the generic metro HMR client
Reviewed By: BYK Differential Revision: D6752278 fbshipit-source-id: 5c93cba3e9f3cee2119cb90a711909e0b4a5b835
1 parent b8e79a7 commit 9a19867

File tree

1 file changed

+46
-81
lines changed

1 file changed

+46
-81
lines changed

Libraries/Utilities/HMRClient.js

+46-81
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*
99
* @providesModule HMRClient
10+
* @format
1011
* @flow
1112
*/
1213
'use strict';
1314

1415
const Platform = require('Platform');
1516
const invariant = require('fbjs/lib/invariant');
1617

18+
const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');
19+
1720
/**
1821
* HMR Client that receives from the server HMR updates and propagates them
1922
* runtime to reflects those changes.
@@ -24,110 +27,72 @@ const HMRClient = {
2427
invariant(bundleEntry, 'Missing required paramenter `bundleEntry`');
2528
invariant(host, 'Missing required paramenter `host`');
2629

27-
// need to require WebSocket inside of `enable` function because
28-
// this module is defined as a `polyfillGlobal`.
29-
// See `InitializeJavascriptAppEngine.js`
30-
const WebSocket = require('WebSocket');
30+
// Moving to top gives errors due to NativeModules not being initialized
31+
const HMRLoadingView = require('HMRLoadingView');
3132

32-
const wsHostPort = port !== null && port !== ''
33-
? `${host}:${port}`
34-
: host;
33+
const wsHostPort = port !== null && port !== '' ? `${host}:${port}` : host;
3534

3635
bundleEntry = bundleEntry.replace(/\.(bundle|delta)/, '.js');
3736

3837
// Build the websocket url
39-
const wsUrl = `ws://${wsHostPort}/hot?` +
38+
const wsUrl =
39+
`ws://${wsHostPort}/hot?` +
4040
`platform=${platform}&` +
4141
`bundleEntry=${bundleEntry}`;
4242

43-
const activeWS = new WebSocket(wsUrl);
44-
activeWS.onerror = (e) => {
45-
let error = (
46-
`Hot loading isn't working because it cannot connect to the development server.
43+
const hmrClient = new MetroHMRClient(wsUrl);
44+
45+
hmrClient.on('connection-error', e => {
46+
let error = `Hot loading isn't working because it cannot connect to the development server.
4747
4848
Try the following to fix the issue:
49-
- Ensure that the packager server is running and available on the same network`
50-
);
49+
- Ensure that the packager server is running and available on the same network`;
5150

5251
if (Platform.OS === 'ios') {
53-
error += (
54-
`
55-
- Ensure that the Packager server URL is correctly set in AppDelegate`
56-
);
52+
error += `
53+
- Ensure that the Packager server URL is correctly set in AppDelegate`;
5754
} else {
58-
error += (
59-
`
55+
error += `
6056
- Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices
6157
- If you're on a physical device connected to the same machine, run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device
62-
- If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081`
63-
);
58+
- If your device is on the same Wi-Fi network, set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081`;
6459
}
6560

66-
error += (
67-
`
61+
error += `
6862
6963
URL: ${host}:${port}
7064
71-
Error: ${e.message}`
72-
);
65+
Error: ${e.message}`;
7366

7467
throw new Error(error);
75-
};
76-
activeWS.onmessage = ({data}) => {
77-
// Moving to top gives errors due to NativeModules not being initialized
78-
const HMRLoadingView = require('HMRLoadingView');
79-
80-
data = JSON.parse(data);
81-
82-
switch (data.type) {
83-
case 'update-start': {
84-
HMRLoadingView.showMessage('Hot Loading...');
85-
break;
86-
}
87-
case 'update': {
88-
const {
89-
modules,
90-
sourceMappingURLs,
91-
sourceURLs,
92-
} = data.body;
93-
94-
if (Platform.OS === 'ios') {
95-
const RCTRedBox = require('NativeModules').RedBox;
96-
RCTRedBox && RCTRedBox.dismiss && RCTRedBox.dismiss();
97-
} else {
98-
const RCTExceptionsManager = require('NativeModules').ExceptionsManager;
99-
RCTExceptionsManager && RCTExceptionsManager.dismissRedbox && RCTExceptionsManager.dismissRedbox();
100-
}
101-
102-
modules.forEach(({id, code}, i) => {
103-
code = code + '\n\n' + sourceMappingURLs[i];
104-
105-
// on JSC we need to inject from native for sourcemaps to work
106-
// (Safari doesn't support `sourceMappingURL` nor any variant when
107-
// evaluating code) but on Chrome we can simply use eval
108-
const injectFunction = typeof global.nativeInjectHMRUpdate === 'function'
109-
? global.nativeInjectHMRUpdate
110-
: eval; // eslint-disable-line no-eval
111-
112-
injectFunction(code, sourceURLs[i]);
113-
});
114-
115-
HMRLoadingView.hide();
116-
break;
117-
}
118-
case 'update-done': {
119-
HMRLoadingView.hide();
120-
break;
121-
}
122-
case 'error': {
123-
HMRLoadingView.hide();
124-
throw new Error(`${data.body.type}: ${data.body.message}`);
125-
}
126-
default: {
127-
throw new Error(`Unexpected message: ${data}`);
128-
}
68+
});
69+
70+
hmrClient.on('update-start', () => {
71+
HMRLoadingView.showMessage('Hot Loading...');
72+
});
73+
74+
hmrClient.on('update', () => {
75+
if (Platform.OS === 'ios') {
76+
const RCTRedBox = require('NativeModules').RedBox;
77+
RCTRedBox && RCTRedBox.dismiss && RCTRedBox.dismiss();
78+
} else {
79+
const RCTExceptionsManager = require('NativeModules').ExceptionsManager;
80+
RCTExceptionsManager &&
81+
RCTExceptionsManager.dismissRedbox &&
82+
RCTExceptionsManager.dismissRedbox();
12983
}
130-
};
84+
});
85+
86+
hmrClient.on('update-done', () => {
87+
HMRLoadingView.hide();
88+
});
89+
90+
hmrClient.on('error', data => {
91+
HMRLoadingView.hide();
92+
throw new Error(`${data.type} ${data.message}`);
93+
});
94+
95+
hmrClient.enable();
13196
},
13297
};
13398

0 commit comments

Comments
 (0)