main.js 2.0 KB

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