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_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;
15 var flush, head, last, notify, toggle, node, promise, then;
17 // modern engines have queueMicrotask method
18 if (!queueMicrotask) {
21 if (IS_NODE && (parent = process.domain)) parent.exit();
29 else last = undefined;
33 if (parent) parent.enter();
36 // browsers with MutationObserver, except iOS - https://github.com/zloirock/core-js/issues/339
37 if (!IS_IOS && !IS_NODE && MutationObserver && document) {
39 node = document.createTextNode('');
40 new MutationObserver(flush).observe(node, { characterData: true });
41 notify = function () {
42 node.data = toggle = !toggle;
44 // environments with maybe non-completely correct, but existent Promise
45 } else if (Promise && Promise.resolve) {
46 // Promise.resolve without an argument throws an error in LG WebOS 2
47 promise = Promise.resolve(undefined);
49 notify = function () {
50 then.call(promise, flush);
52 // Node.js without promises
54 notify = function () {
55 process.nextTick(flush);
57 // for other environments - macrotask based on:
60 // - window.postMessag
61 // - onreadystatechange
64 notify = function () {
65 // strange IE + webpack dev server bug - use .call(global)
66 macrotask.call(global, flush);
71 module.exports = queueMicrotask || function (fn) {
72 var task = { fn: fn, next: undefined };
73 if (last) last.next = task;