1
|
var scope = (typeof global !== "undefined" && global) ||
|
2
|
(typeof self !== "undefined" && self) ||
|
3
|
window;
|
4
|
var apply = Function.prototype.apply;
|
5
|
|
6
|
// DOM APIs, for completeness
|
7
|
|
8
|
exports.setTimeout = function() {
|
9
|
return new Timeout(apply.call(setTimeout, scope, arguments), clearTimeout);
|
10
|
};
|
11
|
exports.setInterval = function() {
|
12
|
return new Timeout(apply.call(setInterval, scope, arguments), clearInterval);
|
13
|
};
|
14
|
exports.clearTimeout =
|
15
|
exports.clearInterval = function(timeout) {
|
16
|
if (timeout) {
|
17
|
timeout.close();
|
18
|
}
|
19
|
};
|
20
|
|
21
|
function Timeout(id, clearFn) {
|
22
|
this._id = id;
|
23
|
this._clearFn = clearFn;
|
24
|
}
|
25
|
Timeout.prototype.unref = Timeout.prototype.ref = function() {};
|
26
|
Timeout.prototype.close = function() {
|
27
|
this._clearFn.call(scope, this._id);
|
28
|
};
|
29
|
|
30
|
// Does not start the time, just sets up the members needed.
|
31
|
exports.enroll = function(item, msecs) {
|
32
|
clearTimeout(item._idleTimeoutId);
|
33
|
item._idleTimeout = msecs;
|
34
|
};
|
35
|
|
36
|
exports.unenroll = function(item) {
|
37
|
clearTimeout(item._idleTimeoutId);
|
38
|
item._idleTimeout = -1;
|
39
|
};
|
40
|
|
41
|
exports._unrefActive = exports.active = function(item) {
|
42
|
clearTimeout(item._idleTimeoutId);
|
43
|
|
44
|
var msecs = item._idleTimeout;
|
45
|
if (msecs >= 0) {
|
46
|
item._idleTimeoutId = setTimeout(function onTimeout() {
|
47
|
if (item._onTimeout)
|
48
|
item._onTimeout();
|
49
|
}, msecs);
|
50
|
}
|
51
|
};
|
52
|
|
53
|
// setimmediate attaches itself to the global object
|
54
|
require("setimmediate");
|
55
|
// On some exotic environments, it's not clear which object `setimmediate` was
|
56
|
// able to install onto. Search each possibility in the same order as the
|
57
|
// `setimmediate` library.
|
58
|
exports.setImmediate = (typeof self !== "undefined" && self.setImmediate) ||
|
59
|
(typeof global !== "undefined" && global.setImmediate) ||
|
60
|
(this && this.setImmediate);
|
61
|
exports.clearImmediate = (typeof self !== "undefined" && self.clearImmediate) ||
|
62
|
(typeof global !== "undefined" && global.clearImmediate) ||
|
63
|
(this && this.clearImmediate);
|