Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
var nextTick = require('process/browser.js').nextTick;
2
var apply = Function.prototype.apply;
3
var slice = Array.prototype.slice;
4
var immediateIds = {};
5
var nextImmediateId = 0;
6
7
// DOM APIs, for completeness
8
9
exports.setTimeout = function() {
10
return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout);
11
};
12
exports.setInterval = function() {
13
return new Timeout(apply.call(setInterval, window, arguments), clearInterval);
14
};
15
exports.clearTimeout =
16
exports.clearInterval = function(timeout) { timeout.close(); };
17
18
function Timeout(id, clearFn) {
19
this._id = id;
20
this._clearFn = clearFn;
21
}
22
Timeout.prototype.unref = Timeout.prototype.ref = function() {};
23
Timeout.prototype.close = function() {
24
this._clearFn.call(window, this._id);
25
};
26
27
// Does not start the time, just sets up the members needed.
28
exports.enroll = function(item, msecs) {
29
clearTimeout(item._idleTimeoutId);
30
item._idleTimeout = msecs;
31
};
32
33
exports.unenroll = function(item) {
34
clearTimeout(item._idleTimeoutId);
35
item._idleTimeout = -1;
36
};
37
38
exports._unrefActive = exports.active = function(item) {
39
clearTimeout(item._idleTimeoutId);
40
41
var msecs = item._idleTimeout;
42
if (msecs >= 0) {
43
item._idleTimeoutId = setTimeout(function onTimeout() {
44
if (item._onTimeout)
45
item._onTimeout();
46
}, msecs);
47
}
48
};
49
50
// That's not how node.js implements it but the exposed api is the same.
51
exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) {
52
var id = nextImmediateId++;
53
var args = arguments.length < 2 ? false : slice.call(arguments, 1);
54
55
immediateIds[id] = true;
56
57
nextTick(function onNextTick() {
58
if (immediateIds[id]) {
59
// fn.call() is faster so we optimize for the common use-case
60
// @see http://jsperf.com/call-apply-segu
61
if (args) {
62
fn.apply(null, args);
63
} else {
64
fn.call(null);
65
}
66
// Prevent ids from leaking
67
exports.clearImmediate(id);
68
}
69
});
70
71
return id;
72
};
73
74
exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) {
75
delete immediateIds[id];
76
};
77