TrinetraLabs/Placebo_AI
0
1'use strict';2var Mutation = global.MutationObserver || global.WebKitMutationObserver;3 4var scheduleDrain;5 6if (process.browser) {7 if (Mutation) {8 var called = 0;9 var observer = new Mutation(nextTick);10 var element = global.document.createTextNode('');11 observer.observe(element, {12 characterData: true13 });14 scheduleDrain = function () {15 element.data = (called = ++called % 2);16 };17 } else if (!global.setImmediate && typeof global.MessageChannel !== 'undefined') {18 var channel = new global.MessageChannel();19 channel.port1.onmessage = nextTick;20 scheduleDrain = function () {21 channel.port2.postMessage(0);22 };23 } else if ('document' in global && 'onreadystatechange' in global.document.createElement('script')) {24 scheduleDrain = function () {25 26 // Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted27 // into the document. Do so, thus queuing up the task. Remember to clean up once it's been called.28 var scriptEl = global.document.createElement('script');29 scriptEl.onreadystatechange = function () {30 nextTick();31 32 scriptEl.onreadystatechange = null;33 scriptEl.parentNode.removeChild(scriptEl);34 scriptEl = null;35 };36 global.document.documentElement.appendChild(scriptEl);37 };38 } else {39 scheduleDrain = function () {40 setTimeout(nextTick, 0);41 };42 }43} else {44 scheduleDrain = function () {45 process.nextTick(nextTick);46 };47}48 49var draining;50var queue = [];51//named nextTick for less confusing stack traces52function nextTick() {53 draining = true;54 var i, oldQueue;55 var len = queue.length;56 while (len) {57 oldQueue = queue;58 queue = [];59 i = -1;60 while (++i < len) {61 oldQueue[i]();62 }63 len = queue.length;64 }65 draining = false;66}67 68module.exports = immediate;69function immediate(task) {70 if (queue.push(task) === 1 && !draining) {71 scheduleDrain();72 }73}74 