Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
var firstLineError;
3
try {throw new Error(); } catch (e) {firstLineError = e;}
4
var schedule = require("./schedule.js");
5
var Queue = require("./queue.js");
6
var util = require("./util.js");
7
8
function Async() {
9
this._isTickUsed = false;
10
this._lateQueue = new Queue(16);
11
this._normalQueue = new Queue(16);
12
this._trampolineEnabled = true;
13
var self = this;
14
this.drainQueues = function () {
15
self._drainQueues();
16
};
17
this._schedule =
18
schedule.isStatic ? schedule(this.drainQueues) : schedule;
19
}
20
21
Async.prototype.disableTrampolineIfNecessary = function() {
22
if (util.hasDevTools) {
23
this._trampolineEnabled = false;
24
}
25
};
26
27
Async.prototype.enableTrampoline = function() {
28
if (!this._trampolineEnabled) {
29
this._trampolineEnabled = true;
30
this._schedule = function(fn) {
31
setTimeout(fn, 0);
32
};
33
}
34
};
35
36
Async.prototype.haveItemsQueued = function () {
37
return this._normalQueue.length() > 0;
38
};
39
40
Async.prototype.throwLater = function(fn, arg) {
41
if (arguments.length === 1) {
42
arg = fn;
43
fn = function () { throw arg; };
44
}
45
var domain = this._getDomain();
46
if (domain !== undefined) fn = domain.bind(fn);
47
if (typeof setTimeout !== "undefined") {
48
setTimeout(function() {
49
fn(arg);
50
}, 0);
51
} else try {
52
this._schedule(function() {
53
fn(arg);
54
});
55
} catch (e) {
56
throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");
57
}
58
};
59
60
Async.prototype._getDomain = function() {};
61
62
if (!false) {
63
if (util.isNode) {
64
var EventsModule = require("events");
65
66
var domainGetter = function() {
67
var domain = process.domain;
68
if (domain === null) return undefined;
69
return domain;
70
};
71
72
if (EventsModule.usingDomains) {
73
Async.prototype._getDomain = domainGetter;
74
} else {
75
var descriptor =
76
Object.getOwnPropertyDescriptor(EventsModule, "usingDomains");
77
78
if (descriptor) {
79
if (!descriptor.configurable) {
80
process.on("domainsActivated", function() {
81
Async.prototype._getDomain = domainGetter;
82
});
83
} else {
84
var usingDomains = false;
85
Object.defineProperty(EventsModule, "usingDomains", {
86
configurable: false,
87
enumerable: true,
88
get: function() {
89
return usingDomains;
90
},
91
set: function(value) {
92
if (usingDomains || !value) return;
93
usingDomains = true;
94
Async.prototype._getDomain = domainGetter;
95
util.toFastProperties(process);
96
process.emit("domainsActivated");
97
}
98
});
99
}
100
}
101
}
102
}
103
}
104
105
function AsyncInvokeLater(fn, receiver, arg) {
106
var domain = this._getDomain();
107
if (domain !== undefined) fn = domain.bind(fn);
108
this._lateQueue.push(fn, receiver, arg);
109
this._queueTick();
110
}
111
112
function AsyncInvoke(fn, receiver, arg) {
113
var domain = this._getDomain();
114
if (domain !== undefined) fn = domain.bind(fn);
115
this._normalQueue.push(fn, receiver, arg);
116
this._queueTick();
117
}
118
119
function AsyncSettlePromises(promise) {
120
var domain = this._getDomain();
121
if (domain !== undefined) {
122
var fn = domain.bind(promise._settlePromises);
123
this._normalQueue.push(fn, promise, undefined);
124
} else {
125
this._normalQueue._pushOne(promise);
126
}
127
this._queueTick();
128
}
129
130
if (!util.hasDevTools) {
131
Async.prototype.invokeLater = AsyncInvokeLater;
132
Async.prototype.invoke = AsyncInvoke;
133
Async.prototype.settlePromises = AsyncSettlePromises;
134
} else {
135
Async.prototype.invokeLater = function (fn, receiver, arg) {
136
if (this._trampolineEnabled) {
137
AsyncInvokeLater.call(this, fn, receiver, arg);
138
} else {
139
setTimeout(function() {
140
fn.call(receiver, arg);
141
}, 100);
142
}
143
};
144
145
Async.prototype.invoke = function (fn, receiver, arg) {
146
if (this._trampolineEnabled) {
147
AsyncInvoke.call(this, fn, receiver, arg);
148
} else {
149
setTimeout(function() {
150
fn.call(receiver, arg);
151
}, 0);
152
}
153
};
154
155
Async.prototype.settlePromises = function(promise) {
156
if (this._trampolineEnabled) {
157
AsyncSettlePromises.call(this, promise);
158
} else {
159
setTimeout(function() {
160
promise._settlePromises();
161
}, 0);
162
}
163
};
164
}
165
166
Async.prototype.invokeFirst = function (fn, receiver, arg) {
167
var domain = this._getDomain();
168
if (domain !== undefined) fn = domain.bind(fn);
169
this._normalQueue.unshift(fn, receiver, arg);
170
this._queueTick();
171
};
172
173
Async.prototype._drainQueue = function(queue) {
174
while (queue.length() > 0) {
175
var fn = queue.shift();
176
if (typeof fn !== "function") {
177
fn._settlePromises();
178
continue;
179
}
180
var receiver = queue.shift();
181
var arg = queue.shift();
182
fn.call(receiver, arg);
183
}
184
};
185
186
Async.prototype._drainQueues = function () {
187
this._drainQueue(this._normalQueue);
188
this._reset();
189
this._drainQueue(this._lateQueue);
190
};
191
192
Async.prototype._queueTick = function () {
193
if (!this._isTickUsed) {
194
this._isTickUsed = true;
195
this._schedule(this.drainQueues);
196
}
197
};
198
199
Async.prototype._reset = function () {
200
this._isTickUsed = false;
201
};
202
203
module.exports = new Async();
204
module.exports.firstLineError = firstLineError;
205
206