reloadApp.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. /* global WorkerGlobalScope self */
  3. var _require = require('./log'),
  4. log = _require.log;
  5. function reloadApp(_ref, _ref2) {
  6. var hotReload = _ref.hotReload,
  7. hot = _ref.hot,
  8. liveReload = _ref.liveReload;
  9. var isUnloading = _ref2.isUnloading,
  10. currentHash = _ref2.currentHash;
  11. if (isUnloading || !hotReload) {
  12. return;
  13. }
  14. if (hot) {
  15. log.info('[WDS] App hot update...');
  16. var hotEmitter = require('webpack/hot/emitter');
  17. hotEmitter.emit('webpackHotUpdate', currentHash);
  18. if (typeof self !== 'undefined' && self.window) {
  19. // broadcast update to window
  20. self.postMessage("webpackHotUpdate".concat(currentHash), '*');
  21. }
  22. } // allow refreshing the page only if liveReload isn't disabled
  23. else if (liveReload) {
  24. var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
  25. var intervalId = self.setInterval(function () {
  26. if (rootWindow.location.protocol !== 'about:') {
  27. // reload immediately if protocol is valid
  28. applyReload(rootWindow, intervalId);
  29. } else {
  30. rootWindow = rootWindow.parent;
  31. if (rootWindow.parent === rootWindow) {
  32. // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
  33. applyReload(rootWindow, intervalId);
  34. }
  35. }
  36. });
  37. }
  38. function applyReload(rootWindow, intervalId) {
  39. clearInterval(intervalId);
  40. log.info('[WDS] App updated. Reloading...');
  41. rootWindow.location.reload();
  42. }
  43. }
  44. module.exports = reloadApp;