123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 'use strict';
- var global = require('../internals/global');
- var bind = require('../internals/function-bind-context');
- var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
- var macrotask = require('../internals/task').set;
- var Queue = require('../internals/queue');
- var IS_IOS = require('../internals/engine-is-ios');
- var IS_IOS_PEBBLE = require('../internals/engine-is-ios-pebble');
- var IS_WEBOS_WEBKIT = require('../internals/engine-is-webos-webkit');
- var IS_NODE = require('../internals/engine-is-node');
- var MutationObserver = global.MutationObserver || global.WebKitMutationObserver;
- var document = global.document;
- var process = global.process;
- var Promise = global.Promise;
- var queueMicrotaskDescriptor = getOwnPropertyDescriptor(global, 'queueMicrotask');
- var microtask = queueMicrotaskDescriptor && queueMicrotaskDescriptor.value;
- var notify, toggle, node, promise, then;
- if (!microtask) {
- var queue = new Queue();
- var flush = function () {
- var parent, fn;
- if (IS_NODE && (parent = process.domain)) parent.exit();
- while (fn = queue.get()) try {
- fn();
- } catch (error) {
- if (queue.head) notify();
- throw error;
- }
- if (parent) parent.enter();
- };
-
-
- if (!IS_IOS && !IS_NODE && !IS_WEBOS_WEBKIT && MutationObserver && document) {
- toggle = true;
- node = document.createTextNode('');
- new MutationObserver(flush).observe(node, { characterData: true });
- notify = function () {
- node.data = toggle = !toggle;
- };
-
- } else if (!IS_IOS_PEBBLE && Promise && Promise.resolve) {
-
- promise = Promise.resolve(undefined);
-
- promise.constructor = Promise;
- then = bind(promise.then, promise);
- notify = function () {
- then(flush);
- };
-
- } else if (IS_NODE) {
- notify = function () {
- process.nextTick(flush);
- };
-
-
-
-
-
-
- } else {
-
- macrotask = bind(macrotask, global);
- notify = function () {
- macrotask(flush);
- };
- }
- microtask = function (fn) {
- if (!queue.head) notify();
- queue.add(fn);
- };
- }
- module.exports = microtask;
|