react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / bluebird / js / main / async.js
81160 views"use strict";1var firstLineError;2try {throw new Error(); } catch (e) {firstLineError = e;}3var schedule = require("./schedule.js");4var Queue = require("./queue.js");5var util = require("./util.js");67function Async() {8this._isTickUsed = false;9this._lateQueue = new Queue(16);10this._normalQueue = new Queue(16);11this._trampolineEnabled = true;12var self = this;13this.drainQueues = function () {14self._drainQueues();15};16this._schedule =17schedule.isStatic ? schedule(this.drainQueues) : schedule;18}1920Async.prototype.disableTrampolineIfNecessary = function() {21if (util.hasDevTools) {22this._trampolineEnabled = false;23}24};2526Async.prototype.enableTrampoline = function() {27if (!this._trampolineEnabled) {28this._trampolineEnabled = true;29this._schedule = function(fn) {30setTimeout(fn, 0);31};32}33};3435Async.prototype.haveItemsQueued = function () {36return this._normalQueue.length() > 0;37};3839Async.prototype.throwLater = function(fn, arg) {40if (arguments.length === 1) {41arg = fn;42fn = function () { throw arg; };43}44var domain = this._getDomain();45if (domain !== undefined) fn = domain.bind(fn);46if (typeof setTimeout !== "undefined") {47setTimeout(function() {48fn(arg);49}, 0);50} else try {51this._schedule(function() {52fn(arg);53});54} catch (e) {55throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");56}57};5859Async.prototype._getDomain = function() {};6061if (!false) {62if (util.isNode) {63var EventsModule = require("events");6465var domainGetter = function() {66var domain = process.domain;67if (domain === null) return undefined;68return domain;69};7071if (EventsModule.usingDomains) {72Async.prototype._getDomain = domainGetter;73} else {74var descriptor =75Object.getOwnPropertyDescriptor(EventsModule, "usingDomains");7677if (descriptor) {78if (!descriptor.configurable) {79process.on("domainsActivated", function() {80Async.prototype._getDomain = domainGetter;81});82} else {83var usingDomains = false;84Object.defineProperty(EventsModule, "usingDomains", {85configurable: false,86enumerable: true,87get: function() {88return usingDomains;89},90set: function(value) {91if (usingDomains || !value) return;92usingDomains = true;93Async.prototype._getDomain = domainGetter;94util.toFastProperties(process);95process.emit("domainsActivated");96}97});98}99}100}101}102}103104function AsyncInvokeLater(fn, receiver, arg) {105var domain = this._getDomain();106if (domain !== undefined) fn = domain.bind(fn);107this._lateQueue.push(fn, receiver, arg);108this._queueTick();109}110111function AsyncInvoke(fn, receiver, arg) {112var domain = this._getDomain();113if (domain !== undefined) fn = domain.bind(fn);114this._normalQueue.push(fn, receiver, arg);115this._queueTick();116}117118function AsyncSettlePromises(promise) {119var domain = this._getDomain();120if (domain !== undefined) {121var fn = domain.bind(promise._settlePromises);122this._normalQueue.push(fn, promise, undefined);123} else {124this._normalQueue._pushOne(promise);125}126this._queueTick();127}128129if (!util.hasDevTools) {130Async.prototype.invokeLater = AsyncInvokeLater;131Async.prototype.invoke = AsyncInvoke;132Async.prototype.settlePromises = AsyncSettlePromises;133} else {134Async.prototype.invokeLater = function (fn, receiver, arg) {135if (this._trampolineEnabled) {136AsyncInvokeLater.call(this, fn, receiver, arg);137} else {138setTimeout(function() {139fn.call(receiver, arg);140}, 100);141}142};143144Async.prototype.invoke = function (fn, receiver, arg) {145if (this._trampolineEnabled) {146AsyncInvoke.call(this, fn, receiver, arg);147} else {148setTimeout(function() {149fn.call(receiver, arg);150}, 0);151}152};153154Async.prototype.settlePromises = function(promise) {155if (this._trampolineEnabled) {156AsyncSettlePromises.call(this, promise);157} else {158setTimeout(function() {159promise._settlePromises();160}, 0);161}162};163}164165Async.prototype.invokeFirst = function (fn, receiver, arg) {166var domain = this._getDomain();167if (domain !== undefined) fn = domain.bind(fn);168this._normalQueue.unshift(fn, receiver, arg);169this._queueTick();170};171172Async.prototype._drainQueue = function(queue) {173while (queue.length() > 0) {174var fn = queue.shift();175if (typeof fn !== "function") {176fn._settlePromises();177continue;178}179var receiver = queue.shift();180var arg = queue.shift();181fn.call(receiver, arg);182}183};184185Async.prototype._drainQueues = function () {186this._drainQueue(this._normalQueue);187this._reset();188this._drainQueue(this._lateQueue);189};190191Async.prototype._queueTick = function () {192if (!this._isTickUsed) {193this._isTickUsed = true;194this._schedule(this.drainQueues);195}196};197198Async.prototype._reset = function () {199this._isTickUsed = false;200};201202module.exports = new Async();203module.exports.firstLineError = firstLineError;204205206