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 |
|
---|
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;
|
---|
15 |
|
---|
16 | var flush, head, last, notify, toggle, node, promise, then;
|
---|
17 |
|
---|
18 | // modern engines have queueMicrotask method
|
---|
19 | if (!queueMicrotask) {
|
---|
20 | flush = function () {
|
---|
21 | var parent, fn;
|
---|
22 | if (IS_NODE && (parent = process.domain)) parent.exit();
|
---|
23 | while (head) {
|
---|
24 | fn = head.fn;
|
---|
25 | head = head.next;
|
---|
26 | try {
|
---|
27 | fn();
|
---|
28 | } catch (error) {
|
---|
29 | if (head) notify();
|
---|
30 | else last = undefined;
|
---|
31 | throw error;
|
---|
32 | }
|
---|
33 | } last = undefined;
|
---|
34 | if (parent) parent.enter();
|
---|
35 | };
|
---|
36 |
|
---|
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) {
|
---|
40 | toggle = true;
|
---|
41 | node = document.createTextNode('');
|
---|
42 | new MutationObserver(flush).observe(node, { characterData: true });
|
---|
43 | notify = function () {
|
---|
44 | node.data = toggle = !toggle;
|
---|
45 | };
|
---|
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);
|
---|
50 | // workaround of WebKit ~ iOS Safari 10.1 bug
|
---|
51 | promise.constructor = Promise;
|
---|
52 | then = promise.then;
|
---|
53 | notify = function () {
|
---|
54 | then.call(promise, flush);
|
---|
55 | };
|
---|
56 | // Node.js without promises
|
---|
57 | } else if (IS_NODE) {
|
---|
58 | notify = function () {
|
---|
59 | process.nextTick(flush);
|
---|
60 | };
|
---|
61 | // for other environments - macrotask based on:
|
---|
62 | // - setImmediate
|
---|
63 | // - MessageChannel
|
---|
64 | // - window.postMessag
|
---|
65 | // - onreadystatechange
|
---|
66 | // - setTimeout
|
---|
67 | } else {
|
---|
68 | notify = function () {
|
---|
69 | // strange IE + webpack dev server bug - use .call(global)
|
---|
70 | macrotask.call(global, flush);
|
---|
71 | };
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | module.exports = queueMicrotask || function (fn) {
|
---|
76 | var task = { fn: fn, next: undefined };
|
---|
77 | if (last) last.next = task;
|
---|
78 | if (!head) {
|
---|
79 | head = task;
|
---|
80 | notify();
|
---|
81 | } last = task;
|
---|
82 | };
|
---|