From 28b3bbefafdfab6f7910752ca339a86b5605308a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Bigio?= Date: Thu, 7 Jan 2016 10:39:13 -0800 Subject: [PATCH] Fixes Hot Loading re-loading bug Summary: public Fixes a terrible bug due to which when Hot Loading enabled when the user reloads we'll serve them the first `hot` bundle he requested. This happened because when HMR enabled we bailed out after sending the HMR updates and didn't rebuild any of the bundles the user requested before. As a consequence, when they reload we'd sent him the first and only one we ever built. The fix is to tweak the hmr listener to return a promise. This way we can run the remaining code on the file change listener just after the HMR stuff finishes. We need to do it this way to avoid the remaining stuff to compete for CPU with the HMR one and give the best possible experience when HMR is enabled. Reviewed By: davidaurelio Differential Revision: D2811382 fb-gh-sync-id: 906932d71f35467485cf8a865a8d59f4d2ff41a0 --- react-packager/src/Server/index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index a8d363e7..f5d284f2 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -180,16 +180,23 @@ class Server { this._fileWatcher.on('all', this._onFileChange.bind(this)); this._debouncedFileChangeHandler = _.debounce(filePath => { + const onFileChange = () => { + this._rebuildBundles(filePath); + this._informChangeWatchers(); + }; + // if Hot Loading is enabled avoid rebuilding bundles and sending live // updates. Instead, send the HMR updates right away and once that // finishes, invoke any other file change listener. if (this._hmrFileChangeListener) { - this._hmrFileChangeListener(filePath, this._bundler.stat(filePath)); + this._hmrFileChangeListener( + filePath, + this._bundler.stat(filePath), + ).then(onFileChange).done(); return; } - this._rebuildBundles(filePath); - this._informChangeWatchers(); + onFileChange(); }, 50); }