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');
8 var MutationObserver = global.MutationObserver || global.WebKitMutationObserver;
9 var document = global.document;
10 var process = global.process;
11 var Promise = global.Promise;
12 // Node.js 11 shows ExperimentalWarning on getting `queueMicrotask`
13 var queueMicrotaskDescriptor = getOwnPropertyDescriptor(global, 'queueMicrotask');
14 var queueMicrotask = queueMicrotaskDescriptor && queueMicrotaskDescriptor.value;
16 var flush, head, last, notify, toggle, node, promise, then;
18 // modern engines have queueMicrotask method
19 if (!queueMicrotask) {
22 if (IS_NODE && (parent = process.domain)) parent.exit();
30 else last = undefined;
34 if (parent) parent.enter();
37 // browsers with MutationObserver, except iOS - https://github.com/zloirock/core-js/issues/339
38 // also except WebOS Webkit https://github.com/zloirock/core-js/issues/898
39 if (!IS_IOS && !IS_NODE && !IS_WEBOS_WEBKIT && MutationObserver && document) {
41 node = document.createTextNode('');
42 new MutationObserver(flush).observe(node, { characterData: true });
43 notify = function () {
44 node.data = toggle = !toggle;
46 // environments with maybe non-completely correct, but existent Promise
47 } else if (Promise && Promise.resolve) {
48 // Promise.resolve without an argument throws an error in LG WebOS 2
49 promise = Promise.resolve(undefined);
51 notify = function () {
52 then.call(promise, flush);
54 // Node.js without promises
56 notify = function () {
57 process.nextTick(flush);
59 // for other environments - macrotask based on:
62 // - window.postMessag
63 // - onreadystatechange
66 notify = function () {
67 // strange IE + webpack dev server bug - use .call(global)
68 macrotask.call(global, flush);
73 module.exports = queueMicrotask || function (fn) {
74 var task = { fn: fn, next: undefined };
75 if (last) last.next = task;