2015-12-29 00:43:08 +00:00
/ * *
2018-09-11 22:27:47 +00:00
* Copyright ( c ) Facebook , Inc . and its affiliates .
2015-12-29 00:43:08 +00:00
*
2018-02-17 02:24:55 +00:00
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree .
2015-12-29 00:43:08 +00:00
*
2018-01-22 18:06:49 +00:00
* @ format
2016-02-27 23:57:14 +00:00
* @ flow
2015-12-29 00:43:08 +00:00
* /
'use strict' ;
2016-02-12 16:09:43 +00:00
const Platform = require ( 'Platform' ) ;
2018-12-03 07:49:12 +00:00
const invariant = require ( 'invariant' ) ;
2015-12-30 02:24:06 +00:00
2018-01-22 18:06:49 +00:00
const MetroHMRClient = require ( 'metro/src/lib/bundle-modules/HMRClient' ) ;
2015-12-29 00:43:08 +00:00
/ * *
* HMR Client that receives from the server HMR updates and propagates them
* runtime to reflects those changes .
* /
const HMRClient = {
2016-02-27 23:57:14 +00:00
enable ( platform : string , bundleEntry : string , host : string , port : number ) {
2015-12-30 02:24:06 +00:00
invariant ( platform , 'Missing required parameter `platform`' ) ;
invariant ( bundleEntry , 'Missing required paramenter `bundleEntry`' ) ;
2016-02-12 16:09:43 +00:00
invariant ( host , 'Missing required paramenter `host`' ) ;
2016-01-04 17:55:09 +00:00
2018-01-22 18:06:49 +00:00
// Moving to top gives errors due to NativeModules not being initialized
const HMRLoadingView = require ( 'HMRLoadingView' ) ;
2015-12-29 00:43:08 +00:00
2018-10-26 01:14:12 +00:00
/ * $ F l o w F i x M e ( > = 0 . 8 4 . 0 s i t e = r e a c t _ n a t i v e _ f b ) T h i s c o m m e n t s u p p r e s s e s a n
* error found when Flow v0 . 84 was deployed . To see the error , delete this
* comment and run Flow . * /
2018-01-22 18:06:49 +00:00
const wsHostPort = port !== null && port !== '' ? ` ${ host } : ${ port } ` : host ;
2016-02-12 16:09:43 +00:00
2017-11-17 15:30:13 +00:00
bundleEntry = bundleEntry . replace ( /\.(bundle|delta)/ , '.js' ) ;
2016-02-12 16:09:43 +00:00
// Build the websocket url
2018-01-22 18:06:49 +00:00
const wsUrl =
` ws:// ${ wsHostPort } /hot? ` +
2016-02-12 16:09:43 +00:00
` platform= ${ platform } & ` +
2017-11-17 15:30:13 +00:00
` bundleEntry= ${ bundleEntry } ` ;
2016-02-12 16:09:43 +00:00
2018-01-22 18:06:49 +00:00
const hmrClient = new MetroHMRClient ( wsUrl ) ;
hmrClient . on ( 'connection-error' , e => {
let error = ` Hot loading isn't working because it cannot connect to the development server.
2016-01-04 17:55:09 +00:00
2016-03-21 21:07:09 +00:00
Try the following to fix the issue :
2018-01-22 18:06:49 +00:00
- Ensure that the packager server is running and available on the same network ` ;
2016-03-21 21:07:09 +00:00
if ( Platform . OS === 'ios' ) {
2018-01-22 18:06:49 +00:00
error += `
- Ensure that the Packager server URL is correctly set in AppDelegate ` ;
2016-03-21 21:07:09 +00:00
} else {
2018-01-22 18:06:49 +00:00
error += `
2016-03-21 21:07:09 +00:00
- 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
- 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
2018-01-22 18:06:49 +00:00
- 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 ` ;
2016-03-21 21:07:09 +00:00
}
2018-01-22 18:06:49 +00:00
error += `
2016-01-04 17:55:09 +00:00
URL : $ { host } : $ { port }
2018-01-22 18:06:49 +00:00
Error : $ { e . message } ` ;
2016-03-21 21:07:09 +00:00
throw new Error ( error ) ;
2018-01-22 18:06:49 +00:00
} ) ;
hmrClient . on ( 'update-start' , ( ) => {
HMRLoadingView . showMessage ( 'Hot Loading...' ) ;
} ) ;
hmrClient . on ( 'update' , ( ) => {
if ( Platform . OS === 'ios' ) {
const RCTRedBox = require ( 'NativeModules' ) . RedBox ;
RCTRedBox && RCTRedBox . dismiss && RCTRedBox . dismiss ( ) ;
} else {
const RCTExceptionsManager = require ( 'NativeModules' ) . ExceptionsManager ;
RCTExceptionsManager &&
RCTExceptionsManager . dismissRedbox &&
RCTExceptionsManager . dismissRedbox ( ) ;
2016-02-01 20:41:04 +00:00
}
2018-01-22 18:06:49 +00:00
} ) ;
hmrClient . on ( 'update-done' , ( ) => {
HMRLoadingView . hide ( ) ;
} ) ;
hmrClient . on ( 'error' , data => {
HMRLoadingView . hide ( ) ;
2018-11-12 16:47:02 +00:00
if ( data . type === 'GraphNotFoundError' ) {
hmrClient . disable ( ) ;
throw new Error (
'The packager server has restarted since the last Hot update. Hot Reloading will be disabled until you reload the application.' ,
) ;
} else if ( data . type === 'RevisionNotFoundError' ) {
hmrClient . disable ( ) ;
throw new Error (
'The packager server and the client are out of sync. Hot Reloading will be disabled until you reload the application.' ,
) ;
} else {
throw new Error ( ` ${ data . type } ${ data . message } ` ) ;
}
} ) ;
hmrClient . on ( 'close' , data => {
HMRLoadingView . hide ( ) ;
throw new Error (
'Disconnected from the packager server. Hot Reloading will be disabled until you reload the application.' ,
) ;
2018-01-22 18:06:49 +00:00
} ) ;
hmrClient . enable ( ) ;
2015-12-29 00:43:08 +00:00
} ,
} ;
module . exports = HMRClient ;