CoolFace
Apppublic

TrinetraLabs/Placebo_AI

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
browser.js70 linesDownload Raw Back to lib
1'use strict';2var Mutation = global.MutationObserver || global.WebKitMutationObserver;3 4var scheduleDrain;5 6{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}44 45var draining;46var queue = [];47//named nextTick for less confusing stack traces48function nextTick() {49  draining = true;50  var i, oldQueue;51  var len = queue.length;52  while (len) {53    oldQueue = queue;54    queue = [];55    i = -1;56    while (++i < len) {57      oldQueue[i]();58    }59    len = queue.length;60  }61  draining = false;62}63 64module.exports = immediate;65function immediate(task) {66  if (queue.push(task) === 1 && !draining) {67    scheduleDrain();68  }69}70