UNPKG

1.97 kBJavaScriptView Raw
1// License https://jryans.mit-license.org/
2
3import {setImmediate, clearImmediate} from './setimmediate';
4export {setImmediate, clearImmediate};
5// DOM APIs, for completeness
6var apply = Function.prototype.apply;
7
8export function clearInterval(timeout) {
9 if (typeof timeout === 'number' && typeof global.clearInterval === 'function') {
10 global.clearInterval(timeout);
11 } else {
12 clearFn(timeout)
13 }
14}
15export function clearTimeout(timeout) {
16 if (typeof timeout === 'number' && typeof global.clearTimeout === 'function') {
17 global.clearTimeout(timeout);
18 } else {
19 clearFn(timeout)
20 }
21}
22function clearFn(timeout) {
23 if (timeout && typeof timeout.close === 'function') {
24 timeout.close();
25 }
26}
27export function setTimeout() {
28 return new Timeout(apply.call(global.setTimeout, window, arguments), clearTimeout);
29}
30export function setInterval() {
31 return new Timeout(apply.call(global.setInterval, window, arguments), clearInterval);
32}
33
34function Timeout(id) {
35 this._id = id;
36}
37Timeout.prototype.unref = Timeout.prototype.ref = function() {};
38Timeout.prototype.close = function() {
39 clearFn(this._id);
40}
41
42// Does not start the time, just sets up the members needed.
43export function enroll(item, msecs) {
44 clearTimeout(item._idleTimeoutId);
45 item._idleTimeout = msecs;
46}
47
48export function unenroll(item) {
49 clearTimeout(item._idleTimeoutId);
50 item._idleTimeout = -1;
51}
52export var _unrefActive = active;
53export function active(item) {
54 clearTimeout(item._idleTimeoutId);
55
56 var msecs = item._idleTimeout;
57 if (msecs >= 0) {
58 item._idleTimeoutId = setTimeout(function onTimeout() {
59 if (item._onTimeout)
60 item._onTimeout();
61 }, msecs);
62 }
63}
64
65export default {
66 setImmediate: setImmediate,
67 clearImmediate: clearImmediate,
68 setTimeout: setTimeout,
69 clearTimeout: clearTimeout,
70 setInterval: setInterval,
71 clearInterval: clearInterval,
72 active: active,
73 unenroll: unenroll,
74 _unrefActive: _unrefActive,
75 enroll: enroll
76};