schedule.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. var schedule;
  3. var noAsyncScheduler = function() {
  4. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");
  5. };
  6. if (require("./util.js").isNode) {
  7. var version = process.versions.node.split(".").map(Number);
  8. schedule = (version[0] === 0 && version[1] > 10) || (version[0] > 0)
  9. ? function(fn) { global.setImmediate(fn); } : process.nextTick;
  10. if (!schedule) {
  11. if (typeof setImmediate !== "undefined") {
  12. schedule = setImmediate;
  13. } else if (typeof setTimeout !== "undefined") {
  14. schedule = setTimeout;
  15. } else {
  16. schedule = noAsyncScheduler;
  17. }
  18. }
  19. } else if (typeof MutationObserver !== "undefined") {
  20. schedule = function(fn) {
  21. var div = document.createElement("div");
  22. var observer = new MutationObserver(fn);
  23. observer.observe(div, {attributes: true});
  24. return function() { div.classList.toggle("foo"); };
  25. };
  26. schedule.isStatic = true;
  27. } else if (typeof setImmediate !== "undefined") {
  28. schedule = function (fn) {
  29. setImmediate(fn);
  30. };
  31. } else if (typeof setTimeout !== "undefined") {
  32. schedule = function (fn) {
  33. setTimeout(fn, 0);
  34. };
  35. } else {
  36. schedule = noAsyncScheduler;
  37. }
  38. module.exports = schedule;