poll.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. var hotPollInterval = +__resourceQuery.slice(1) || 10 * 60 * 1000;
  8. var log = require("./log");
  9. /**
  10. * @param {boolean=} fromUpdate true when called from update
  11. */
  12. var checkForUpdate = function checkForUpdate(fromUpdate) {
  13. if (module.hot.status() === "idle") {
  14. module.hot
  15. .check(true)
  16. .then(function (updatedModules) {
  17. if (!updatedModules) {
  18. if (fromUpdate) log("info", "[HMR] Update applied.");
  19. return;
  20. }
  21. require("./log-apply-result")(updatedModules, updatedModules);
  22. checkForUpdate(true);
  23. })
  24. .catch(function (err) {
  25. var status = module.hot.status();
  26. if (["abort", "fail"].indexOf(status) >= 0) {
  27. log("warning", "[HMR] Cannot apply update.");
  28. log("warning", "[HMR] " + log.formatError(err));
  29. log("warning", "[HMR] You need to restart the application!");
  30. } else {
  31. log("warning", "[HMR] Update failed: " + log.formatError(err));
  32. }
  33. });
  34. }
  35. };
  36. setInterval(checkForUpdate, hotPollInterval);
  37. } else {
  38. throw new Error("[HMR] Hot Module Replacement is disabled.");
  39. }