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');
10 var MutationObserver = global.MutationObserver || global.WebKitMutationObserver;
11 var document = global.document;
12 var process = global.process;
13 var Promise = global.Promise;
14 // Node.js 11 shows ExperimentalWarning on getting `queueMicrotask`
15 var queueMicrotaskDescriptor = getOwnPropertyDescriptor(global, 'queueMicrotask');
16 var queueMicrotask = queueMicrotaskDescriptor && queueMicrotaskDescriptor.value;
18 var flush, head, last, notify, toggle, node, promise, then;
20 // modern engines have queueMicrotask method
21 if (!queueMicrotask) {
24 if (IS_NODE && (parent = process.domain)) parent.exit();
32 else last = undefined;
36 if (parent) parent.enter();
39 // browsers with MutationObserver, except iOS - https://github.com/zloirock/core-js/issues/339
40 // also except WebOS Webkit https://github.com/zloirock/core-js/issues/898
41 if (!IS_IOS && !IS_NODE && !IS_WEBOS_WEBKIT && MutationObserver && document) {
43 node = document.createTextNode('');
44 new MutationObserver(flush).observe(node, { characterData: true });
45 notify = function () {
46 node.data = toggle = !toggle;
48 // environments with maybe non-completely correct, but existent Promise
49 } else if (!IS_IOS_PEBBLE && Promise && Promise.resolve) {
50 // Promise.resolve without an argument throws an error in LG WebOS 2
51 promise = Promise.resolve(undefined);
52 // workaround of WebKit ~ iOS Safari 10.1 bug
53 promise.constructor = Promise;
54 then = bind(promise.then, promise);
55 notify = function () {
58 // Node.js without promises
60 notify = function () {
61 process.nextTick(flush);
63 // for other environments - macrotask based on:
66 // - window.postMessag
67 // - onreadystatechange
70 // strange IE + webpack dev server bug - use .bind(global)
71 macrotask = bind(macrotask, global);
72 notify = function () {
78 module.exports = queueMicrotask || function (fn) {
79 var task = { fn: fn, next: undefined };
80 if (last) last.next = task;