main.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var apply = Function.prototype.apply;
  2. // DOM APIs, for completeness
  3. exports.setTimeout = function() {
  4. return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout);
  5. };
  6. exports.setInterval = function() {
  7. return new Timeout(apply.call(setInterval, window, arguments), clearInterval);
  8. };
  9. exports.clearTimeout =
  10. exports.clearInterval = function(timeout) {
  11. if (timeout) {
  12. timeout.close();
  13. }
  14. };
  15. function Timeout(id, clearFn) {
  16. this._id = id;
  17. this._clearFn = clearFn;
  18. }
  19. Timeout.prototype.unref = Timeout.prototype.ref = function() {};
  20. Timeout.prototype.close = function() {
  21. this._clearFn.call(window, this._id);
  22. };
  23. // Does not start the time, just sets up the members needed.
  24. exports.enroll = function(item, msecs) {
  25. clearTimeout(item._idleTimeoutId);
  26. item._idleTimeout = msecs;
  27. };
  28. exports.unenroll = function(item) {
  29. clearTimeout(item._idleTimeoutId);
  30. item._idleTimeout = -1;
  31. };
  32. exports._unrefActive = exports.active = function(item) {
  33. clearTimeout(item._idleTimeoutId);
  34. var msecs = item._idleTimeout;
  35. if (msecs >= 0) {
  36. item._idleTimeoutId = setTimeout(function onTimeout() {
  37. if (item._onTimeout)
  38. item._onTimeout();
  39. }, msecs);
  40. }
  41. };
  42. // setimmediate attaches itself to the global object
  43. require("setimmediate");
  44. exports.setImmediate = setImmediate;
  45. exports.clearImmediate = clearImmediate;