You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.1 KiB

4 years ago
  1. "use strict";
  2. var util = require("./util");
  3. var schedule;
  4. var noAsyncScheduler = function() {
  5. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  6. };
  7. var NativePromise = util.getNativePromise();
  8. if (util.isNode && typeof MutationObserver === "undefined") {
  9. var GlobalSetImmediate = global.setImmediate;
  10. var ProcessNextTick = process.nextTick;
  11. schedule = util.isRecentNode
  12. ? function(fn) { GlobalSetImmediate.call(global, fn); }
  13. : function(fn) { ProcessNextTick.call(process, fn); };
  14. } else if (typeof NativePromise === "function" &&
  15. typeof NativePromise.resolve === "function") {
  16. var nativePromise = NativePromise.resolve();
  17. schedule = function(fn) {
  18. nativePromise.then(fn);
  19. };
  20. } else if ((typeof MutationObserver !== "undefined") &&
  21. !(typeof window !== "undefined" &&
  22. window.navigator &&
  23. (window.navigator.standalone || window.cordova)) &&
  24. ("classList" in document.documentElement)) {
  25. schedule = (function() {
  26. var div = document.createElement("div");
  27. var opts = {attributes: true};
  28. var toggleScheduled = false;
  29. var div2 = document.createElement("div");
  30. var o2 = new MutationObserver(function() {
  31. div.classList.toggle("foo");
  32. toggleScheduled = false;
  33. });
  34. o2.observe(div2, opts);
  35. var scheduleToggle = function() {
  36. if (toggleScheduled) return;
  37. toggleScheduled = true;
  38. div2.classList.toggle("foo");
  39. };
  40. return function schedule(fn) {
  41. var o = new MutationObserver(function() {
  42. o.disconnect();
  43. fn();
  44. });
  45. o.observe(div, opts);
  46. scheduleToggle();
  47. };
  48. })();
  49. } else if (typeof setImmediate !== "undefined") {
  50. schedule = function (fn) {
  51. setImmediate(fn);
  52. };
  53. } else if (typeof setTimeout !== "undefined") {
  54. schedule = function (fn) {
  55. setTimeout(fn, 0);
  56. };
  57. } else {
  58. schedule = noAsyncScheduler;
  59. }
  60. module.exports = schedule;