Node-Red configuration
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.

microtask.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. var globalThis = require('../internals/global-this');
  3. var safeGetBuiltIn = require('../internals/safe-get-built-in');
  4. var bind = require('../internals/function-bind-context');
  5. var macrotask = require('../internals/task').set;
  6. var Queue = require('../internals/queue');
  7. var IS_IOS = require('../internals/environment-is-ios');
  8. var IS_IOS_PEBBLE = require('../internals/environment-is-ios-pebble');
  9. var IS_WEBOS_WEBKIT = require('../internals/environment-is-webos-webkit');
  10. var IS_NODE = require('../internals/environment-is-node');
  11. var MutationObserver = globalThis.MutationObserver || globalThis.WebKitMutationObserver;
  12. var document = globalThis.document;
  13. var process = globalThis.process;
  14. var Promise = globalThis.Promise;
  15. var microtask = safeGetBuiltIn('queueMicrotask');
  16. var notify, toggle, node, promise, then;
  17. // modern engines have queueMicrotask method
  18. if (!microtask) {
  19. var queue = new Queue();
  20. var flush = function () {
  21. var parent, fn;
  22. if (IS_NODE && (parent = process.domain)) parent.exit();
  23. while (fn = queue.get()) try {
  24. fn();
  25. } catch (error) {
  26. if (queue.head) notify();
  27. throw error;
  28. }
  29. if (parent) parent.enter();
  30. };
  31. // browsers with MutationObserver, except iOS - https://github.com/zloirock/core-js/issues/339
  32. // also except WebOS Webkit https://github.com/zloirock/core-js/issues/898
  33. if (!IS_IOS && !IS_NODE && !IS_WEBOS_WEBKIT && MutationObserver && document) {
  34. toggle = true;
  35. node = document.createTextNode('');
  36. new MutationObserver(flush).observe(node, { characterData: true });
  37. notify = function () {
  38. node.data = toggle = !toggle;
  39. };
  40. // environments with maybe non-completely correct, but existent Promise
  41. } else if (!IS_IOS_PEBBLE && Promise && Promise.resolve) {
  42. // Promise.resolve without an argument throws an error in LG WebOS 2
  43. promise = Promise.resolve(undefined);
  44. // workaround of WebKit ~ iOS Safari 10.1 bug
  45. promise.constructor = Promise;
  46. then = bind(promise.then, promise);
  47. notify = function () {
  48. then(flush);
  49. };
  50. // Node.js without promises
  51. } else if (IS_NODE) {
  52. notify = function () {
  53. process.nextTick(flush);
  54. };
  55. // for other environments - macrotask based on:
  56. // - setImmediate
  57. // - MessageChannel
  58. // - window.postMessage
  59. // - onreadystatechange
  60. // - setTimeout
  61. } else {
  62. // `webpack` dev server bug on IE global methods - use bind(fn, global)
  63. macrotask = bind(macrotask, globalThis);
  64. notify = function () {
  65. macrotask(flush);
  66. };
  67. }
  68. microtask = function (fn) {
  69. if (!queue.head) notify();
  70. queue.add(fn);
  71. };
  72. }
  73. module.exports = microtask;