microtask.js 2.7 KB

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