poll.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. /* globals __resourceQuery */
  6. if (module.hot) {
  7. // eslint-disable-next-line no-implicit-coercion
  8. var hotPollInterval = +__resourceQuery.slice(1) || 10 * 60 * 1000;
  9. var log = require("./log");
  10. /**
  11. * @param {boolean=} fromUpdate true when called from update
  12. */
  13. var checkForUpdate = function checkForUpdate(fromUpdate) {
  14. if (module.hot.status() === "idle") {
  15. module.hot
  16. .check(true)
  17. .then(function (updatedModules) {
  18. if (!updatedModules) {
  19. if (fromUpdate) log("info", "[HMR] Update applied.");
  20. return;
  21. }
  22. require("./log-apply-result")(updatedModules, updatedModules);
  23. checkForUpdate(true);
  24. })
  25. .catch(function (err) {
  26. var status = module.hot.status();
  27. if (["abort", "fail"].indexOf(status) >= 0) {
  28. log("warning", "[HMR] Cannot apply update.");
  29. log("warning", "[HMR] " + log.formatError(err));
  30. log("warning", "[HMR] You need to restart the application!");
  31. } else {
  32. log("warning", "[HMR] Update failed: " + log.formatError(err));
  33. }
  34. });
  35. }
  36. };
  37. setInterval(checkForUpdate, hotPollInterval);
  38. } else {
  39. throw new Error("[HMR] Hot Module Replacement is disabled.");
  40. }