schedule.js (2148B)
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 36 var scheduleToggle = function() { 37 if (toggleScheduled) return; 38 toggleScheduled = true; 39 div2.classList.toggle("foo"); 40 }; 41 42 return function schedule(fn) { 43 var o = new MutationObserver(function() { 44 o.disconnect(); 45 fn(); 46 }); 47 o.observe(div, opts); 48 scheduleToggle(); 49 }; 50 })(); 51 } else if (typeof setImmediate !== "undefined") { 52 schedule = function (fn) { 53 setImmediate(fn); 54 }; 55 } else if (typeof setTimeout !== "undefined") { 56 schedule = function (fn) { 57 setTimeout(fn, 0); 58 }; 59 } else { 60 schedule = noAsyncScheduler; 61 } 62 module.exports = schedule;