microtask.js 2.9 KB

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