1 |
|
2 |
|
3 | import {setImmediate, clearImmediate} from './setimmediate';
|
4 | export {setImmediate, clearImmediate};
|
5 |
|
6 | var apply = Function.prototype.apply;
|
7 |
|
8 | export function clearInterval(timeout) {
|
9 | if (typeof timeout === 'number' && typeof global.clearInterval === 'function') {
|
10 | global.clearInterval(timeout);
|
11 | } else {
|
12 | clearFn(timeout)
|
13 | }
|
14 | }
|
15 | export function clearTimeout(timeout) {
|
16 | if (typeof timeout === 'number' && typeof global.clearTimeout === 'function') {
|
17 | global.clearTimeout(timeout);
|
18 | } else {
|
19 | clearFn(timeout)
|
20 | }
|
21 | }
|
22 | function clearFn(timeout) {
|
23 | if (timeout && typeof timeout.close === 'function') {
|
24 | timeout.close();
|
25 | }
|
26 | }
|
27 | export function setTimeout() {
|
28 | return new Timeout(apply.call(global.setTimeout, window, arguments), clearTimeout);
|
29 | }
|
30 | export function setInterval() {
|
31 | return new Timeout(apply.call(global.setInterval, window, arguments), clearInterval);
|
32 | }
|
33 |
|
34 | function Timeout(id) {
|
35 | this._id = id;
|
36 | }
|
37 | Timeout.prototype.unref = Timeout.prototype.ref = function() {};
|
38 | Timeout.prototype.close = function() {
|
39 | clearFn(this._id);
|
40 | }
|
41 |
|
42 |
|
43 | export function enroll(item, msecs) {
|
44 | clearTimeout(item._idleTimeoutId);
|
45 | item._idleTimeout = msecs;
|
46 | }
|
47 |
|
48 | export function unenroll(item) {
|
49 | clearTimeout(item._idleTimeoutId);
|
50 | item._idleTimeout = -1;
|
51 | }
|
52 | export var _unrefActive = active;
|
53 | export 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 |
|
65 | export 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 | };
|